Do I understand you right that you need just a map from language (like en
) to locale (like en_GB
)? If so, you can compile one for the languages you use:
$localeMap = array(
'en' => 'en_GB',
'fr' => 'fr_FR',
# ...
);
# Usage:
$locale = $localeMap[$lang];
But this is so trivial that I'm unsure if I properly understood your question.
If you are unsure what the locale is for a language, just take the language itself which should be a valid locale, just without a country:
l => ll
As long as the language is in the two letter format looks fine to me (ISO standard 639, "Code for the representation of names of languages"), See as well Tags for the Identification of Languages (RFC 1766).
$locale = isset($localeMap[$lang]) ? $localeMap[$lang] : $lang;
However this can differ depending which kind of locale format the function you're using is expecting.
class System
{
/**
* @return array
*/
public function getLocales()
{
$locales = array();
exec('locale -a', $locales, $retCode);
!$retCode || $locales = array();
return $locales;
}
/**
* @return string something matching
* @note Improve by creating your system aware locale class to move
* away the responsibility to map system locales onto strings
* of certain kinds and to deal with the different locale types.
*/
public function getLocaleByLang($lang)
{
...
}
}
$lang = ...
$system = new System;
$locale = $system->getLocaleByLang($lang);