2014-07-23 15:52:50 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MINZ - Copyright 2011 Marien Fressinaud
|
|
|
|
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-02-08 18:55:48 +01:00
|
|
|
* This class is used for the internationalization.
|
|
|
|
* It uses files in `./app/i18n/`
|
2014-07-23 15:52:50 +02:00
|
|
|
*/
|
|
|
|
class Minz_Translate {
|
|
|
|
/**
|
2015-02-08 18:55:48 +01:00
|
|
|
* $path_list is the list of registered base path to search translations.
|
2014-07-23 15:52:50 +02:00
|
|
|
*/
|
2015-02-08 18:55:48 +01:00
|
|
|
private static $path_list = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $lang_name is the name of the current language to use.
|
|
|
|
*/
|
|
|
|
private static $lang_name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $lang_files is a list of registered i18n files.
|
|
|
|
*/
|
|
|
|
private static $lang_files = array();
|
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
/**
|
2015-02-08 18:55:48 +01:00
|
|
|
* $translates is a cache for i18n translation.
|
2014-07-23 15:52:50 +02:00
|
|
|
*/
|
|
|
|
private static $translates = array();
|
2015-02-08 18:55:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init the translation object.
|
|
|
|
* @param $lang_name the lang to show.
|
|
|
|
*/
|
|
|
|
public static function init($lang_name = null) {
|
|
|
|
self::$lang_name = $lang_name;
|
|
|
|
self::$lang_files = array();
|
|
|
|
self::$translates = array();
|
|
|
|
self::registerPath(APP_PATH . '/i18n');
|
|
|
|
foreach (self::$path_list as $path) {
|
|
|
|
self::loadLang($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the translation object with a new language.
|
|
|
|
* @param $lang_name the new language to use
|
|
|
|
*/
|
|
|
|
public static function reset($lang_name) {
|
|
|
|
self::$lang_name = $lang_name;
|
|
|
|
self::$lang_files = array();
|
|
|
|
self::$translates = array();
|
|
|
|
foreach (self::$path_list as $path) {
|
|
|
|
self::loadLang($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the list of available languages.
|
|
|
|
* @return an array containing langs found in different registered paths.
|
|
|
|
*/
|
|
|
|
public static function availableLanguages() {
|
|
|
|
$list_langs = array();
|
|
|
|
|
|
|
|
foreach (self::$path_list as $path) {
|
|
|
|
$path_langs = array_values(array_diff(
|
|
|
|
scandir($path),
|
|
|
|
array('..', '.')
|
|
|
|
));
|
|
|
|
|
|
|
|
$list_langs = array_merge($list_langs, $path_langs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($list_langs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a new path.
|
|
|
|
* @param $path a path containing i18n directories (e.g. ./en/, ./fr/).
|
|
|
|
*/
|
|
|
|
public static function registerPath($path) {
|
|
|
|
if (in_array($path, self::$path_list)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$path_list[] = $path;
|
|
|
|
self::loadLang($path);
|
|
|
|
}
|
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
/**
|
2015-02-08 18:55:48 +01:00
|
|
|
* Load translations of the current language from the given path.
|
|
|
|
* @param $path the path containing i18n directories.
|
2014-07-23 15:52:50 +02:00
|
|
|
*/
|
2015-02-08 18:55:48 +01:00
|
|
|
private static function loadLang($path) {
|
|
|
|
$lang_path = $path . '/' . self::$lang_name;
|
|
|
|
if (!file_exists($lang_path) || is_null(self::$lang_name)) {
|
|
|
|
// The lang path does not exist, nothing more to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$list_i18n_files = array_values(array_diff(
|
|
|
|
scandir($lang_path),
|
|
|
|
array('..', '.')
|
|
|
|
));
|
|
|
|
|
|
|
|
// Each file basename correspond to a top-level i18n key. For each of
|
|
|
|
// these keys we store the file pathname and mark translations must be
|
|
|
|
// reloaded (by setting $translates[$i18n_key] to null).
|
|
|
|
foreach ($list_i18n_files as $i18n_filename) {
|
|
|
|
$i18n_key = basename($i18n_filename, '.php');
|
|
|
|
if (!isset(self::$lang_files[$i18n_key])) {
|
|
|
|
self::$lang_files[$i18n_key] = array();
|
|
|
|
}
|
|
|
|
self::$lang_files[$i18n_key][] = $lang_path . '/' . $i18n_filename;
|
|
|
|
self::$translates[$i18n_key] = null;
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
/**
|
2015-02-08 18:55:48 +01:00
|
|
|
* Load the files associated to $key into $translates.
|
|
|
|
* @param $key the top level i18n key we want to load.
|
2014-07-23 15:52:50 +02:00
|
|
|
*/
|
2015-02-08 18:55:48 +01:00
|
|
|
private static function loadKey($key) {
|
|
|
|
// The top level key is not in $lang_files, it means it does not exist!
|
|
|
|
if (!isset(self::$lang_files[$key])) {
|
|
|
|
Minz_Log::debug($key . ' is not a valid top level key');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$translates[$key] = array();
|
|
|
|
|
|
|
|
foreach (self::$lang_files[$key] as $lang_pathname) {
|
|
|
|
$i18n_array = include($lang_pathname);
|
|
|
|
if (!is_array($i18n_array)) {
|
|
|
|
Minz_Log::warning('`' . $lang_pathname . '` does not contain a PHP array');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must avoid to erase previous data so we just override them if
|
|
|
|
// needed.
|
|
|
|
self::$translates[$key] = array_replace_recursive(
|
|
|
|
self::$translates[$key], $i18n_array
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
/**
|
2015-02-08 18:55:48 +01:00
|
|
|
* Translate a key into its corresponding value based on selected language.
|
|
|
|
* @param $key the key to translate.
|
|
|
|
* @param additional parameters for variable keys.
|
|
|
|
* @return the value corresponding to the key.
|
|
|
|
* If no value is found, return the key itself.
|
2014-07-23 15:52:50 +02:00
|
|
|
*/
|
|
|
|
public static function t($key) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$group = explode('.', $key);
|
|
|
|
|
|
|
|
if (count($group) < 2) {
|
|
|
|
Minz_Log::debug($key . ' is not in a valid format');
|
|
|
|
$top_level = 'gen';
|
|
|
|
} else {
|
|
|
|
$top_level = array_shift($group);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If $translates[$top_level] is null it means we have to load the
|
|
|
|
// corresponding files.
|
|
|
|
if (!isset(self::$translates[$top_level]) ||
|
|
|
|
is_null(self::$translates[$top_level])) {
|
|
|
|
$res = self::loadKey($top_level);
|
|
|
|
if (!$res) {
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through the i18n keys to get the correct translation value.
|
|
|
|
$translates = self::$translates[$top_level];
|
|
|
|
$size_group = count($group);
|
|
|
|
$level_processed = 0;
|
|
|
|
$translation_value = $key;
|
|
|
|
foreach ($group as $i18n_level) {
|
|
|
|
$level_processed++;
|
|
|
|
if (!isset($translates[$i18n_level])) {
|
|
|
|
Minz_Log::debug($key . ' is not a valid key');
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($level_processed < $size_group) {
|
|
|
|
$translates = $translates[$i18n_level];
|
|
|
|
} else {
|
|
|
|
$translation_value = $translates[$i18n_level];
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
if (is_array($translation_value)) {
|
|
|
|
if (isset($translation_value['_'])) {
|
|
|
|
$translation_value = $translation_value['_'];
|
|
|
|
} else {
|
|
|
|
Minz_Log::debug($key . ' is not a valid key');
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the facultative arguments to replace i18n variables.
|
2014-07-23 15:52:50 +02:00
|
|
|
$args = func_get_args();
|
|
|
|
unset($args[0]);
|
2015-02-08 18:55:48 +01:00
|
|
|
|
|
|
|
return vsprintf($translation_value, $args);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
/**
|
2015-02-08 18:55:48 +01:00
|
|
|
* Return the current language.
|
2014-07-23 15:52:50 +02:00
|
|
|
*/
|
|
|
|
public static function language() {
|
2015-02-08 18:55:48 +01:00
|
|
|
return self::$lang_name;
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for Minz_Translate::t()
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
function _t($key) {
|
|
|
|
$args = func_get_args();
|
|
|
|
unset($args[0]);
|
|
|
|
array_unshift($args, $key);
|
|
|
|
|
2014-09-21 12:12:35 +02:00
|
|
|
return call_user_func_array('Minz_Translate::t', $args);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|