source: branches/version-2_12-multilang/data/class/helper/SC_Helper_Locale.php @ 22731

Revision 22731, 10.1 KB checked in by m_uehara, 11 years ago (diff)

#2190 ・定数の存在チェックを変更
・プラグイン情報取得処理の変更

Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24require DATA_REALDIR . 'module/Locale/streams.php';
25require DATA_REALDIR . 'module/Locale/gettext.php';
26
27/**
28 * Helper class for localization.
29 * Library of static method.
30 *
31 * @package Helper
32 * @author LOCKON CO.,LTD.
33 * @version $Id$
34 */
35class SC_Helper_Locale {
36
37    /**
38     * Store the instance of SC_Helper_Locale_Ex.
39     * @var SC_Helper_Locale
40     */
41    static $_instance = NULL;
42
43    public $_translations = array();
44
45     /**
46     * Return a string which corresponding with message alias.
47     *
48     * @param   string  $string     message alias
49     * @param   array   $options    options
50     * @return  string  a string corresponding with message alias
51     */
52    public static function get_locale($string, &$options) {
53        is_null(SC_Helper_Locale_Ex::$_instance) and SC_Helper_Locale_Ex::$_instance = new SC_Helper_Locale_Ex();
54
55        // If language code is not specified, use site default.
56        if (empty($options['lang_code'])) {
57            $lang_code = $options['lang_code'] = defined('LANG_CODE') ? LANG_CODE : 'en-US';
58        } else {
59            $lang_code = $options['lang_code'];
60        }
61        // If device type ID is not specified, detect the viewing device.
62        if (!isset($options['device_type_id']) || ($options['device_type_id'] !== FALSE && !strlen($options['device_type_id']))) {
63            if (method_exists('SC_Display_Ex', 'detectDevice')) {
64                $device_type_id = SC_Display_Ex::detectDevice();
65            } else {
66                $device_type_id = FALSE;
67            }
68        } else {
69            $device_type_id = $options['device_type_id'];
70        }
71
72        $return = $string;
73
74        // Get string list of specified language.
75        $translations = SC_Helper_Locale_Ex::$_instance->get_translations($lang_code, $device_type_id);
76        // Whether a string which corresponding with alias is exist.
77        if (isset($translations[$return])) {
78            $return = $translations[$return];
79        }
80
81        if (is_array($options['escape'])) {
82            foreach ($options['escape'] as $esc_type) {
83                $return = SC_Helper_Locale_Ex::escape($return, $esc_type);
84            }
85        }
86
87        return $return;
88    }
89
90    /**
91     * Return a string which corresponding with message alias.
92     *
93     * @param   string  $single     message alias (single)
94     * @param   string  $plural     message alias (plural)
95     * @param   array   $options    options
96     * @return  array
97     */
98    public static function get_locale_plural($single, $plural, &$options) {
99        // Plural strings are coupled with a null character.
100        $key = $single . chr(0) . $plural;
101        // Get a string of specified language which corresponds to the message alias.
102        $translated = SC_Helper_Locale_Ex::get_locale($key, $options);
103        // Divide with a null character.
104        return explode(chr(0), $translated);
105    }
106
107    /**
108     * Get the strings of specified language from locale files.
109     *
110     * @param   string  $lang_code      language code
111     * @param   integer $device_type_id device type ID
112     * @return  array   strings
113     */
114    function get_translations($lang_code, $device_type_id = FALSE) {
115        $translations_key = "translations_" . $lang_code . "_" . $device_type_id;
116        // If the strings of specified language is not loaded
117        if (empty($this->_translations[$translations_key])) {
118            $translations = array();
119
120            // Get a list of files to load.
121            $file_list = $this->get_locale_file_list($lang_code, $device_type_id);
122
123            // Get the strings from each locale file using php_gettext.
124            foreach ($file_list as $locale_file) {
125                $stream = new FileReader($locale_file);
126                $gettext = new gettext_reader($stream);
127
128                $gettext->load_tables();
129                $translations = array_merge($translations, $gettext->cache_translations);
130            }
131
132            $this->_translations[$translations_key] = $translations;
133        }
134
135        return $this->_translations[$translations_key];
136    }
137
138    /**
139     * Get a list of locale files.
140     *
141     * @param   string  $lang_code      language code
142     * @param   integer $device_type_id device type ID
143     * @return  array   file list
144     */
145    function get_locale_file_list($lang_code, $device_type_id = FALSE) {
146        $file_list = array();
147
148        // Path to the EC-CUBE Core locale file.
149        $core_locale_path = DATA_REALDIR . "locales/{$lang_code}.mo";
150        // If a locale file of specified language is exist, add to the file list.
151        if (file_exists($core_locale_path)) {
152            $file_list[] = $core_locale_path;
153        }
154
155        // Get a list of enabled plugins.
156        if (defined('ECCUBE_INSTALL')) {
157            $arrPluginDataList = SC_Plugin_Util_Ex::getAllPlugin();
158            // Get the plugins directory.
159            $arrPluginDirectory = SC_Plugin_Util_Ex::getPluginDirectory();
160            foreach ($arrPluginDataList as $arrPluginData) {
161                // Check that the plugin filename is contained in the list of plugins directory.
162                if (array_search($arrPluginData['plugin_code'], $arrPluginDirectory) !== false) {
163                    // Path to the plugin locale file.
164                    $plugin_locale_path = PLUGIN_UPLOAD_REALDIR . $arrPluginData['plugin_code'] . "/locales/{$lang_code}.mo";
165                    // If a locale file of specified language is exist, add to the file list.
166                    if (file_exists($plugin_locale_path)) {
167                        $file_list[] = $plugin_locale_path;
168                    }
169                }
170            }
171        }
172
173        // Path to the template locale file.
174        if ($device_type_id !== FALSE) {
175            $template_locale_path = HTML_REALDIR . SC_Helper_PageLayout_Ex::getUserDir($device_type_id, true) . "locales/{$lang_code}.mo";
176            // If a locale file of specified language is exist, add to the file list.
177            if (file_exists($template_locale_path)) {
178                $file_list[] = $template_locale_path;
179            }
180        }
181
182        return $file_list;
183    }
184
185    /**
186     * 文字列のエスケープを行う
187     *
188     * @param   string  $string     エスケープを行う文字列
189     * @param   string  $esc_type   エスケープの種類
190     * @return  string  エスケープを行った文字列
191     */
192    static function escape($string, $esc_type) {
193        $return = $string;
194
195        switch ($esc_type) {
196            case 'h':
197            case 'html':
198                $return = htmlspecialchars($return, ENT_QUOTES);
199                break;
200
201            case 'j':
202            case 'javascript':
203                // escape quotes and backslashes, newlines, etc.
204                $return = strtr($return, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
205                break;
206
207            case 'nl2br':
208                $return = nl2br($return);
209                break;
210
211            case '':
212            case 'none':
213                break;
214
215            case 'htmlall':
216                $return = htmlentities($return, ENT_QUOTES);
217                break;
218
219            case 'u':
220            case 'url':
221                $return = rawurlencode($return);
222                break;
223
224            case 'urlpathinfo':
225                $return = str_replace('%2F','/',rawurlencode($return));
226                break;
227
228            case 'quotes':
229                // escape unescaped single quotes
230                $return = preg_replace("%(?<!\\\\)'%", "\\'", $return);
231                break;
232
233            case 'hex':
234                // escape every character into hex
235                $text = '';
236                for ($x=0; $x < strlen($return); $x++) {
237                    $text .= '%' . bin2hex($return[$x]);
238                }
239                $return = $text;
240                break;
241
242            case 'hexentity':
243                $text = '';
244                for ($x=0; $x < strlen($return); $x++) {
245                    $text .= '&#x' . bin2hex($return[$x]) . ';';
246                }
247                $return = $text;
248                break;
249
250            case 'decentity':
251                $text = '';
252                for ($x=0; $x < strlen($return); $x++) {
253                    $text .= '&#' . ord($return[$x]) . ';';
254                }
255                $return = $text;
256                break;
257
258            case 'mail':
259                // safe way to display e-mail address on a web page
260                $return = str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $return);
261                break;
262
263            case 'nonstd':
264                // escape non-standard chars, such as ms document quotes
265                $_res = '';
266                for($_i = 0, $_len = strlen($return); $_i < $_len; $_i++) {
267                    $_ord = ord(substr($return, $_i, 1));
268                    // non-standard char, escape it
269                    if($_ord >= 126){
270                        $_res .= '&#' . $_ord . ';';
271                    }
272                    else {
273                        $_res .= substr($return, $_i, 1);
274                    }
275                }
276                $return = $_res;
277                break;
278
279            default:
280                trigger_error('unknown escape type. ' . var_export(func_get_args(), true), E_USER_WARNING);
281                break;
282        }
283
284        return $return;
285    }
286}
Note: See TracBrowser for help on using the repository browser.