00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010 class Translator {
00011
00012
00013
00014 private static $inst = null;
00015
00016
00017
00018
00019 private $lang;
00020
00021
00022
00023
00024 public $groups = array();
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 public static function Instance() {
00036 if (empty(self::$inst)) {
00037 self::$inst = new Translator();
00038 }
00039 return self::$inst;
00040 }
00041
00042
00043
00044
00045 public function __construct($lang = false) {
00046 $this->lang = ($lang) ? $lang : GyroLocale::get_language();
00047 }
00048
00049
00050
00051
00052 public function translate($text, $groupname = 'default', $params = NULL) {
00053 $groupname = Arr::force($groupname, false);
00054 $arr_groupnames = array();
00055 foreach($groupname as $g) {
00056 if ($g !== 'app' && $g !== 'default') {
00057 $arr_groupnames[] = $g. '.overloads';
00058 }
00059 $arr_groupnames[] = $g;
00060 }
00061
00062
00063 if (count($arr_groupnames) == 0) {
00064 $arr_groupnames[] = 'default';
00065 }
00066
00067 $ret = false;
00068 $arr_groupnames[] = 'global';
00069 foreach($arr_groupnames as $current_groupname) {
00070 $group = $this->get_group($current_groupname);
00071 $ret = $this->get_translation($group, $text, $this->lang);
00072
00073 if ($ret !== false) {
00074 break;
00075 }
00076 }
00077
00078 if ($ret === false) {
00079 if (Config::has_feature(Config::LOG_TRANSLATIONS)) {
00080 $data = array(
00081 'groupname' => implode('|', $groupname),
00082 'groups' => implode('|', $arr_groupnames),
00083 'lang' => $this->lang,
00084 'translation' => $text
00085 );
00086 Load::components('logger');
00087 Logger::log('translations', $data);
00088 }
00089 $ret = $text;
00090 }
00091
00092 $ret = $this->apply_params($ret, $params);
00093 return $ret;
00094 }
00095
00096
00097
00098
00099 private function apply_params($text, $params) {
00100 if (is_array($params)) {
00101 $text = str_replace(array_keys($params), array_values($params), $text);
00102 }
00103 return $text;
00104 }
00105
00106
00107
00108
00109 private function get_group($group) {
00110 if (!isset($this->groups[$group])) {
00111 $ret = $this->load_group($group);
00112 $this->groups[$group] = $ret;
00113 }
00114 else {
00115 $ret = $this->groups[$group];
00116 }
00117 return $ret;
00118 }
00119
00120
00121
00122
00123
00124
00125 protected function load_group($group) {
00126 $ret = array();
00127 if (Load::classes_in_directory('view/translations/', $group, 'translations', false)) {
00128 $func = String::plain_ascii($group, '_') . '_load_translations';
00129 if (function_exists($func)) {
00130 $params = array($this->lang);
00131 $ret = $func($params);
00132 }
00133 }
00134
00135 return $ret;
00136 }
00137
00138
00139
00140
00141 protected function get_translation($group, $key, $lang) {
00142 if (isset($group[$key])) {
00143 return Arr::get_item($group[$key], $lang, false);
00144 }
00145 return false;
00146 }
00147 }
00148
00149
00150
00151
00152 function tr($text, $group = '', $params = NULL) {
00153 return Translator::Instance()->translate($text, $group, $params);
00154 }