pluf2

pluf2 Git Source Tree


Root/src/Pluf/L10n.php

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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Localization class.
 *
 * The localization of the code is performed using the __() function call.
 * This function is directly available in the Pluf.php file.
 *
 * The Pluf_L10n class is used to load the localization strings in memory
 * in the $GLOBALS['_PX_locale'] array. All the strings are stored in utf-8
 * as all the applications created with the Plume Framework must use the utf-8
 * encoding.
 * The Pluf locale files are in the Pluf/locale/ folder.
 *
 * The locale files can be optimized and an optimized version of the files
 * stored in the Pluf temp folder. The temp folder is defined in the global
 * configuration as 'tmp_folder'.
 *
 */
class Pluf_L10n
{
    /**
     * Folder in which the locale file are available.
     */
    public $locale_folder = '';
 
    /**
     * Constructor.
     *
     * See loadDomain(). If no folder is provided, the default Pluf/locale
     * folder is used to load the locales from.
     *
     * @param string Locale folder without trailing slash ('')
     * @param string Language ('en')
     * @param string Domain ('pluf')
     */
    function __construct($folder='', $lang='en', $domain='pluf')
    {
        if ('' == $folder) {
            $this->locale_folder = dirname(__FILE__).'/locale';
        }
        $this->loadDomain($lang, $domain);
    }
     
    /**
     * Load a domain file.
     * A domain file is a .lang file in the main locale folder of plume.
     *
     * @param string Language ('en')
     * @param string Domain, without the .lang ('pluf')
     * @return bool Success
     */
    function loadDomain($lang='en', $domain='pluf')
    {
        if ('en' == $lang) {
            return true;
        }
        return $this->loadFile($this->locale_folder.$lang.'/'.$domain.'.lang');
    }
 
    /**
     * Load a locale file
     *
     * @param string Complete path to the locale file
     * @return bool Success
     */
    function loadFile($file)
    {
        if (!empty($GLOBALS['_PX_locale_files'][$file])) {
            return true;
        }
        if (!file_exists($file)) {
            return false;
        }
        if (!isset($GLOBALS['_PX_locale'])) {
            $GLOBALS['_PX_locale'] = array();
        }
        // Load optimized file if available
        if ('' != Pluf::f('tmp_folder')) {
            $phpfile = Pluf::f('tmp_folder').'/Pluf_L10n-'
                .str_replace(DIRECTORY_SEPARATOR, '_', substr($file, 0, -5))
                .'.php';
            if (file_exists($phpfile)
                && (@filemtime($file) < @filemtime($phpfile))) {
                $l = include $phpfile;
                $GLOBALS['_PX_locale'] = array_merge($GLOBALS['_PX_locale'], $l);
                $GLOBALS['_PX_locale_files'][$file] = 'optimized';
                return true;
            }
        }
        $lines = file($file);
        $count = count($lines);
        for ($i=1; $i<$count; $i++) {
            $tmp = (!empty($lines[$i+1])) ? trim($lines[$i+1]) : '';
            if (!empty($tmp) && ';' == substr($lines[$i],0,1)) {
                $GLOBALS['_PX_locale'][trim(substr($lines[$i],1))] = $tmp;
                $i++;
            }
        }
        $GLOBALS['_PX_locale_files'][$file] = true;
        return true;
    }
     
    /**
     * Optimize a locale. Convert the .lang in a .php file
     * ready to be included. The optimized file is encoded
     * with the current encoding.
     *
     * @param string Locale file to optimize
     * @return bool Success
     */
    function optimizeLocale($file)
    {
        if (!file_exists($file)) {
            return false;
        }
        $phpfile = Pluf::f('tmp_folder').'/Pluf_L10n-'
            .str_replace(DIRECTORY_SEPARATOR, '_', substr($file, 0, -5))
            .'.php';
        $lines = file($file);
        if (false === ($fp = @fopen($phpfile,'w'))) {
            return false;
        }
        fputs($fp, '<?php '."\n".'/* automatically generated file from: '
              .$file.'  */'."\n\n");
        fputs($fp, '$l = array();'."\n");
        $count = count($lines);
        for ($i=1; $i<$count; $i++) {
            $tmp = (!empty($lines[$i+1])) ? trim($lines[$i+1]) : '';
            if (!empty($tmp) && ';' == substr($lines[$i],0,1)) {
                $string = '$l[\''
                    .str_replace("'", "\\'", trim(substr($lines[$i],1)))
                    .'\'] = \''.str_replace("'", "\\'", $tmp).'\';'."\n";
                fputs($fp, $string);
                $i++;
            }
        }
        fputs($fp, 'return $l;'."\n");
        fputs($fp, "\n".'?>');
        @fclose($fp);
        @chmod($phpfile, 0777);
        return true;
    }
 
