aboutsummaryrefslogtreecommitdiffstats
path: root/langs.inc.php
diff options
context:
space:
mode:
authorFilip Komar <filip@mageia.org>2014-03-01 18:15:17 +0000
committerFilip Komar <filip@mageia.org>2014-03-01 18:15:17 +0000
commitfea2eca9cb50ba0dd1058358bc0feb58304a3d80 (patch)
tree14473e0a8ed3499e559ff4e957a5ecdbf42dfa3a /langs.inc.php
parent1f3db960405e08e0f707a5de5810c3c12040709d (diff)
downloadwww-fea2eca9cb50ba0dd1058358bc0feb58304a3d80.tar
www-fea2eca9cb50ba0dd1058358bc0feb58304a3d80.tar.gz
www-fea2eca9cb50ba0dd1058358bc0feb58304a3d80.tar.bz2
www-fea2eca9cb50ba0dd1058358bc0feb58304a3d80.tar.xz
www-fea2eca9cb50ba0dd1058358bc0feb58304a3d80.zip
first step towards transition from lang files to gettext translation system
Diffstat (limited to 'langs.inc.php')
-rw-r--r--langs.inc.php91
1 files changed, 91 insertions, 0 deletions
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('"','<br>', ' ');
+ $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</%s>', $tag_or_space, $translated_string, $close_tag);
+ } else {
+ echo $translated_string . $tag_or_space;
+ }
+}