| 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 | /** |
|---|
| 25 | * @file |
|---|
| 26 | * Common functions that many EC-CUBE classes will need to reference. |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * Translate a message alias. |
|---|
| 31 | * |
|---|
| 32 | * @param string $string message alias |
|---|
| 33 | * @param array $tokens parameters for translation |
|---|
| 34 | * @param array $options options |
|---|
| 35 | * @return string message to display |
|---|
| 36 | */ |
|---|
| 37 | function t($string, $tokens = array(), $options = array()) { |
|---|
| 38 | if (method_exists('SC_Helper_Locale_Ex', 'get_locale')) { |
|---|
| 39 | // Get a string of specified language which corresponds to the message alias. |
|---|
| 40 | $translated = SC_Helper_Locale_Ex::get_locale($string, $options); |
|---|
| 41 | } else { |
|---|
| 42 | $translated = $string; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | // If parameters are set, translate a message. |
|---|
| 46 | if (empty($tokens)) { |
|---|
| 47 | return $translated; |
|---|
| 48 | } |
|---|
| 49 | else { |
|---|
| 50 | return strtr($translated, $tokens); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Translate a message alias (plural). |
|---|
| 56 | * |
|---|
| 57 | * @param integer $count number for detecting format |
|---|
| 58 | * @param string $single message alias (single) |
|---|
| 59 | * @param string $plural message alias (plural) |
|---|
| 60 | * @param array $tokens parameters for translation |
|---|
| 61 | * @param array $options options |
|---|
| 62 | * @return string message to display |
|---|
| 63 | */ |
|---|
| 64 | function t_plural($count, $single, $plural, $tokens = array(), $options = array()) { |
|---|
| 65 | if (method_exists('SC_Helper_Locale_Ex', 'get_locale_plural')) { |
|---|
| 66 | list($translated_single, $translated_plural) = SC_Helper_Locale_Ex::get_locale_plural($single, $plural, $options); |
|---|
| 67 | } else { |
|---|
| 68 | $translated_single = $single; |
|---|
| 69 | $translated_plural = $plural; |
|---|
| 70 | $options['lang_code'] = 'en-US'; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | if ($count == 1) { |
|---|
| 74 | $return = $translated_single; |
|---|
| 75 | } else { |
|---|
| 76 | // Determine appropriate plural form. |
|---|
| 77 | $index = get_plural_index($count, $options['lang_code']); |
|---|
| 78 | |
|---|
| 79 | if ($index < 0) { |
|---|
| 80 | $return = $translated_plural; |
|---|
| 81 | } else { |
|---|
| 82 | switch ($index) { |
|---|
| 83 | case "0": |
|---|
| 84 | $return = $translated_single; |
|---|
| 85 | case "1": |
|---|
| 86 | default: |
|---|
| 87 | $return = $translated_plural; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | // Add a counter to translation parameters. |
|---|
| 93 | $tokens['T_COUNT'] = number_format($count); |
|---|
| 94 | |
|---|
| 95 | return strtr($return, $tokens); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Determine appropriate plural form. |
|---|
| 100 | * |
|---|
| 101 | * @param integer $count counter |
|---|
| 102 | * @param string $lang_code language code |
|---|
| 103 | * @return integer index |
|---|
| 104 | */ |
|---|
| 105 | function get_plural_index($count, $lang_code = 'en-US') { |
|---|
| 106 | static $plural_indexes = array(); |
|---|
| 107 | |
|---|
| 108 | if (!isset($plural_indexes[$lang_code][$count])) { |
|---|
| 109 | // Get a formula |
|---|
| 110 | $formula = get_plural_formula($lang_code); |
|---|
| 111 | |
|---|
| 112 | // If there is a plural formula for the language, evaluate it |
|---|
| 113 | if (!empty($formula)) { |
|---|
| 114 | $string = str_replace('nplurals', "\$total", $formula); |
|---|
| 115 | $string = str_replace("n", $count, $string); |
|---|
| 116 | $string = str_replace('plural', "\$plural", $string); |
|---|
| 117 | |
|---|
| 118 | $total = 0; |
|---|
| 119 | $plural = 0; |
|---|
| 120 | |
|---|
| 121 | eval("$string"); |
|---|
| 122 | if ($plural >= $total) $plural = $total - 1; |
|---|
| 123 | |
|---|
| 124 | $plural_indexes[$lang_code][$count] = $plural; |
|---|
| 125 | // If there is no plural formula for English |
|---|
| 126 | } elseif ($lang_code == 'en-US') { |
|---|
| 127 | $plural_indexes[$lang_code][$count] = (int) ($count != 1); |
|---|
| 128 | // Otherwise, return -1 (unknown). |
|---|
| 129 | } else { |
|---|
| 130 | $plural_indexes[$lang_code][$count] = -1; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | return $plural_indexes[$lang_code][$count]; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /** |
|---|
| 138 | * Get a formula to determine appropriate plural form. |
|---|
| 139 | * |
|---|
| 140 | * @param string $lang_code language code |
|---|
| 141 | * @return string formula |
|---|
| 142 | */ |
|---|
| 143 | function get_plural_formula($lang_code) { |
|---|
| 144 | static $plural_formulas = array(); |
|---|
| 145 | |
|---|
| 146 | // If formula is empty, include the file. |
|---|
| 147 | if(empty($plural_formulas)){ |
|---|
| 148 | $plural_formulas = @include_once DATA_REALDIR . "include/plural_forms.inc"; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | return isset($plural_formulas[$lang_code]) ? $plural_formulas[$lang_code] : NULL; |
|---|
| 152 | } |
|---|