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
tedkulp (author)
Sun Sep 23 08:53:32 -0700 2007
commit  ece5ea0630724e5c5381922e6d5f2b572123b48e
tree    15bc8079e1a63eedb49b04a683304ba7cf8bdf0d
parent  61673391b01d96acd6738038a775ee7f6e51208d
cmsmadesimple-2-0 / lib / classes / class.cms_language.php
100644 275 lines (242 sloc) 7.925 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
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
#CMS - CMS Made Simple
#(c)2004-2007 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($name, $params = array(), $module = 'core', $current_language = '', $default_language = 'en_US')
  {
    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, $default_language);
      
      if ($current_language != $default_language)
      {
        CmsLanguage::load_lang_file($module, $current_language);
      }
    }
 
    $result = null;
    
    if (array_key_exists($name, self::$lang[$module][$current_language]))
      $result = self::$lang[$module][$current_language][$name];
    else if ($default_language != $current_language && array_key_exists($name, self::$lang[$module][$default_language]))
      $result = self::$lang[$module][$default_language][$name];
    else
      {
        //$result = "--Add Me - $module - $name --";
        // calguy1000 - a move to the tr plugin
        // just return the original text
        return $name;
      }
      
    if (count($params) > 0)
    {
      $result = vsprintf($result, $params);
    }
    
    return $result;
  }
  
  /**
   * 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)
  {
    $lang = array();
 
    $file = cms_join_path(ROOT_DIR, 'lang', $module . '.' . $language . '.php');
    if (!is_file($file))
    {
      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) && 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"];
      }
      
      //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
?>