diff --git a/files/_locales/remove.txt b/files/_locales/remove.txt new file mode 100644 index 00000000000..977321426cf --- /dev/null +++ b/files/_locales/remove.txt @@ -0,0 +1,3 @@ +Vous pouvez effacer ce fichier sans dommages. + +You can safely remove this file. diff --git a/inc/based_config.php b/inc/based_config.php index ba7260459de..9075081e19e 100644 --- a/inc/based_config.php +++ b/inc/based_config.php @@ -85,6 +85,12 @@ define("GLPI_SESSION_DIR", GLPI_VAR_DIR . "/_sessions"); } +// Path for local i18n files +define('GLPI_I18N_DIR', GLPI_ROOT . "/locales"); +if (!defined("GLPI_LOCAL_I18N_DIR")) { + define("GLPI_LOCAL_I18N_DIR", GLPI_VAR_DIR . "/_locales"); +} + // Path for plugins documents storage if (!defined("GLPI_PLUGIN_DOC_DIR")) { define("GLPI_PLUGIN_DOC_DIR", GLPI_VAR_DIR . "/_plugins"); diff --git a/inc/plugin.class.php b/inc/plugin.class.php index b8d9713fa67..332d22b50ea 100644 --- a/inc/plugin.class.php +++ b/inc/plugin.class.php @@ -197,40 +197,37 @@ static function loadLang($name, $forcelang = '', $coretrytoload = '') { $dir = GLPI_ROOT . "/plugins/$name/locales/"; - $translation_included = false; + $mofile = false; // New localisation system if (file_exists($dir.$CFG_GLPI["languages"][$trytoload][1])) { - $TRANSLATE->addTranslationFile('gettext', - $dir.$CFG_GLPI["languages"][$trytoload][1], - $name, $coretrytoload); - - $translation_included = true; - + $mofile = $dir.$CFG_GLPI["languages"][$trytoload][1]; } else if (!empty($CFG_GLPI["language"]) && file_exists($dir.$CFG_GLPI["languages"][$CFG_GLPI["language"]][1])) { - $TRANSLATE->addTranslationFile('gettext', - $dir.$CFG_GLPI["languages"][$CFG_GLPI["language"]][1], - $name, $coretrytoload); - $translation_included = true; + $mofile = $dir.$CFG_GLPI["languages"][$CFG_GLPI["language"]][1]; } else if (file_exists($dir."en_GB.mo")) { - $TRANSLATE->addTranslationFile('gettext', - $dir."en_GB.mo", - $name, $coretrytoload); - $translation_included = true; - - } - - if (!$translation_included) { - if (file_exists($dir.$trytoload.'.php')) { - include ($dir.$trytoload.'.php'); - } else if (isset($CFG_GLPI["language"]) - && file_exists($dir.$CFG_GLPI["language"].'.php')) { - include ($dir.$CFG_GLPI["language"].'.php'); - } else if (file_exists($dir . "en_GB.php")) { - include ($dir . "en_GB.php"); - } else if (file_exists($dir . "fr_FR.php")) { - include ($dir . "fr_FR.php"); - } + $mofile = $dir."en_GB.mo"; + } + + if ($mofile !== false) { + $TRANSLATE->addTranslationFile( + 'gettext', + $mofile, + $name, + $coretrytoload + ); + } + + $mofile = str_replace($dir, GLPI_LOCAL_I18N_DIR . '/'.$name, $mofile); + $phpfile = str_replace('.mo', '.php', $mofile); + + // Load local PHP file if it exists + if (file_exists($phpfile)) { + $TRANSLATE->addTranslationFile('phparray', $phpfile, $name, $coretrytoload); + } + + // Load local MO file if it exists -- keep last so it gets precedence + if (file_exists($mofile)) { + $TRANSLATE->addTranslationFile('gettext', $mofile, $name, $coretrytoload); } } diff --git a/inc/session.class.php b/inc/session.class.php index af17569be22..a876b2f15ab 100644 --- a/inc/session.class.php +++ b/inc/session.class.php @@ -593,11 +593,11 @@ static function loadLanguage($forcelang = '', $with_plugins = true) { } if (isset($CFG_GLPI["languages"][$trytoload])) { - $newfile = "/locales/" . $CFG_GLPI["languages"][$trytoload][1]; + $newfile = "/" . $CFG_GLPI["languages"][$trytoload][1]; } - if (empty($newfile) || !is_file(GLPI_ROOT . $newfile)) { - $newfile = "/locales/en_GB.mo"; + if (empty($newfile) || !is_file(GLPI_I18N_DIR . $newfile)) { + $newfile = "/en_GB.mo"; } if (isset($CFG_GLPI["languages"][$trytoload][5])) { @@ -607,11 +607,24 @@ static function loadLanguage($forcelang = '', $with_plugins = true) { $TRANSLATE->setLocale($trytoload); $cache = Config::getCache('cache_trans', 'core', false); - if ($cache !== false) { + if ($cache !== false && !defined('TU_USER')) { $TRANSLATE->setCache($cache); } - $TRANSLATE->addTranslationFile('gettext', GLPI_ROOT.$newfile, 'glpi', $trytoload); + $TRANSLATE->addTranslationFile('gettext', GLPI_I18N_DIR.$newfile, 'glpi', $trytoload); + + $mofile = GLPI_LOCAL_I18N_DIR . '/core/' . $newfile; + $phpfile = str_replace('.mo', '.php', $mofile); + + // Load local PHP file if it exists + if (file_exists($phpfile)) { + $TRANSLATE->addTranslationFile('phparray', $phpfile, 'glpi', $trytoload); + } + + // Load local MO file if it exists -- keep last so it gets precedence + if (file_exists($mofile)) { + $TRANSLATE->addTranslationFile('gettext', $mofile, 'glpi', $trytoload); + } // Load plugin dicts if ($with_plugins) { diff --git a/tests/functionnal/Session.php b/tests/functionnal/Session.php index 6d44cfe15e8..9d1487212ca 100644 --- a/tests/functionnal/Session.php +++ b/tests/functionnal/Session.php @@ -133,4 +133,39 @@ function () { $this->array($_SESSION['MESSAGE_AFTER_REDIRECT'])->isEmpty(); } + + public function testLocalI18n() { + //load locales + \Session::loadLanguage('en_GB'); + $this->string(__('Login'))->isIdenticalTo('Login'); + + //create directory for local i18n + if (!file_exists(GLPI_LOCAL_I18N_DIR.'/core')) { + mkdir(GLPI_LOCAL_I18N_DIR.'/core'); + } + + //write local MO file with i18n override + copy( + __DIR__ . '/../local_en_GB.mo', + GLPI_LOCAL_I18N_DIR.'/core/en_GB.mo' + ); + \Session::loadLanguage('en_GB'); + + $this->string(__('Login'))->isIdenticalTo('Login from local gettext'); + $this->string(__('Password'))->isIdenticalTo('Password'); + + //write local PHP file with i18n override + file_put_contents( + GLPI_LOCAL_I18N_DIR.'/core/en_GB.php', + "string(__('Login'))->isIdenticalTo('Login from local gettext'); + $this->string(__('Password'))->isIdenticalTo('Password from local PHP'); + + //cleanup -- keep at the end + unlink(GLPI_LOCAL_I18N_DIR.'/core/en_GB.php'); + unlink(GLPI_LOCAL_I18N_DIR.'/core/en_GB.mo'); + } } diff --git a/tests/local_en_GB.mo b/tests/local_en_GB.mo new file mode 100644 index 00000000000..933878e407a Binary files /dev/null and b/tests/local_en_GB.mo differ diff --git a/tests/local_en_GB.po b/tests/local_en_GB.po new file mode 100644 index 00000000000..94cf79750c8 --- /dev/null +++ b/tests/local_en_GB.po @@ -0,0 +1,24 @@ +# Override PO file +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2019-04-13 10:43+0200\n" +"PO-Revision-Date: 2019-04-13 10:43+0200\n" +"Last-Translator: Automatically generated\n" +"Language-Team: none\n" +"Language: en_GB\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#: index.php:131 inc/authldap.class.php:3198 inc/authmail.class.php:231 +#: inc/config.class.php:1762 inc/dbutils.class.php:1647 +#: inc/mailcollector.class.php:270 inc/mailcollector.class.php:473 +#: inc/notificationtargetuser.class.php:107 inc/ruleright.class.php:259 +#: inc/user.class.php:1995 inc/user.class.php:2426 inc/user.class.php:2884 +#: inc/user.class.php:3892 +msgid "Login" +msgstr "Login from local gettext"