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 | |
---|
24 | require DATA_REALDIR . 'module/Locale/streams.php'; |
---|
25 | require DATA_REALDIR . 'module/Locale/gettext.php'; |
---|
26 | |
---|
27 | /** |
---|
28 | * ローカリゼーション関係のヘルパークラス. |
---|
29 | * 主に static 参照する関数群. |
---|
30 | * |
---|
31 | * @package Helper |
---|
32 | * @author LOCKON CO.,LTD. |
---|
33 | * @version $Id$ |
---|
34 | */ |
---|
35 | class SC_Helper_Locale { |
---|
36 | |
---|
37 | // }}} |
---|
38 | // {{{ functions |
---|
39 | |
---|
40 | /** |
---|
41 | * メッセージの一覧から、エイリアスに対応する文字列を返す. |
---|
42 | * |
---|
43 | * @param string $string メッセージエイリアス |
---|
44 | * @param string $lang_code 言語コード |
---|
45 | * @param integer $device_type_id 端末種別ID |
---|
46 | * @return string エイリアスに対応する文字列 |
---|
47 | */ |
---|
48 | function get_locale($string, $lang_code = LANG_CODE, $device_type_id = DEVICE_TYPE_PC) { |
---|
49 | // 指定言語の文字列一覧を取得する |
---|
50 | $translations = SC_Helper_Locale_Ex::get_translations($lang_code, $device_type_id); |
---|
51 | // エイリアスに対応する文字列が存在するかどうか |
---|
52 | if (isset($translations[$string])) { |
---|
53 | return $translations[$string]; |
---|
54 | } |
---|
55 | else { |
---|
56 | return $string; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * 指定言語の文字列一覧を取得する |
---|
62 | * |
---|
63 | * @param string $lang_code 言語コード |
---|
64 | * @param integer $device_type_id 端末種別ID |
---|
65 | * @return array 文字列一覧 |
---|
66 | */ |
---|
67 | function get_translations($lang_code = LANG_CODE, $device_type_id = DEVICE_TYPE_PC) { |
---|
68 | static $translations; |
---|
69 | |
---|
70 | // 指定言語の文字列がまだ読み込まれていない場合 |
---|
71 | if (empty($translations[$lang_code][$device_type_id])) { |
---|
72 | $translations[$lang_code][$device_type_id] = array(); |
---|
73 | |
---|
74 | // 読み込むロケールファイルの一覧を取得 |
---|
75 | $file_list = SC_Helper_Locale_Ex::get_locale_file_list($lang_code, $device_type_id); |
---|
76 | |
---|
77 | // ロケールファイル毎に、php_gettextを利用して文字列を取得する |
---|
78 | foreach ($file_list as $locale_file) { |
---|
79 | $stream = new FileReader($locale_file); |
---|
80 | $gettext = new gettext_reader($stream); |
---|
81 | |
---|
82 | $gettext->load_tables(); |
---|
83 | $translations[$lang_code][$device_type_id] = array_merge($translations[$lang_code][$device_type_id], $gettext->cache_translations); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | return $translations[$lang_code][$device_type_id]; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * ロケールファイルの一覧を取得する |
---|
92 | * |
---|
93 | * @param string $lang_code 言語コード |
---|
94 | * @param integer $device_type_id 端末種別ID |
---|
95 | * @return array ファイル一覧 |
---|
96 | */ |
---|
97 | function get_locale_file_list($lang_code = LANG_CODE, $device_type_id = DEVICE_TYPE_PC) { |
---|
98 | $file_list = array(); |
---|
99 | |
---|
100 | // コアのロケールファイルのパスを作成 |
---|
101 | $core_locale_path = DATA_REALDIR . "locales/$lang_code.mo"; |
---|
102 | // 指定言語のロケールファイルが存在すればファイル一覧に追加 |
---|
103 | if (file_exists($core_locale_path)) { |
---|
104 | $file_list[] = $core_locale_path; |
---|
105 | } |
---|
106 | |
---|
107 | // 有効なプラグインを取得 |
---|
108 | $arrPluginDataList = SC_Plugin_Util_Ex::getEnablePlugin(); |
---|
109 | // pluginディレクトリを取得 |
---|
110 | $arrPluginDirectory = SC_Plugin_Util_Ex::getPluginDirectory(); |
---|
111 | foreach ($arrPluginDataList as $arrPluginData) { |
---|
112 | // プラグイン本体ファイル名が取得したプラグインディレクトリ一覧にある事を確認 |
---|
113 | if (array_search($arrPluginData['plugin_code'], $arrPluginDirectory) !== false) { |
---|
114 | // プラグインのロケールファイルのパスを作成 |
---|
115 | $plugin_locale_path = PLUGIN_UPLOAD_REALDIR . $arrPluginData['plugin_code'] . "/locales/$lang_code.mo"; |
---|
116 | // 指定言語のロケールファイルが存在すればファイル一覧に追加 |
---|
117 | if (file_exists($plugin_locale_path)) { |
---|
118 | $file_list[] = $plugin_locale_path; |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | // テンプレートのロケールファイルのパスを作成 |
---|
124 | $template_locale_path = HTML_REALDIR . SC_Helper_PageLayout_Ex::getUserDir($device_type_id, true) . "locales/$lang_code.mo"; |
---|
125 | // 指定言語のロケールファイルが存在すればファイル一覧に追加 |
---|
126 | if (file_exists($template_locale_path)) { |
---|
127 | $file_list[] = $template_locale_path; |
---|
128 | } |
---|
129 | |
---|
130 | return $file_list; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * 適切な複数形の書式を判定する |
---|
135 | * |
---|
136 | * @param integer $count 数値 |
---|
137 | * @param string $lang_code 言語コード |
---|
138 | * @return integer インデックス |
---|
139 | */ |
---|
140 | function get_plural_index($count, $lang_code = LANG_CODE) { |
---|
141 | // 計算式を取得 |
---|
142 | $string = SC_Helper_Locale_Ex::get_plural_forms($lang_code); |
---|
143 | $string = str_replace('nplurals', "\$total", $string); |
---|
144 | $string = str_replace("n", $count, $string); |
---|
145 | $string = str_replace('plural', "\$plural", $string); |
---|
146 | |
---|
147 | $total = 0; |
---|
148 | $plural = 0; |
---|
149 | |
---|
150 | eval("$string"); |
---|
151 | if ($plural >= $total) $plural = $total - 1; |
---|
152 | |
---|
153 | return $plural; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * 適切な複数形の書式判定に使用する計算式を取得する |
---|
158 | * |
---|
159 | * @param string $lang_code 言語コード |
---|
160 | * @return string 計算式 |
---|
161 | */ |
---|
162 | function get_plural_forms($lang_code = LANG_CODE) { |
---|
163 | static $plural_forms; |
---|
164 | |
---|
165 | // 書式が読みこまれていない場合はファイルから一覧を読み込む |
---|
166 | if(empty($plural_forms)){ |
---|
167 | $plural_forms = @include_once DATA_REALDIR . "include/plural_forms.inc"; |
---|
168 | } |
---|
169 | |
---|
170 | // 書式が無い場合は、日本語用のものをデフォルトで返す |
---|
171 | return (isset($plural_forms[$lang_code])) ? $plural_forms[$lang_code] : "nplurals=2; plural=(n!=1);"; |
---|
172 | } |
---|
173 | } |
---|