public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
cmsmadesimple-2-0 / lib / classes / class.cms_language.php
100644 302 lines (266 sloc) 8.7 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#$Id: class.cms_language.php 3807 2007-03-05 03:05:39Z wishy $
 
/**
* Methods for handling language and translation functions.
*
* @author Ted Kulp
* @since 2.0
* @version $Revision: 3807 $
* @modifiedby $LastChangedBy: wishy $
* @lastmodified $Date: 2007-03-04 20:05:39 -0700 (Sun, 04 Mar 2007) $
* @license GPL
**/
class CmsLanguage extends CmsObject
{
  private static $nls = null;
  private static $lang = array();
  private static $current_language = null;
 
  function __construct()
  {
    parent::__construct();
  }
  
  public static function translate($string, $params = array(), $module = 'core', $current_language = '')
  {
    if ($string == null || $string == '')
      return '';
 
    if (self::$nls == null)
    {
      self::$nls = CmsCache::get_instance()->call(array('CmsLanguage', 'load_nls_files'));
    }
    
    $current_language = $current_language != '' ? $current_language : CmsLanguage::get_current_language();
    
    if (!array_key_exists($module, self::$lang) || !array_key_exists($current_language, self::$lang[$module]))
    {
      CmsLanguage::load_lang_file($module, $current_language);
    }
 
    $result = null;
    
    if (array_key_exists($string, self::$lang[$module][$current_language]))
    {
      $result = self::$lang[$module][$current_language][$string];
    }
    else
    {
      //Send event here
      CmsEventOperations::send_event('Core', 'MissingTranslation', array('module' => $module, 'language' => $current_language, 'string' => $string, 'hash' => self::create_bt_hash(debug_backtrace())));
      $result = $string;
    }
 
    if (count($params) > 0)
    {
      $result = vsprintf($result, $params);
    }
    
    return $result;
  }
  
  public static function create_bt_hash($backtrace)
  {
    if (count($backtrace) > 1)
    {
      $num = 1;
      #if (($backtrace[$num]["function"] == '_' || $backtrace[$num]["function"] == '__' || $backtrace[$num]["function"] == 'call_user_func_array') && count($backtrace) > 2)
      #  $num = 2;
      #print_r($string, $backtrace[$num]["line"] . basename($backtrace[$num]["file"]) . $backtrace[$num]["function"] . '<br />');
      return md5($backtrace[$num]["line"] . basename($backtrace[$num]["file"]));
    }
    
    return '';
  }
  
  /**
   * Returns a list of all the registered languages in the system. This does not necessarily
   * mean that the language file actually exists in the system, but it will be tried on all
   * translation calls before reverting back to the default language.
   *
   * @param boolean If you want to include the english translation of the language in the value
   * @return array Key/Value pairs of language id (en_US) and native names (English)
   * @author Ted Kulp
   **/
  public static function get_language_list($show_english = false)
  {
    if (self::$nls == null)
    {
      self::$nls = CmsCache::get_instance()->call(array('CmsLanguage', 'load_nls_files'));
    }
    
    $result = array();
    
    //asort(self::$nls['language']);
    
    foreach (self::$nls["language"] as $key=>$val)
    {
      $result[$key] = $val . (isset(self::$nls["englishlang"][$key]) && $show_english ? ' ('.self::$nls["englishlang"][$key].')' : '');
    }
    
    return $result;
  }
  
  public static function get_flag_image($language)
  {
    if (self::$nls == null)
    {
      self::$nls = CmsCache::get_instance()->call(array('CmsLanguage', 'load_nls_files'));
    }
    
    if (isset(self::$nls['flag_image'][$language]))
    {
      return 'images/lang/' . self::$nls['flag_image'][$language];
    }
    else
    {
      return '';
    }
  }
  
