Language 'Recompile' bug

After adding  a new form field with 'title' and 'system title'. I created a new language key in the corresponding language file. For this new key to appear correctly I 'recompiled' the language files to reproduce the lang-en.xml file in the cache folder.

My notice from many posts here on the discussion, is that compileLanguage() function is faulty and the reproduced lang-en.xml file in the cache folder would be missing many keys, including the original UNA system keys for profile, privacy, membership levels and other.
LeonidS could you please explain the logic of the compileLanguage() function so that I can try to play with it to see wheather there might be a bug that makes lang-en.xml file not generated correctly!

here is the code from BxDolStudioLanguagesUtils.php:

    function compileLanguage($mixedLang = 0, $bForce = false)

    {

        $sType = 'all';

        if(!empty($mixedLang)) {

            if(is_string($mixedLang) && !is_numeric($mixedLang))

                $sType = 'all_by_name';

            else if(is_numeric($mixedLang) || is_int($mixedLang)) {

                $sType = 'all_by_id';

                $mixedLang = (int)$mixedLang;

            }

        }

 

        $aLanguages = array();

        $iLanguages = $this->oDb->getLanguagesBy(array('type' => $sType, 'value' => $mixedLang), $aLanguages);

 

        if($iLanguages == 0)

            return false;

 

        $oFile = BxDolFile::getInstance();

 

        $sNewLine = "\r\n";

        foreach($aLanguages as $aLanguage) {

            if(!$bForce && (int)$aLanguage['enabled'] != 1)

                continue;

 

            $aKeys = array();

            $this->oDb->getKeysBy(array('type' => 'by_language_id_key_key', 'value' => $aLanguage['id']), $aKeys);

 

            if($aLanguage['name'] != BX_DOL_LANGUAGE_DEFAULT && getParam('lang_subst_from_en') == 'on') {

                $aKeysAll = array();

                $this->oDb->getKeysBy(array('type' => 'by_language_name_key_key', 'value' => BX_DOL_LANGUAGE_DEFAULT), $aKeysAll);

 

                $aKeys = array_merge($aKeysAll, $aKeys);

            }

 

            $sLanguageFile = "lang-" . $aLanguage['name'] . ".php";

            $oFile->delete('cache/' . $sLanguageFile);

 

            $rHandle = fopen( BX_DIRECTORY_PATH_CACHE . $sLanguageFile, 'w');

            if($rHandle === false)

                return false;

 

            $sContent = "

            foreach($aKeys as $aKey) {

                list($sKey, $sString) = str_replace(array("\\", "'"), array("\\\\", "\\'"), array($aKey['key'], $aKey['string']));

                $sContent .= "{$sNewLine}\t'$sKey' => '$sString',";

            }

 

            $sContent = trim($sContent, ',');

 

            if(fwrite($rHandle, $sContent."{$sNewLine});?>") === false)

                return false;

 

            if(fclose($rHandle) === false)

                return false;

 

            @chmod( BX_DIRECTORY_PATH_CACHE . "lang-{$aLanguage['name']}.php", 0666);

 

            if (function_exists('opcache_invalidate')) opcache_invalidate(BX_DIRECTORY_PATH_CACHE . "lang-{$aLanguage['name']}.php");

        }

 

        $this->init();

        BxDolCacheUtilities::getInstance()->clear('template');

 

        return true;

    }

 

  • 555
  • More
Replies (7)
    • Hello Scholar !

      This method (compileLanguage) takes the data from the database, not from the XML files. The method installLanguage uses the XML-files as source.

      • LeonidS thanks for your reply. Can you advise on the sequence of events?

        1- Assuming I'm installing French language, which method gets the keys from xml files to build the database (I presume this is mainly sys_localization tables,any other tables involved?).

        2-Then after getting these new language keys into the database, which method is used to extract the new keys and build the cache file 'lang-fr.xml'.

        3- Finally, how the Polyglot work? does it change the database? if this is the case, then 'restore' will only update the values of the keys which are already on the database, but will not process new keys added from Polyglot.
        Explanation of the process will be really helpful for us to troubleshoot many reported problems with language keys on the forum.

        • 1-2) It is processed by method installLanguage by the mentioned BxDolStudioLanguagesUtils class. All new keys and values will be recorded to the database and then the file cache/lang-[lang name].php will be generated.

          3) Polyglot makes the changes in the database for all keys, but during the restoration updates only those keys which were existing in the original XML files of this language.

          •  All new keys and values will be recorded to the database (are they sys_localization_ tables only? any other tables involved?)

            3) Polyglot makes the changes in the database for all keys, but during the restoration updates only those keys which were existing in the original XML files of this language (what happens then if you add a new lang key through Polyglot? does it create a new key pair in the database? also, who does it get the new lang keys into the Cache lang-en.php?)

            • Yes, lang keys and strings are placed into sys_localization_  tables only. Yes, Polyglot creates the new pair key-string in the database tables `sys_localization_keys` and `sys_localization_strings` and then translates them to the language file (it is rewritten completely).

              • one last question, when polyglot rewrite the lang-php cache files after adding a new pair, which function does it use? is it restore or recompile function?

                • The following code:

                  if(!BxDolStudioLanguagesUtils::getInstance()->updateLanguageStringById($iId, $aStrings))

                  call the next one:

                  if($bRecompile)

                         $this->compileLanguage($aLanguage['id']);

                  Login or Join to comment.