If no parameter is provided and the constant BBN_LANG and BBN_LOCALE are not defined the function will also define those constants.
function(string $locale = NULL)
{
$locales = [];
if (empty($locale)) {
array_push(
$locales,
'en-EN.utf8',
'en_EN.utf8',
'en-EN',
'en-US.utf8',
'en_US.utf8',
'en-US',
'en',
'en_US'
);
if (!defined('BBN_LOCALE')) {
// No user detection for CLI: default language
if ($this->_mode === 'cli') {
if (defined('BBN_LANG')) {
$lang = BBN_LANG;
}
}
else {
$user_locales = self::detectLanguage();
if (!defined('BBN_LANG') && $user_locales) {
if (strpos($user_locales[0], '-')) {
if ($lang = X::split($user_locales[0], '-')[0]) {
define('BBN_LANG', $lang);
}
}
elseif (strpos($user_locales[0], '_')) {
if ($lang = X::split($user_locales[0], '_')[0]) {
define('BBN_LANG', $lang);
}
}
elseif ($user_locales[0]) {
define('BBN_LANG', $user_locales[0]);
}
}
if (!defined('BBN_LANG')) {
throw new \Exception("Impossible to determine the language");
}
$lang = BBN_LANG;
}
if (isset($lang)) {
array_unshift(
$locales,
$lang . '-' . strtoupper($lang) . '.utf8',
$lang . '_' . strtoupper($lang) . '.utf8',
$lang . '-' . strtoupper($lang),
$lang
);
if (!empty($user_locales)) {
array_unshift($locales, ...$user_locales);
}
}
}
}
elseif (!strpos($locale, '-') && !strpos($locale, '_')) {
if ($locale === 'en') {
array_unshift(
$locales,
'en_US.utf8',
'en-US.utf8',
'en_US',
'en-US'
);
}
array_unshift(
$locales,
strtolower($locale) . '-' . strtoupper($locale) . '.utf8',
strtolower($locale) . '_' . strtoupper($locale) . '.utf8',
strtolower($locale) . '-' . strtoupper($locale),
strtolower($locale)
);
}
else {
$locales[] = $locale;
}
if ($confirmed = $this->_tryLocales($locales)) {
if (!defined('BBN_LOCALE')) {
define('BBN_LOCALE', $confirmed);
}
$this->_locale = $confirmed;
if (!isset($lang)) {
$lang = X::split(X::split($this->_locale, '-')[0], '_')[0];
}
putenv("LANG=".$lang);
putenv("LC_MESSAGES=".$this->_locale);
setlocale(LC_MESSAGES, $this->_locale);
}
else {
throw new \Exception("Impossible to find a corresponding locale on this server for this app");
}
}