From fea2eca9cb50ba0dd1058358bc0feb58304a3d80 Mon Sep 17 00:00:00 2001 From: Filip Komar Date: Sat, 1 Mar 2014 18:15:17 +0000 Subject: first step towards transition from lang files to gettext translation system --- langs.inc.php | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) (limited to 'langs.inc.php') diff --git a/langs.inc.php b/langs.inc.php index 59f89b910..a30e44a24 100644 --- a/langs.inc.php +++ b/langs.inc.php @@ -49,6 +49,8 @@ $domains_lang = array( 'mageia.ro' => 'ro', ); +require_once('langs/php-mo.php'); + /** * Redirect to a localized path, depending on incoming TLD. * Only manages redirections to main home path. @@ -434,3 +436,92 @@ function _e($s = null, $args = null) { return i18n::_e($s, $args); } function _h($s = null, $args = null, $tag = 'p') { return i18n::_h($s, $args, $tag); } function _lang_load($locale, $domain) { return i18n::_lang_load($locale, $domain); } + +/** + * Create dictionary from gettext file + * Return array. + * Do not exit the process. + * + * @param string $locale from which we want to create dictionary + * @param string $name_of_translation gettext filename + * + * @return array +*/ +function read_translation_file($locale, $name_of_translation) +{ + return phpmo_parse_po_file(G_APP_ROOT . '/langs/' . $locale . '/' . $name_of_translation .'.po'); +} + +/** + * Returns a translated string from global $dictionary + * it can append space if needed + * + * Note that it trims {ok} for translations equal to original too. + * + * Use it when you need to capture the string to output. + * + * Examples: + * echo _r("Hello!", ' ') . _r("How are you?") + * which should return translated: Hello! How are you? + * + * @param string $string_for_translation which we want to translate + * @param string $sufix append (usually space) + * + * @return string translated to current locale +*/ +function _r($string_for_translation, $sufix = '') +{ + global $dictionary; + $escapeded_string = str_replace(array('"'), array('\\"'), $string_for_translation); + if(!empty($dictionary[$escapeded_string]["msgstr"][0])) { + $find = array('\\"', '\n', ' ', '{ok}', '{OK}', '{Ok}', '{oK}'); + $replace = array('"','
', ' '); + $prepared_string = trim(str_replace($find, $replace, $dictionary[$escapeded_string]["msgstr"][0])); + } + if(empty($prepared_string)) { + $prepared_string = $string_for_translation; + } + if(!empty($sufix)) { + $prepared_string .= $sufix; + } + return $prepared_string; +} + +/** + * Higher level function for _r() to echo a translated string from global $dictionary + * used also to wrap the translation with HTML tags + * it can also append space if needed + * + * Examples: + *_g("How are you?") + * will just echo translation + * + * _g('Download Mageia %d!', array(5), 'a href="" style="color: blue;"') + * will echo blue link + * + * _g("Hey there.", null, ' '); _g("How are you?") + * will just echo translation: Hey there. How are you? + * + * Return boolean. + * Do not exit the process. + * + * @param string $string_for_translation which we want to translate + * @param array $args for vsprintf + * @param string $tag_or_space HTML tag or space to append + * + * @return null +*/ +function _g($string_for_translation, $args = null, $tag_or_space = '') +{ + $translated_string = _r($string_for_translation); + if(is_array($args)) { + $translated_string = vsprintf($translated_string, $args); + } + if(!empty($tag_or_space) && $tag_or_space != ' ') { + $tag_or_space_w_args = explode(' ', $tag_or_space); + $close_tag = array_shift($tag_or_space_w_args); + echo sprintf('<%s>%s', $tag_or_space, $translated_string, $close_tag); + } else { + echo $translated_string . $tag_or_space; + } +} -- cgit v1.2.1