Jeremy Newman : add translation methods to html class
Jeremy Newman
jnewman at wine.codeweavers.com
Tue Jan 19 12:45:30 CST 2016
Module: website
Branch: master
Commit: 7cd0f5d3599658bd4cae44f20fd18ce624af4117
URL: http://source.winehq.org/git/website.git/?a=commit;h=7cd0f5d3599658bd4cae44f20fd18ce624af4117
Author: Jeremy Newman <jnewman at codeweavers.com>
Date: Tue Jan 19 10:36:14 2016 -0600
add translation methods to html class
---
include/html.php | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 147 insertions(+)
diff --git a/include/html.php b/include/html.php
index df4bb4f..439cb96 100644
--- a/include/html.php
+++ b/include/html.php
@@ -1267,6 +1267,153 @@ class html
return $str;
}
+ // LOAD_TRANS (load a new translation table)
+ // $table - table to load
+ // $lang - language override
+ public function load_trans ($table, $lang = null)
+ {
+ // set language
+ if (empty($lang))
+ $lang = $this->lang;
+
+ // if table is loaded already, return
+ if (isset($this->trans_table[$lang][$table]))
+ return;
+
+ // init XMLToArray
+ check_and_require("XMLToArray");
+ $xml = new XMLToArray();
+
+ // load table XML (fallback to english)
+ if (file_exists("{$this->_file_root}/templates/{$lang}/global/xml/trans/{$table}.xml"))
+ $xml->filename = "{$this->_file_root}/templates/{$lang}/global/xml/trans/{$table}.xml";
+ else if ($lang != "en" and file_exists("{$this->_file_root}/templates/en/global/xml/trans/{$table}.xml"))
+ $xml->filename = "{$this->_file_root}/templates/en/global/xml/trans/{$table}.xml";
+ else
+ trigger_error("Unable to load translation XML", E_USER_ERROR);
+
+ // parse XML into array
+ $data = $xml->parse();
+
+ // parse the translation table
+ $this->trans_table[$lang][$table] = $this->parse_trans_array($data['trans']['#']['str']);
+
+ // debug load trans
+ debug("func", "html:load_trans() -> table[{$lang}:{$table}]");
+ if ($GLOBALS['config']->web_debug and method_exists($GLOBALS['login'], 'checkpriv')
+ and $GLOBALS['login']->checkpriv($GLOBALS['login']->id, "debug_trans"))
+ crap("trans: templates/{$lang}/global/xml/trans/{$table}.xml");
+
+ // done
+ return 1;
+ }
+
+ // PARSE TRANS ARRAY (nested translation array parser)
+ // $data - XML string array
+ private function parse_trans_array ($data)
+ {
+ // array for storage
+ $arr = array();
+ // loop and add strings
+ foreach ($data as $in)
+ {
+ if (is_array($in['#']))
+ {
+ // nested elements, continue parsing tree
+ $arr[$in['@']['name']] = $this->parse_trans_array($in['#']['str']);
+ }
+ else
+ {
+ // single element, return it
+ $arr[$in['@']['name']] = $in['#'];
+ }
+ }
+ // return final array
+ return $arr;
+ }
+
+ // UNLOAD_TRANS (clear a translation table from memory)
+ // $table - table to unload
+ public function unload_trans ($table)
+ {
+ debug("func", "html:unload_trans() -> table[{$table}]");
+ unset($this->trans_table[$this->lang][$table]);
+ return true;
+ }
+
+ // TRANS (return a processed string from the translastion table)
+ // $table - table to load string from
+ // $str - name of str to load
+ // $vars - array of vars to load into string
+ public function trans ($table = "global", $str = "", $vars = array())
+ {
+ // language override
+ if (preg_match('/:/', $table))
+ list($lang, $table) = preg_split('/:/', $table, 2);
+ else
+ $lang = $this->lang;
+
+ debug("func", "html:trans() -> table[{$lang}:{$table}] str[{$str}]");
+ $this->load_trans($table, ($lang == $this->lang ? null : $lang));
+ $out = $this->trans_table[$lang][$table][$str];
+ if (count($vars) > 0)
+ {
+ foreach ($vars as $x => $val)
+ {
+ $out = str_replace('{$'.$x.'}', $val, $out);
+ }
+ }
+ return $out;
+ }
+
+ // TRANS VAL (return a single value of a nested trans array)
+ // $table - table to load string from
+ // $str - name of str array
+ // $val - value to return from array
+ // $vars = array of vars to load into string
+ public function trans_val ($table, $str, $val, $vars = array())
+ {
+ // language override
+ if (preg_match('/:/', $table))
+ list($lang, $table) = preg_split('/:/', $table, 2);
+ else
+ $lang = $this->lang;
+
+ debug("func", "html:trans_val() -> table[{$table}] str[{$str}] val[{$val}]");
+ $this->load_trans($table, ($lang == $this->lang ? null : $lang));
+ if (is_array($this->trans_table[$lang][$table][$str]) and $this->trans_table[$lang][$table][$str][$val])
+ {
+ $out = $this->trans_table[$lang][$table][$str][$val];
+ if (count($vars) > 0)
+ {
+ foreach ($vars as $x => $val)
+ {
+ $out = str_replace('{$'.$x.'}', $val, $out);
+ }
+ }
+ return $out;
+ }
+ return "";
+ }
+
+ // GET TRANS (return a single element of trans array, no further processing, used for translation arrays)
+ // $table - table to load string from
+ // $str - name of str (array) to return
+ public function get_trans ($table, $str)
+ {
+ debug("func", "html:get_trans() -> table[{$table}] str[{$str}]");
+ $this->load_trans($table);
+ return $this->trans_table[$this->lang][$table][$str];
+ }
+
+ // GET_TRANS_TABLE (return an entire trans table, also load if not loaded)
+ // $table - trans table
+ public function get_trans_table ($table)
+ {
+ $this->load_trans($table);
+ return $this->trans_table[$this->lang][$table];
+ }
+
// TEMPLATE (read a template file and fill in the variables)
public function template ($theme = null, $template, $vars = null, $noremovetags = 0)
{
More information about the wine-cvs
mailing list