public function setLocale($locale)
{
// fetch locales list
$locales = Config::get('l4gettext::locales.list');
// sanity check
if (!in_array($locale, $locales)) {
throw new InvalidLocaleException("The provided locale [{$locale}] does not exist in the list of valid locales [config/locales.php]");
}
// set locale in class
$this->locale = $locale;
// get localecodeset
$localecodeset = $this->getLocaleAndEncoding();
// set environment variable
if (!putenv('LC_ALL=' . $localecodeset)) {
throw new EnvironmentNotSetException("The given locale [{$localecodeset}] could not be set as environment [LC_ALL] variable; it seems it does not exist on this system");
}
if (!putenv('LANG=' . $localecodeset)) {
throw new EnvironmentNotSetException("The given locale [{$localecodeset}] could not be set as environment [LANG] variable; it seems it does not exist on this system");
}
// set locale - the exception is only thrown in case the app is NOT run from the command line
// ignoring the cli creates a chicken and egg problem when attempting to fetch the installed locales/encodings,
// since the ServiceProvider will always attempt to load the locale/encoding before the config files can even be
// published; thus not allowing the user to change the default settings which should prevent the exception in the first place
if (!setlocale(LC_ALL, $localecodeset) && !\App::runningInConsole()) {
throw new LocaleNotFoundException("The given locale [{$localecodeset}] could not be set; it seems it does not exist on this system");
}
// save locale to session
Session::put('l4gettext_locale', $this->locale);
// return - allow object chaining
return $this;
}