source: branches/version-2_12-multilang/data/class/SC_I18n.php @ 22045

Revision 22045, 4.1 KB checked in by pineray, 12 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
24class SC_I18n {
25
26    /**
27     * Store the instance of SC_Helper_Locale_Ex.
28     * @var object
29     */
30    static $_instance = NULL;
31
32    /**
33     * Translate a message alias.
34     *
35     * @param   string  $string     message alias
36     * @param   array   $tokens     parameters for translation
37     * @param   array   $options    options
38     * @return  string  message to display
39     */
40    public static function t($string, $tokens = array(), $options = array()) {
41        is_null(SC_I18n_Ex::$_instance) and SC_I18n_Ex::$_instance = new SC_Helper_Locale_Ex();
42        $helper = SC_I18n_Ex::$_instance;
43
44        // If language code is not specified, use site default.
45        if (empty($options['lang_code'])) {
46            $options['lang_code'] = LANG_CODE;
47        }
48        // If device type ID is not specified, detect the viewing device.
49        if (!isset($options['device_type_id']) || ($options['device_type_id'] !== FALSE && !strlen($options['device_type_id']))) {
50            $options['device_type_id'] = SC_Display_Ex::detectDevice();
51        }
52
53        // Get a string of specified language which corresponds to the message alias.
54        $translated = $helper->get_locale($string, $options['lang_code'], $options['device_type_id']);
55
56        // If parameters are set, translate a message.
57        if (empty($tokens)) {
58          return $translated;
59        }
60        else {
61          return strtr($translated, $tokens);
62        }
63    }
64
65    /**
66     * Translate a message alias (plural).
67     *
68     * @param   integer $count      number for detecting format
69     * @param   string  $single     message alias (single)
70     * @param   string  $plural     message alias (plural)
71     * @param   array   $tokens     parameters for translation
72     * @param   array   $options    options
73     * @return  string  message to display
74     */
75    public static function t_plural($count, $single, $plural, $tokens = array(), $options = array()) {
76        is_null(SC_I18n_Ex::$_instance) and SC_I18n_Ex::$_instance = new SC_Helper_Locale_Ex();
77        $helper = SC_I18n_Ex::$_instance;
78
79        // Add a counter to translation parameters.
80        $tokens['T_COUNT'] = number_format($count);
81
82        // If language code is not specified, use site default.
83        if (empty($options['lang_code'])) {
84            $options['lang_code'] = LANG_CODE;
85        }
86        // If device type ID is not specified, detect the viewing device.
87        if (!isset($options['device_type_id']) || ($options['device_type_id'] !== FALSE && !strlen($options['device_type_id']))) {
88            $options['device_type_id'] = SC_Display_Ex::detectDevice();
89        }
90
91        // Determine appropriate plural form.
92        $index = $helper->get_plural_index($count, $options['lang_code']);
93
94        // Plural strings are coupled with a null character.
95        $key = $single . chr(0) . $plural;
96        // Get a string of specified language which corresponds to the message alias.
97        $translated = $helper->get_locale($key, $options['lang_code'], $options['device_type_id']);
98        // Divide with a null character.
99        $list = explode(chr(0), $translated);
100
101        return strtr($list[$index], $tokens);
102    }
103}
Note: See TracBrowser for help on using the repository browser.