    /**
     * Get the available locales for a domain.
     *
     * @param string Domain ('')
     * @return array List of 2 letter iso codes
     */
    function getAvailableLocales($domain='')
    {
        $rootdir = $this->locale_folder.'/';
        $locales = array();
        $locales[] = 'en'; //English is always available
        $current_dir = opendir($rootdir);
        if (!empty($domain)) {
            $domain .= '.lang';
        }
        while($entryname = readdir($current_dir)) {
            if (is_dir($rootdir.$entryname.'/')
                and ($entryname != '.' and $entryname!='..')
                and (2 == strlen($entryname))
                ) {
                $entryname = strtolower($entryname);
                if (empty($domain)) {
                    $locales[] = $entryname;
                } elseif (is_file($rootdir.$entryname.'/'.$domain)) {
                    $locales[] = $entryname;
                }
            }
        }
        closedir($current_dir);
        sort($locales);
        reset($locales);
        return $locales;
    }
 
    /**
     * Return the "best" accepted language from the list of available
     * languages.
     *
     * Use $_SERVER['HTTP_ACCEPT_LANGUAGE'] if the accepted language is empty
     *
     * @param array Available languages in the system
     * @param string String of comma separated accepted languages ('')
     * @return string Language 2 letter iso code, default is 'en'
     */
    function getAcceptedLanguage($available, $accepted ='')
    {
        if (empty($accepted)) {
            if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
                $accepted = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
            } else {
                return 'en';
            }
        }
        $acceptedlist = explode(',', $accepted);
        foreach ($acceptedlist as $lang) {
            //for the fr-FR en-US cases
            $lang = strtolower(substr($lang, 0, 2));
            if (in_array($lang, $available)) {
                return $lang;
            }
        }
        //no match found, English
        return 'en';
    }
 
    /**
     * Returns iso codes.
     *
     * @param bool Is the language the key in the array (false)
     * @return array The key is either the language or the iso code
     */
    function getIsoCodes($lang=false)
    {
        $res = array('aa' => 'Afar',
                     'ab' => 'Abkhazian',
                     'af' => 'Afrikaans',
                     'am' => 'Amharic',
                     'ar' => 'Arabic',
                     'as' => 'Assamese',
                     'ay' => 'Aymara',
                     'az' => 'Azerbaijani',
                     'ba' => 'Bashkir',
                     'be' => 'Byelorussian',
                     'bg' => 'Bulgarian',
                     'bh' => 'Bihari',
                     'bi' => 'Bislama',
                     'bn' => 'Bengali',
                     'bo' => 'Tibetan',
                     'br' => 'Breton',
                     'ca' => 'Catalan',
                     'co' => 'Corsican',
                     'cs' => 'Czech',
                     'cy' => 'Welsh',
                     'da' => 'Danish',
                     'de' => 'German',
                     'dz' => 'Bhutani',
                     'el' => 'Greek',
                     'en' => 'English',
                     'eo' => 'Esperanto',
                     'es' => 'Spanish',
                     'et' => 'Estonian',
                     'eu' => 'Basque',
                     'fa' => 'Persian',
                     'fi' => 'Finnish',
                     'fj' => 'Fiji',
                     'fo' => 'Faroese',
                     'fr' => 'French',
                     'fy' => 'Frisian',
                     'ga' => 'Irish',
                     'gd' => 'Scots gaelic',
                     'gl' => 'Galician',
                     'gn' => 'Guarani',
                     'gu' => 'Gujarati',
                     'ha' => 'Hausa',
                     'he' => 'Hebrew',
                     'hi' => 'Hindi',
                     'hr' => 'Croatian',
                     'hu' => 'Hungarian',
                     'hy' => 'Armenian',
                     'ia' => 'Interlingua',
                     'ie' => 'Interlingue',
                     'ik' => 'Inupiak',
                     'id' => 'Indonesian',
                     'is' => 'Icelandic',
                     'it' => 'Italian',
                     'iu' => 'Inuktitut',
                     'ja' => 'Japanese',
                     'jv' => 'Javanese',
                     'ka' => 'Georgian',
                     'kk' => 'Kazakh',
                     'kl' => 'Greenlandic',
                     'km' => 'Cambodian',
                     'kn' => 'Kannada',
                     'ko' => 'Korean',
                     'ks' => 'Kashmiri',
                     'ku' => 'Kurdish',
                     'ky' => 'Kirghiz',
                     'la' => 'Latin',
                     'ln' => 'Lingala',
                     'lo' => 'Laothian',
                     'lt' => 'Lithuanian',
                     'lv' => 'Latvian;lettish',
                     'mg' => 'Malagasy',
                     'mi' => 'Maori',
                     'mk' => 'Macedonian',
                     'ml' => 'Malayalam',
                     'mn' => 'Mongolian',
                     'mo' => 'Moldavian',
                     'mr' => 'Marathi',
                     'ms' => 'Malay',
                     'mt' => 'Maltese',
                     'my' => 'Burmese',
                     'na' => 'Nauru',
                     'ne' => 'Nepali',
                     'nl' => 'Dutch',
                     'no' => 'Norwegian',
                     'oc' => 'Occitan',
                     'om' => 'Afan (oromo)',
                     'or' => 'Oriya',
                     'pa' => 'Punjabi',
                     'pl' => 'Polish',
                     'ps' => 'Pashto;pushto',
                     'pt' => 'Portuguese',
                     'qu' => 'Quechua',
                     'rm' => 'Rhaeto-romance',
                     'rn' => 'Kurundi',
                     'ro' => 'Romanian',
                     'ru' => 'Russian',
                     'rw' => 'Kinyarwanda',
                     'sa' => 'Sanskrit',
                     'sd' => 'Sindhi',
                     'sg' => 'Sangho',
                     'sh' => 'Serbo-croatian',
                     'si' => 'Singhalese',
                     'sk' => 'Slovak',
                     'sl' => 'Slovenian',
                     'sm' => 'Samoan',
                     'sn' => 'Shona',
                     'so' => 'Somali',
                     'sq' => 'Albanian',
                     'sr' => 'Serbian',
                     'ss' => 'Siswati',
                     'st' => 'Sesotho',
                     'su' => 'Sundanese',
                     'sv' => 'Swedish',
                     'sw' => 'Swahili',
                     'ta' => 'Tamil',
                     'te' => 'Telugu',
                     'tg' => 'Tajik',
                     'th' => 'Thai',
                     'ti' => 'Tigrinya',
                     'tk' => 'Turkmen',
                     'tl' => 'Tagalog',
                     'tn' => 'Setswana',
                     'to' => 'Tonga',
                     'tr' => 'Turkish',
                     'ts' => 'Tsonga',
                     'tt' => 'Tatar',
                     'tw' => 'Twi',
                     'ug' => 'Uigur',
                     'uk' => 'Ukrainian',
                     'ur' => 'Urdu',
                     'uz' => 'Uzbek',
                     'vi' => 'Vietnamese',
                     'vo' => 'Volapuk',
                     'wo' => 'Wolof',
                     'xh' => 'Xhosa',
                     'yi' => 'Yiddish',
                     'yo' => 'Yoruba',
                     'za' => 'Zhuang',
                     'zh' => 'Chinese',
                     'zu' => 'Zulu');
        if ($lang) {
            $res = array_flip($res);
            ksort($res); //order by lang
        }
        return $res;
    }
 
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.09714s using 11 queries.