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

Revision 22085, 6.8 KB checked in by pineray, 10 years ago (diff)

#163 テキスト出力多言語対応

文字列のキーが英文となったことをうけた変更.

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     public $_translations = array();
38
39     public $_plural_forms = array();
40
41     /**
42     * Return a string which corresponding with message alias.
43     *
44     * @param   string  $string             message alias
45     * @param   string  $lang_code          language code
46     * @param   integer $device_type_id     device type ID
47     * @return  string  a string corresponding with message alias
48     */
49    function get_locale($string, $lang_code = FALSE, $device_type_id = FALSE) {
50        // Set language code when it is not set.
51        if ($lang_code === FALSE) {
52            if (defined(LANG_CODE)) {
53                $lang_code = LANG_CODE;
54            } else {
55                $lang_code = 'en';
56            }
57        }
58        // Get string list of specified language.
59        if ($lang_code == 'en') {
60            return $string;
61        } else {
62            $translations = $this->get_translations($lang_code, $device_type_id);
63            // Whether a string which corresponding with alias is exist.
64            if (isset($translations[$string])) {
65                return $translations[$string];
66            }
67            else {
68                return $string;
69            }
70        }
71    }
72
73    /**
74     * Get the strings of specified language from locale files.
75     *
76     * @param   string  $lang_code      language code
77     * @param   integer $device_type_id device type ID
78     * @return  array   strings
79     */
80    function get_translations($lang_code, $device_type_id = FALSE) {
81        $translations_key = "translations_" . $lang_code . "_" . $device_type_id;
82        // If the strings of specified language is not loaded
83        if (empty($this->_translations[$translations_key])) {
84            $translations = array();
85
86            // Get a list of files to load.
87            $file_list = $this->get_locale_file_list($lang_code, $device_type_id);
88
89            // Get the strings from each locale file using php_gettext.
90            foreach ($file_list as $locale_file) {
91                $stream = new FileReader($locale_file);
92                $gettext = new gettext_reader($stream);
93
94                $gettext->load_tables();
95                $translations = array_merge($translations, $gettext->cache_translations);
96            }
97
98            $this->_translations[$translations_key] = $translations;
99        }
100
101        return $this->_translations[$translations_key];
102    }
103
104    /**
105     * Get a list of locale files.
106     *
107     * @param   string  $lang_code      language code
108     * @param   integer $device_type_id device type ID
109     * @return  array   file list
110     */
111    function get_locale_file_list($lang_code, $device_type_id = FALSE) {
112        $file_list = array();
113
114        // Path to the EC-CUBE Core locale file.
115        $core_locale_path = DATA_REALDIR . "locales/{$lang_code}.mo";
116        // If a locale file of specified language is exist, add to the file list.
117        if (file_exists($core_locale_path)) {
118            $file_list[] = $core_locale_path;
119        }
120
121        // Get a list of enabled plugins.
122        if (defined(PLUGIN_UPLOAD_REALDIR)) {
123            $arrPluginDataList = SC_Plugin_Util_Ex::getEnablePlugin();
124            // Get the plugins directory.
125            $arrPluginDirectory = SC_Plugin_Util_Ex::getPluginDirectory();
126            foreach ($arrPluginDataList as $arrPluginData) {
127                // Check that the plugin filename is contained in the list of plugins directory.
128                if (array_search($arrPluginData['plugin_code'], $arrPluginDirectory) !== false) {
129                    // Path to the plugin locale file.
130                    $plugin_locale_path = PLUGIN_UPLOAD_REALDIR . $arrPluginData['plugin_code'] . "/locales/{$lang_code}.mo";
131                    // If a locale file of specified language is exist, add to the file list.
132                    if (file_exists($plugin_locale_path)) {
133                        $file_list[] = $plugin_locale_path;
134                    }
135                }
136            }
137        }
138
139        // Path to the template locale file.
140        if ($device_type_id !== FALSE) {
141            $template_locale_path = HTML_REALDIR . SC_Helper_PageLayout_Ex::getUserDir($device_type_id, true) . "locales/{$lang_code}.mo";
142            // If a locale file of specified language is exist, add to the file list.
143            if (file_exists($template_locale_path)) {
144                $file_list[] = $template_locale_path;
145            }
146        }
147
148        return $file_list;
149    }
150
151    /**
152     * Determine appropriate plural form.
153     *
154     * @param integer   $count      counter
155     * @param string    $lang_code  language code
156     * @return integer  index
157     */
158    function get_plural_index($count, $lang_code = FALSE) {
159        // Set language code when it is not set.
160        if ($lang_code === FALSE) {
161            if (defined(LANG_CODE)) {
162                $lang_code = LANG_CODE;
163            } else {
164                $lang_code = 'en';
165            }
166        }
167        // Get a formula
168        $string = $this->get_plural_forms($lang_code);
169        $string = str_replace('nplurals', "\$total", $string);
170        $string = str_replace("n", $count, $string);
171        $string = str_replace('plural', "\$plural", $string);
172
173        $total = 0;
174        $plural = 0;
175
176        eval("$string");
177        if ($plural >= $total) $plural = $total - 1;
178
179        return $plural;
180    }
181
182    /**
183     * Get a formula to determine appropriate plural form.
184     *
185     * @param   string  $lang_code  language code
186     * @return  string  formula
187     */
188    function get_plural_forms($lang_code) {
189        // If formula is empty, include the file.
190        if(empty($this->_plural_forms)){
191            $this->_plural_forms = @include_once DATA_REALDIR . "include/plural_forms.inc";
192        }
193
194        return $this->_plural_forms[$lang_code];
195    }
196}
Note: See TracBrowser for help on using the repository browser.