PageRenderTime 62ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/gallery/helpers/locales.php

http://github.com/gallery/gallery3
PHP | 264 lines | 192 code | 20 blank | 52 comment | 20 complexity | c111b91edba70b3216c6989286fb2653 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2013 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /**
  21. * This is the API for handling locales.
  22. */
  23. class locales_Core {
  24. private static $locales;
  25. private static $language_subtag_to_locale;
  26. /**
  27. * Return the list of available locales.
  28. */
  29. static function available() {
  30. if (empty(self::$locales)) {
  31. self::_init_language_data();
  32. }
  33. return self::$locales;
  34. }
  35. static function installed() {
  36. $available = self::available();
  37. $default = module::get_var("gallery", "default_locale");
  38. $codes = explode("|", module::get_var("gallery", "installed_locales", $default));
  39. foreach ($codes as $code) {
  40. if (isset($available[$code])) {
  41. $installed[$code] = $available[$code];
  42. }
  43. }
  44. return $installed;
  45. }
  46. static function update_installed($locales) {
  47. // Ensure that the default is included...
  48. $default = module::get_var("gallery", "default_locale");
  49. $locales = in_array($default, $locales)
  50. ? $locales
  51. : array_merge($locales, array($default));
  52. module::set_var("gallery", "installed_locales", join("|", $locales));
  53. // Clear the cache
  54. self::$locales = null;
  55. }
  56. // @todo Might want to add a localizable language name as well.
  57. // ref: http://cldr.unicode.org/
  58. // ref: http://cldr.unicode.org/index/cldr-spec/picking-the-right-language-code
  59. // ref: http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/likely_subtags.html
  60. private static function _init_language_data() {
  61. $l["af_ZA"] = "Afrikaans"; // Afrikaans
  62. $l["ar_SA"] = "العربية"; // Arabic
  63. $l["be_BY"] = "Беларускі"; // Belarusian
  64. $l["bg_BG"] = "български"; // Bulgarian
  65. $l["bn_BD"] = "বাংলা"; // Bengali
  66. $l["ca_ES"] = "Catalan"; // Catalan
  67. $l["cs_CZ"] = "čeština"; // Czech
  68. $l["da_DK"] = "Dansk"; // Danish
  69. $l["de_DE"] = "Deutsch"; // German
  70. $l["el_GR"] = "Greek"; // Greek
  71. $l["en_GB"] = "English (UK)"; // English (UK)
  72. $l["en_US"] = "English (US)"; // English (US)
  73. $l["es_AR"] = "Español (AR)"; // Spanish (AR)
  74. $l["es_ES"] = "Español"; // Spanish (ES)
  75. $l["es_MX"] = "Español (MX)"; // Spanish (MX)
  76. $l["et_EE"] = "Eesti"; // Estonian
  77. $l["eu_ES"] = "Euskara"; // Basque
  78. $l["fa_IR"] = "فارس"; // Farsi
  79. $l["fi_FI"] = "Suomi"; // Finnish
  80. $l["fo_FO"] = "Føroyskt"; // Faroese
  81. $l["fr_FR"] = "Français"; // French
  82. $l["ga_IE"] = "Gaeilge"; // Irish
  83. $l["he_IL"] = "עברית"; // Hebrew
  84. $l["hr_HR"] = "hr̀vātskī"; // Croatian
  85. $l["hu_HU"] = "Magyar"; // Hungarian
  86. $l["is_IS"] = "Icelandic"; // Icelandic
  87. $l["it_IT"] = "Italiano"; // Italian
  88. $l["ja_JP"] = "日本語"; // Japanese
  89. $l["ko_KR"] = "한국어"; // Korean
  90. $l["lt_LT"] = "Lietuvių"; // Lithuanian
  91. $l["lv_LV"] = "Latviešu"; // Latvian
  92. $l["ms_MY"] = "Bahasa Melayu"; // Malay
  93. $l["mk_MK"] = "Македонски јазик"; // Macedonian
  94. $l["nl_NL"] = "Nederlands"; // Dutch
  95. $l["no_NO"] = "Norsk bokmål"; // Norwegian
  96. $l["pl_PL"] = "Polski"; // Polish
  97. $l["pt_BR"] = "Português do Brasil"; // Portuguese (BR)
  98. $l["pt_PT"] = "Português ibérico"; // Portuguese (PT)
  99. $l["ro_RO"] = "Română"; // Romanian
  100. $l["ru_RU"] = "Русский"; // Russian
  101. $l["sk_SK"] = "Slovenčina"; // Slovak
  102. $l["sl_SI"] = "Slovenščina"; // Slovenian
  103. $l["sr_CS"] = "Srpski"; // Serbian
  104. $l["sv_SE"] = "Svenska"; // Swedish
  105. $l["th_TH"] = "ภาษาไทย"; // Thai
  106. $l["tn_ZA"] = "Setswana"; // Setswana
  107. $l["tr_TR"] = "Türkçe"; // Turkish
  108. $l["uk_UA"] = "українська"; // Ukrainian
  109. $l["vi_VN"] = "Tiếng Việt"; // Vietnamese
  110. $l["zh_CN"] = "简体中文"; // Chinese (CN)
  111. $l["zh_TW"] = "繁體中文"; // Chinese (TW)
  112. asort($l, SORT_LOCALE_STRING);
  113. self::$locales = $l;
  114. // Language subtag to (default) locale mapping
  115. foreach ($l as $locale => $name) {
  116. list ($language) = explode("_", $locale . "_");
  117. // The first one mentioned is the default
  118. if (!isset($d[$language])) {
  119. $d[$language] = $locale;
  120. }
  121. }
  122. self::$language_subtag_to_locale = $d;
  123. }
  124. static function display_name($locale=null) {
  125. if (empty(self::$locales)) {
  126. self::_init_language_data();
  127. }
  128. $locale or $locale = Gallery_I18n::instance()->locale();
  129. return self::$locales[$locale];
  130. }
  131. static function is_rtl($locale=null) {
  132. return Gallery_I18n::instance()->is_rtl($locale);
  133. }
  134. /**
  135. * Returns the best match comparing the HTTP accept-language header
  136. * with the installed locales.
  137. * @todo replace this with request::accepts_language() when we upgrade to Kohana 2.4
  138. */
  139. static function locale_from_http_request() {
  140. $http_accept_language = Input::instance()->server("HTTP_ACCEPT_LANGUAGE");
  141. if ($http_accept_language) {
  142. // Parse the HTTP header and build a preference list
  143. // Example value: "de,en-us;q=0.7,en-uk,fr-fr;q=0.2"
  144. $locale_preferences = array();
  145. foreach (explode(",", $http_accept_language) as $code) {
  146. list ($requested_locale, $qvalue) = explode(";", $code . ";");
  147. $requested_locale = trim($requested_locale);
  148. $qvalue = trim($qvalue);
  149. if (preg_match("/^([a-z]{2,3})(?:[_-]([a-zA-Z]{2}))?/", $requested_locale, $matches)) {
  150. $requested_locale = strtolower($matches[1]);
  151. if (!empty($matches[2])) {
  152. $requested_locale .= "_" . strtoupper($matches[2]);
  153. }
  154. $requested_locale = trim(str_replace("-", "_", $requested_locale));
  155. if (!strlen($qvalue)) {
  156. // If not specified, default to 1.
  157. $qvalue = 1;
  158. } else {
  159. // qvalue is expected to be something like "q=0.7"
  160. list ($ignored, $qvalue) = explode("=", $qvalue . "==");
  161. $qvalue = floatval($qvalue);
  162. }
  163. // Group by language to boost inexact same-language matches
  164. list ($language) = explode("_", $requested_locale . "_");
  165. if (!isset($locale_preferences[$language])) {
  166. $locale_preferences[$language] = array();
  167. }
  168. $locale_preferences[$language][$requested_locale] = $qvalue;
  169. }
  170. }
  171. // Compare and score requested locales with installed ones
  172. $scored_locales = array();
  173. foreach ($locale_preferences as $language => $requested_locales) {
  174. // Inexact match adjustment (same language, different region)
  175. $fallback_adjustment_factor = 0.95;
  176. if (count($requested_locales) > 1) {
  177. // Sort by qvalue, descending
  178. $qvalues = array_values($requested_locales);
  179. rsort($qvalues);
  180. // Ensure inexact match scores worse than 2nd preference in same language.
  181. $fallback_adjustment_factor *= $qvalues[1];
  182. }
  183. foreach ($requested_locales as $requested_locale => $qvalue) {
  184. list ($matched_locale, $match_score) =
  185. self::_locale_match_score($requested_locale, $qvalue, $fallback_adjustment_factor);
  186. if ($matched_locale &&
  187. (!isset($scored_locales[$matched_locale]) ||
  188. $match_score > $scored_locales[$matched_locale])) {
  189. $scored_locales[$matched_locale] = $match_score;
  190. }
  191. }
  192. }
  193. arsort($scored_locales);
  194. list ($locale) = each($scored_locales);
  195. return $locale;
  196. }
  197. return null;
  198. }
  199. private static function _locale_match_score($requested_locale, $qvalue, $adjustment_factor) {
  200. $installed = locales::installed();
  201. if (isset($installed[$requested_locale])) {
  202. return array($requested_locale, $qvalue);
  203. }
  204. list ($language) = explode("_", $requested_locale . "_");
  205. if (isset(self::$language_subtag_to_locale[$language]) &&
  206. isset($installed[self::$language_subtag_to_locale[$language]])) {
  207. $score = $adjustment_factor * $qvalue;
  208. return array(self::$language_subtag_to_locale[$language], $score);
  209. }
  210. return array(null, 0);
  211. }
  212. static function set_request_locale() {
  213. // 1. Check the session specific preference (cookie)
  214. $locale = locales::cookie_locale();
  215. // 2. Check the user's preference
  216. if (!$locale) {
  217. $locale = identity::active_user()->locale;
  218. }
  219. // 3. Check the browser's / OS' preference
  220. if (!$locale) {
  221. $locale = locales::locale_from_http_request();
  222. }
  223. // If we have any preference, override the site's default locale
  224. if ($locale) {
  225. Gallery_I18n::instance()->locale($locale);
  226. }
  227. }
  228. static function cookie_locale() {
  229. // Can't use Input framework for client side cookies since
  230. // they're not signed.
  231. $cookie_data = isset($_COOKIE["g_locale"]) ? $_COOKIE["g_locale"] : null;
  232. $locale = null;
  233. if ($cookie_data) {
  234. if (preg_match("/^([a-z]{2,3}(?:_[A-Z]{2})?)$/", trim($cookie_data), $matches)) {
  235. $requested_locale = $matches[1];
  236. $installed_locales = locales::installed();
  237. if (isset($installed_locales[$requested_locale])) {
  238. $locale = $requested_locale;
  239. }
  240. }
  241. }
  242. return $locale;
  243. }
  244. }