Build the language files
public static buildCache ( string $language, string $application ) | ||
$language | string | The language to build the locale-file for. |
$application | string | The application to build the locale-file for. |
/** * Create locale cache files * * @param InstallationData $data */ protected function createLocaleFiles(InstallationData $data) { // all available languages $languages = array_unique(array_merge($data->getLanguages(), $data->getInterfaceLanguages())); // loop all the languages foreach ($languages as $language) { // get applications $applications = $this->container->get('database')->getColumn('SELECT DISTINCT application FROM locale WHERE language = ?', array((string) $language)); // loop applications foreach ((array) $applications as $application) { // build application locale cache BackendLocaleModel::buildCache($language, $application); } } }
/** * Set locale * It will require the correct file and init the needed vars * * @param string $language The language to load. */ public static function setLocale($language) { $language = (string) $language; // validate file, generate it if needed if (!is_file(BACKEND_CACHE_PATH . '/Locale/en.json')) { BackendLocaleModel::buildCache('en', APPLICATION); } if (!is_file(BACKEND_CACHE_PATH . '/Locale/' . $language . '.json')) { // if you use the language in the console act like it is in the backend BackendLocaleModel::buildCache($language, defined('APPLICATION') && APPLICATION === 'Console' ? 'Backend' : APPLICATION); } // store self::$currentInterfaceLanguage = $language; // attempt to set a cookie try { // Needed to make it possible to use the backend language in the console. if (defined('APPLICATION') && APPLICATION !== 'Console') { CommonCookie::set('interface_language', $language); } } catch (\SpoonCookieException $e) { // settings cookies isn't allowed, because this isn't a real problem we ignore the exception } // set English translations, they'll be the fallback $translations = json_decode(file_get_contents(BACKEND_CACHE_PATH . '/Locale/en.json'), true); self::$err = (array) $translations['err']; self::$lbl = (array) $translations['lbl']; self::$msg = (array) $translations['msg']; // overwrite with the requested language's translations $translations = json_decode(file_get_contents(BACKEND_CACHE_PATH . '/Locale/' . $language . '.json'), true); $err = (array) $translations['err']; $lbl = (array) $translations['lbl']; $msg = (array) $translations['msg']; foreach ($err as $module => $translations) { if (!isset(self::$err[$module])) { self::$err[$module] = array(); } self::$err[$module] = array_merge(self::$err[$module], $translations); } foreach ($lbl as $module => $translations) { if (!isset(self::$lbl[$module])) { self::$lbl[$module] = array(); } self::$lbl[$module] = array_merge(self::$lbl[$module], $translations); } foreach ($msg as $module => $translations) { if (!isset(self::$msg[$module])) { self::$msg[$module] = array(); } self::$msg[$module] = array_merge(self::$msg[$module], $translations); } }