Get language -- locale
Moderator: General Moderators
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Get language -- locale
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.
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Code: Select all
setlocale()- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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
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?
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);
}
}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?
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.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Well spotted.
Unfortunately it was no useoutput......that's with or without that first line commented out.
Unfortunately it was no use
Code: Select all
// setlocale(LC_ALL, 'nl_NL');
var_dump(setlocale(LC_MESSAGES, '0'));Code: Select all
string(1) "C"- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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']); } }
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK