Contao\System::loadLanguageFile PHP Method

loadLanguageFile() public static method

Load a set of language files
public static loadLanguageFile ( string $strName, boolean $strLanguage = null, boolean $blnNoCache = false )
$strName string The table name
$strLanguage boolean An optional language code
$blnNoCache boolean If true, the cache will be bypassed
    public static function loadLanguageFile($strName, $strLanguage = null, $blnNoCache = false)
    {
        if ($strLanguage === null) {
            $strLanguage = str_replace('-', '_', $GLOBALS['TL_LANGUAGE']);
        }
        // Fall back to English
        if ($strLanguage == '') {
            $strLanguage = 'en';
        }
        // Return if the language file has been loaded already
        if (isset(static::$arrLanguageFiles[$strName][$strLanguage]) && !$blnNoCache) {
            return;
        }
        $strCacheKey = $strLanguage;
        // Make sure the language exists
        if (!static::isInstalledLanguage($strLanguage)) {
            $strShortLang = substr($strLanguage, 0, 2);
            // Fall back to "de" if "de_DE" does not exist
            if ($strShortLang != $strLanguage && static::isInstalledLanguage($strShortLang)) {
                $strLanguage = $strShortLang;
            } else {
                $strLanguage = 'en';
            }
        }
        // Use a global cache variable to support nested calls
        static::$arrLanguageFiles[$strName][$strCacheKey] = $strLanguage;
        // Fall back to English
        $arrCreateLangs = $strLanguage == 'en' ? array('en') : array('en', $strLanguage);
        // Prepare the XLIFF loader
        $xlfLoader = new XliffFileLoader(static::getContainer()->getParameter('kernel.root_dir'), true);
        $strCacheDir = static::getContainer()->getParameter('kernel.cache_dir');
        // Load the language(s)
        foreach ($arrCreateLangs as $strCreateLang) {
            // Try to load from cache
            if (file_exists($strCacheDir . '/contao/languages/' . $strCreateLang . '/' . $strName . '.php')) {
                include $strCacheDir . '/contao/languages/' . $strCreateLang . '/' . $strName . '.php';
            } else {
                try {
                    $files = static::getContainer()->get('contao.resource_locator')->locate('languages/' . $strCreateLang . '/' . $strName . '.php', null, false);
                } catch (\InvalidArgumentException $e) {
                    $files = array();
                }
                foreach ($files as $file) {
                    include $file;
                }
                try {
                    $files = static::getContainer()->get('contao.resource_locator')->locate('languages/' . $strCreateLang . '/' . $strName . '.xlf', null, false);
                } catch (\InvalidArgumentException $e) {
                    $files = array();
                }
                foreach ($files as $file) {
                    $xlfLoader->load($file, $strCreateLang);
                }
            }
        }
        // HOOK: allow to load custom labels
        if (isset($GLOBALS['TL_HOOKS']['loadLanguageFile']) && is_array($GLOBALS['TL_HOOKS']['loadLanguageFile'])) {
            foreach ($GLOBALS['TL_HOOKS']['loadLanguageFile'] as $callback) {
                static::importStatic($callback[0])->{$callback[1]}($strName, $strLanguage, $strCacheKey);
            }
        }
        // Handle single quotes in the deleteConfirm message
        if ($strName == 'default') {
            $GLOBALS['TL_LANG']['MSC']['deleteConfirm'] = str_replace("'", "\\'", $GLOBALS['TL_LANG']['MSC']['deleteConfirm']);
        }
        // Local configuration file
        if (file_exists(TL_ROOT . '/system/config/langconfig.php')) {
            @trigger_error('Using the langconfig.php file has been deprecated and will no longer work in Contao 5.0. Create one or more language files in app/Resources/contao/languages instead.', E_USER_DEPRECATED);
            include TL_ROOT . '/system/config/langconfig.php';
        }
    }

Usage Example

Example #1
0
 /**
  * @return array
  */
 public function getCurrencies()
 {
     $return = array();
     $arrAux = array();
     \Contao\System::loadLanguageFile('currencies');
     $this->loadCurrencies();
     if (is_array($this->arrCurrencies)) {
         foreach ($this->arrCurrencies as $strKey => $strName) {
             $arrAux[$strKey] = isset($GLOBALS['TL_LANG']['CUR'][$strKey]) ? Utf8::toAscii($GLOBALS['TL_LANG']['CUR'][$strKey]) : $strName;
         }
     }
     asort($arrAux);
     if (is_array($arrAux)) {
         foreach (array_keys($arrAux) as $strKey) {
             $return[$strKey] = isset($GLOBALS['TL_LANG']['CUR'][$strKey]) ? $GLOBALS['TL_LANG']['CUR'][$strKey] : $this->arrCurrencies[$strKey];
         }
     }
     // HOOK: add custom logic
     if (isset($GLOBALS['TL_HOOKS']['getCurrencies']) && is_array($GLOBALS['TL_HOOKS']['getCurrencies'])) {
         foreach ($GLOBALS['TL_HOOKS']['getCurrencies'] as $callback) {
             $return = static::importStatic($callback[0])->{$callback}[1]($return, $this->arrCurrencies);
         }
     }
     return $return;
 }
All Usage Examples Of Contao\System::loadLanguageFile