Get language -- locale

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Get language -- locale

Post by Ollie Saunders »

I am trying to obtain the human language as it understands given the locale set. In particular I am trying to find out if it is English or not.
I've looked at http://www.php.net/manual/en/function.localeconv.php and http://www.php.net/manual/en/function.nl-langinfo.php but I don't see human language there.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

That might be kind of difficult or kind of easy, depending on if I'm reading you correctly.

Which I don't think I am.

What is this "Locale set" you speak of?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

setlocale()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why not map the locales with their corresponding language(s)?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

OK I think I need to explain myself better. I didn't really know how to phase the question so I'm going to show you the specific context of the problem.

I have this method

Code: Select all

public static function titleFormat($value)
{
    /**
     * @todo work out the current language on the system somehow, this will
     * have to work for both linux and windows systems
     */
    $isEnglish = true;
    if ($isEnglish) {
        $word = preg_split('/\s+/', $value);

        // using keys for performance
        $keyword = array('of' => 1, 'a' => 1, 'the' => 1, 'and' => 1, 'an' => 1, 'or' => 1, 'nor' => 1,
                         'but' => 1, 'is' => 1, 'if' => 1, 'then' => 1, 'else' => 1, 'when' => 1,
                         'at' => 1, 'from' => 1, 'by' => 1, 'on' => 1, 'off' => 1, 'for' => 1,
                         'in' => 1, 'out' => 1, 'over' => 1, 'to' => 1, 'into' => 1, 'with' => 1);
        for ($i=0, $j=count($word); $i<$j; $i++) {
            if (strlen($word[$i]) < 4) {
                $lower = strtolower($word[$i]);
                if (isset($keyword[$lower])) {
                    $word[$i] = $lower;
                }
            } else {
                $word[$i] = ucfirst($word[$i]);
            }
        }
    } else {
        return ucwords($value);
    }
}
The method formats a string into title case (Title Case is Like This). In title case common English words should not be ucfirst()ed but of course this only applies to English, and if it does apply to other languages the words will be different.

So I want to find out what the language is, now I could just use an argument or query the server somehow, but if there is a built in system in PHP, part of the locale system (or what ever you call it) that would be best.

As setlocale is used to set regional information and I would have thought language is a part of that. Looking on the manual I see things like en, fr, and es being used in setlocale, I also remember having to enter 'Europe/London' into a function once to meet an E_STRICT demand. So what I am asking is how do I get that information out?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

you can use setlocale(LC_MESSAGES, "0") for that:
PHP Manual wrote: If locale is "0", the locale setting is not affected, only the current setting is returned.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Well spotted.

Unfortunately it was no use

Code: Select all

// setlocale(LC_ALL, 'nl_NL');
var_dump(setlocale(LC_MESSAGES, '0'));
output...

Code: Select all

string(1) "C"
...that's with or without that first line commented out.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I'm curious as to why you have that function declared as static.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Because its part of a class with all static methods. Each one filters in a different way and none of them require an internal class property. Also the calling of them relies on it being static elsewhere in my code. Do you have any reason for it not to be static?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

No reason for it not to be, it just seemed strange to me. If it's just a function that doesn't need to be in an instantiated class, I would be inclined to say just put it in a function file, unless your just trying to make your code more organized or something.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It's a kind of namespacing I suspect.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I am using a class because:
  • There are about 30 methods all doing a similar task
  • Programmers will likely want to extend it
  • To group together different sets of filtering
  • This code relies upon it (self::$filterClass can be reassigned to point to any filter class):

    Code: Select all

    protected function _filter()
    {
    	$class = self::$filterClass;
    	foreach ($this->_filters as $fnc) {
    		$fnc['parameters'][0] = $this->_input;
    		$this->_input = call_user_func_array(array($class, $fnc['name']), $fnc['parameters']);
    	}
    }
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

So, going back to my locale problem, is this something beyond PHP?
It seems like nobody knows a solution to this. :(
Post Reply