Page 1 of 1

Get language -- locale

Posted: Sun Aug 06, 2006 12:29 pm
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.

Posted: Sun Aug 06, 2006 6:52 pm
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?

Posted: Sun Aug 06, 2006 7:24 pm
by Ollie Saunders

Code: Select all

setlocale()

Posted: Sun Aug 06, 2006 7:33 pm
by feyd
Why not map the locales with their corresponding language(s)?

Posted: Sun Aug 06, 2006 8:04 pm
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?

Posted: Sun Aug 06, 2006 8:37 pm
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.

Posted: Sun Aug 06, 2006 9:00 pm
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.

Posted: Sun Aug 06, 2006 9:11 pm
by Benjamin
I'm curious as to why you have that function declared as static.

Posted: Sun Aug 06, 2006 9:15 pm
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?

Posted: Sun Aug 06, 2006 9:21 pm
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.

Posted: Sun Aug 06, 2006 9:24 pm
by Ambush Commander
It's a kind of namespacing I suspect.

Posted: Sun Aug 06, 2006 9:53 pm
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']);
    	}
    }

Posted: Mon Aug 07, 2006 6:09 pm
by Ollie Saunders
So, going back to my locale problem, is this something beyond PHP?
It seems like nobody knows a solution to this. :(