Skip to content

Commit

Permalink
Load specific locales from MO or PHP files
Browse files Browse the repository at this point in the history
PHP strings have precedence on standard strings,
and MO have precedence on PHP strings.

Drop legacy code
  • Loading branch information
trasher committed May 15, 2019
1 parent 991b3cd commit 242b4ae
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 34 deletions.
3 changes: 3 additions & 0 deletions files/_locales/remove.txt
@@ -0,0 +1,3 @@
Vous pouvez effacer ce fichier sans dommages.

You can safely remove this file.
6 changes: 6 additions & 0 deletions inc/based_config.php
Expand Up @@ -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");
Expand Down
55 changes: 26 additions & 29 deletions inc/plugin.class.php
Expand Up @@ -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);
}
}

Expand Down
23 changes: 18 additions & 5 deletions inc/session.class.php
Expand Up @@ -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])) {
Expand All @@ -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) {
Expand Down
35 changes: 35 additions & 0 deletions tests/functionnal/Session.php
Expand Up @@ -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',
"<?php\n\$lang['Login'] = 'Login from local PHP';\n\$lang['Password'] = 'Password from local PHP';\nreturn \$lang;"
);
\Session::loadLanguage('en_GB');

$this->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');
}
}
Binary file added tests/local_en_GB.mo
Binary file not shown.
24 changes: 24 additions & 0 deletions 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"

0 comments on commit 242b4ae

Please sign in to comment.