| 97 | | ?> |
| 98 | | No newline at end of file |
| | 97 | /** |
| | 98 | * Finds the best key match for a bunch of locales |
| | 99 | * |
| | 100 | * Follows the freedesktop.org standart: |
| | 101 | * <a href="http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s04.html">Localized values for keys</a> |
| | 102 | * |
| | 103 | * @param mixed $keys_array each element must be an array, whose |
| | 104 | * first (not zero) element is the locale and its second is the key value |
| | 105 | * This format is suitable to come from for preg_match_all results |
| | 106 | * @param string $default_value the value to be used if a match is not found |
| | 107 | * @return string the most suitable localized value for the given locales |
| | 108 | */ |
| | 109 | function l10n_match_key($keys_array, $default_value) { |
| | 110 | $locale = l10n_parse_locale(get_locale(), true); |
| | 111 | if (false === $locale) { |
| | 112 | return $default_value; |
| | 113 | } |
| | 114 | $keys_dict = array(); |
| | 115 | foreach ($keys_array as $item) { |
| | 116 | $keys_dict[strtolower($item[1])] = $item[2]; |
| | 117 | } |
| | 118 | |
| | 119 | $lcm = strtolower(l10n_locale_parts($locale, 'lcm')); |
| | 120 | $lc = strtolower(l10n_locale_parts($locale, 'lc')); |
| | 121 | $lm = strtolower(l10n_locale_parts($locale, 'lm')); |
| | 122 | $l = strtolower(l10n_locale_parts($locale, 'l')); |
| | 123 | |
| | 124 | if (array_key_exists($lcm, $keys_dict)) { |
| | 125 | return $keys_dict[$lcm]; |
| | 126 | } elseif (array_key_exists($lc, $keys_dict)) { |
| | 127 | return $keys_dict[$lc]; |
| | 128 | } elseif (array_key_exists($lm, $keys_dict)) { |
| | 129 | return $keys_dict[$lm]; |
| | 130 | } elseif (array_key_exists($l, $keys_dict)) { |
| | 131 | return $keys_dict[$l]; |
| | 132 | } |
| | 133 | return $default_value; |
| | 134 | } |
| | 135 | |
| | 136 | /** |
| | 137 | * Parses a ln_CN.Encoding@Modifier string and returns hash with its parts |
| | 138 | * |
| | 139 | * @param string $locale the locale string |
| | 140 | * @param bool $strip_encoding whether to return empty encoding part |
| | 141 | * @return mixed hash with following keys: |
| | 142 | * lang, country, encoding, modifier |
| | 143 | */ |
| | 144 | function l10n_parse_locale($locale, $strip_encoding = false) { |
| | 145 | $locale_re = "^([a-z]{2})(?:_([a-z]{2}))?(?:\.([a-z0-9-]+))?(?:\@([a-z0-9-]+))?$"; |
| | 146 | if (preg_match("|$locale_re|i", $locale, $matches)) { |
| | 147 | if ($strip_encoding) { |
| | 148 | $matches[3] = ''; |
| | 149 | } |
| | 150 | return array('locale' => $matches[0], 'lang' => $matches[1], 'country' => $matches[2], |
| | 151 | 'encoding' => $matches[3], 'modifier' => $matches[4]); |
| | 152 | } else { |
| | 153 | return false; |
| | 154 | } |
| | 155 | } |
| | 156 | |
| | 157 | /** |
| | 158 | * Extracts parts of an already parsed locale |
| | 159 | * |
| | 160 | * @param mixed $parsed_locale locale string parsed with {@link ln10_parse_locale} |
| | 161 | * @param string $format some characters are replaced with their corresponding locale parts: |
| | 162 | * 'l' -> language |
| | 163 | * 'c' -> _country |
| | 164 | * 'e' -> .encoding |
| | 165 | * 'm' -> @modofier |
| | 166 | * @return string the parts defined by $format, joined together in s string |
| | 167 | * |
| | 168 | * @example <code>l10n_locale_parts(l10n_parse_locale('bg_BG@baba'), 'lem')</code> returns <code>bg@baba</code> |
| | 169 | */ |
| | 170 | function l10n_locale_parts($parsed_locale, $format) { |
| | 171 | $result = ''; |
| | 172 | $format = preg_split('//', $format, -1, PREG_SPLIT_NO_EMPTY); |
| | 173 | foreach ($format as $c) { |
| | 174 | switch ($c) { |
| | 175 | case 'l': $result .= !empty($parsed_locale['lang'])? $parsed_locale['lang'] : ''; break; |
| | 176 | case 'c': $result .= !empty($parsed_locale['country'])? '_'.$parsed_locale['country'] : ''; break; |
| | 177 | case 'e': $result .= !empty($parsed_locale['encoding'])? '.'.$parsed_locale['encoding'] : ''; break; |
| | 178 | case 'm': $result .= !empty($parsed_locale['modifier'])? '@'.$parsed_locale['modifier'] : ''; break; |
| | 179 | } |
| | 180 | } |
| | 181 | return $result; |
| | 182 | } |
| | 183 | |
| | 184 | ?> |