  private static function load_lang_file($module, $language)
  {
    $file = cms_join_path(ROOT_DIR, 'tmp', 'translations', $module . '.' . $language . '.xml');
    if (is_file($file))
    {
      //New parsing code
      $xml = simplexml_load_file($file);
      foreach ($xml as $one_entry)
      {
        self::$lang[$module][$language][(string)$one_entry->st] = (string)$one_entry->tr;
      }
    }
    else
    {
      $lang = array();
 
      if ($module == 'core')
      {
       $file = cms_join_path(ROOT_DIR, 'admin', 'lang', 'ext', $language, 'admin.inc.php');
        if (!is_file($file))
        {
          $file = cms_join_path(ROOT_DIR, 'admin', 'lang', $language, 'admin.inc.php');
        }
      }
      else
      {
       $file = cms_join_path(ROOT_DIR, 'modules', $module, 'lang', 'ext', $language . '.php');
        if (!is_file($file))
        {
          $file = cms_join_path(ROOT_DIR, 'modules', $module, 'lang', $language . '.php');
          if (!is_file($file))
          {
            $file = cms_join_path(ROOT_DIR, 'modules', $module, 'lang', 'ext', $language . '.php');            
          }
        }
      }
      
     if (is_file($file) && strlen($language) == 5 && strpos($language, ".") === false)
     {
        CmsProfiler::get_instance()->mark('Load:' . $file);
     include ($file);
        if (isset($lang['admin']) && is_array($lang['admin']) && count($lang['admin'] > 1))
        {
          $lang = $lang['admin'];
        }
     }
 
      self::$lang[$module][$language] =& $lang;
    }
  }
  
  public static function load_nls_files()
  {
    $nls = array();
    
    $xml = simplexml_load_file(cms_join_path(ROOT_DIR, 'tmp', 'translations', 'languages.xml'));
    
    foreach ($xml as $onelang)
    {
      $code = (string)$onelang->code;
      $nls['language'][$code] = (string)$onelang->native_name;
      $nls['englishlang'][$code] = (string)$onelang->english_name;
      $nls['flag_image'][$code] = (string)$onelang->flag_image;
      
      foreach ($onelang->aliases as $onealias)
      {
        foreach ($onealias->alias as $aliascode)
        {
          $nls['alias'][(string)$aliascode] = $code;  
        }
      }
    }
    
    return $nls;
  }
  
  private static function get_current_language()
  {
    #Check to see if there is already a language in use...
    if (self::$current_language == null)
    {
      $current_language = '';
    
      //See if it's in a cookie or posted.
      if (isset($_POST["default_cms_lang"]))
      {
        $current_language = $_POST["default_cms_lang"];
        if ($current_language == '')
        {
          setcookie("cms_language", '', time() - 3600);
        }
        else
        {
          setcookie("cms_language", $_POST["change_cms_lang"]);
        }
      }
      else if (isset($_SESSION['login_cms_language']))
      {
        debug_buffer('Setting language to: ' . $_SESSION['login_cms_language']);
        $current_language = $_SESSION['login_cms_language'];
        setcookie('cms_language', $_SESSION['login_cms_language']);
        unset($_SESSION['login_cms_language']);
      }
      else if (isset($_COOKIE["cms_language"]))
      {
        $current_language = $_COOKIE["cms_language"];
      }
      
      if ($current_language == '0')
        $current_language = '';
      
      //Anything yet?
      if ($current_language == '')
      {
        //Do we have a default locale in the config file?
        $config = cms_config();
        if (isset($config['locale']) && $config['locale'] != '')
        {
          $current_language = $config['locale'];
        }
        else
        {
          //No, take a stab at figuring out the default language...
          //Figure out default language and set it if it exists
          if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
          {
            $alllang = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
            if (strpos($alllang, ";") !== FALSE)
              $alllang = substr($alllang,0,strpos($alllang, ";"));
            $langs = explode(",", $alllang);
 
            foreach ($langs as $onelang)
            {
              #Check to see if lang exists...
              if (isset(self::$nls['language'][$onelang]))
              {
                $current_language = $onelang;
                setcookie("cms_language", $onelang);
                break;
              }
              #Check to see if alias exists...
              if (isset(self::$nls['alias'][$onelang]))
              {
                $alias = self::$nls['alias'][$onelang];
                if (isset(self::$nls['language'][$alias]))
                {
                  $current_language = $alias;
                  setcookie("cms_language", $alias);
                  break;
                }
              }
            }
          }
        }
      }
      
      self::$current_language = ($current_language != '' ? $current_language : 'en_US');
    }
    
    return self::$current_language;
  }
}
 
# vim:ts=4 sw=4 noet
?>