language.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * utility functions for i18n
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (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 Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category I18n
  23. * @package StatusNet
  24. * @author Matthew Gregg <matthew.gregg@gmail.com>
  25. * @author Ciaran Gultnieks <ciaran@ciarang.com>
  26. * @author Evan Prodromou <evan@status.net>
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. // Locale category constants are usually predefined, but may not be
  34. // on some systems such as Win32.
  35. $LC_CATEGORIES = array('LC_CTYPE',
  36. 'LC_NUMERIC',
  37. 'LC_TIME',
  38. 'LC_COLLATE',
  39. 'LC_MONETARY',
  40. 'LC_MESSAGES',
  41. 'LC_ALL');
  42. foreach ($LC_CATEGORIES as $key => $name) {
  43. if (!defined($name)) {
  44. define($name, $key);
  45. }
  46. }
  47. if (!function_exists('gettext')) {
  48. require_once("php-gettext/gettext.inc");
  49. }
  50. if (!function_exists('dpgettext')) {
  51. /**
  52. * Context-aware dgettext wrapper; use when messages in different contexts
  53. * won't be distinguished from the English source but need different translations.
  54. * The context string will appear as msgctxt in the .po files.
  55. *
  56. * Not currently exposed in PHP's gettext module; implemented to be compat
  57. * with gettext.h's macros.
  58. *
  59. * @param string $domain domain identifier
  60. * @param string $context context identifier, should be some key like "menu|file"
  61. * @param string $msgid English source text
  62. * @return string original or translated message
  63. */
  64. function dpgettext($domain, $context, $msg)
  65. {
  66. $msgid = $context . "\004" . $msg;
  67. $out = dcgettext($domain, $msgid, LC_MESSAGES);
  68. if ($out == $msgid) {
  69. return $msg;
  70. } else {
  71. return $out;
  72. }
  73. }
  74. }
  75. if (!function_exists('pgettext')) {
  76. /**
  77. * Context-aware gettext wrapper; use when messages in different contexts
  78. * won't be distinguished from the English source but need different translations.
  79. * The context string will appear as msgctxt in the .po files.
  80. *
  81. * Not currently exposed in PHP's gettext module; implemented to be compat
  82. * with gettext.h's macros.
  83. *
  84. * @param string $context context identifier, should be some key like "menu|file"
  85. * @param string $msgid English source text
  86. * @return string original or translated message
  87. */
  88. function pgettext($context, $msg)
  89. {
  90. return dpgettext(textdomain(NULL), $context, $msg);
  91. }
  92. }
  93. if (!function_exists('dnpgettext')) {
  94. /**
  95. * Context-aware dngettext wrapper; use when messages in different contexts
  96. * won't be distinguished from the English source but need different translations.
  97. * The context string will appear as msgctxt in the .po files.
  98. *
  99. * Not currently exposed in PHP's gettext module; implemented to be compat
  100. * with gettext.h's macros.
  101. *
  102. * @param string $domain domain identifier
  103. * @param string $context context identifier, should be some key like "menu|file"
  104. * @param string $msg singular English source text
  105. * @param string $plural plural English source text
  106. * @param int $n number of items to control plural selection
  107. * @return string original or translated message
  108. */
  109. function dnpgettext($domain, $context, $msg, $plural, $n)
  110. {
  111. $msgid = $context . "\004" . $msg;
  112. $out = dcngettext($domain, $msgid, $plural, $n, LC_MESSAGES);
  113. if ($out == $msgid) {
  114. return $msg;
  115. } else {
  116. return $out;
  117. }
  118. }
  119. }
  120. if (!function_exists('npgettext')) {
  121. /**
  122. * Context-aware ngettext wrapper; use when messages in different contexts
  123. * won't be distinguished from the English source but need different translations.
  124. * The context string will appear as msgctxt in the .po files.
  125. *
  126. * Not currently exposed in PHP's gettext module; implemented to be compat
  127. * with gettext.h's macros.
  128. *
  129. * @param string $context context identifier, should be some key like "menu|file"
  130. * @param string $msg singular English source text
  131. * @param string $plural plural English source text
  132. * @param int $n number of items to control plural selection
  133. * @return string original or translated message
  134. */
  135. function npgettext($context, $msg, $plural, $n)
  136. {
  137. return dnpgettext(textdomain(NULL), $msgid, $plural, $n, LC_MESSAGES);
  138. }
  139. }
  140. /**
  141. * Shortcut for *gettext functions with smart domain detection.
  142. *
  143. * If calling from a plugin, this function checks which plugin was
  144. * being called from and uses that as text domain, which will have
  145. * been set up during plugin initialization.
  146. *
  147. * Also handles plurals and contexts depending on what parameters
  148. * are passed to it:
  149. *
  150. * gettext -> _m($msg)
  151. * ngettext -> _m($msg1, $msg2, $n)
  152. * pgettext -> _m($ctx, $msg)
  153. * npgettext -> _m($ctx, $msg1, $msg2, $n)
  154. *
  155. * @fixme may not work properly in eval'd code
  156. *
  157. * @param string $msg
  158. * @return string
  159. */
  160. function _m($msg/*, ...*/)
  161. {
  162. $domain = _mdomain(debug_backtrace());
  163. $args = func_get_args();
  164. switch(count($args)) {
  165. case 1: return dgettext($domain, $msg);
  166. case 2: return dpgettext($domain, $args[0], $args[1]);
  167. case 3: return dngettext($domain, $args[0], $args[1], $args[2]);
  168. case 4: return dnpgettext($domain, $args[0], $args[1], $args[2], $args[3]);
  169. default: throw new Exception("Bad parameter count to _m()");
  170. }
  171. }
  172. /**
  173. * Looks for which plugin we've been called from to set the gettext domain;
  174. * if not in a plugin subdirectory, we'll use the default 'statusnet'.
  175. *
  176. * Note: we can't return null for default domain since most of the PHP gettext
  177. * wrapper functions turn null into "" before passing to the backend library.
  178. *
  179. * @param array $backtrace debug_backtrace() output
  180. * @return string
  181. * @private
  182. * @fixme could explode if SN is under a 'plugins' folder or share name.
  183. */
  184. function _mdomain($backtrace)
  185. {
  186. /*
  187. 0 =>
  188. array
  189. 'file' => string '/var/www/mublog/plugins/FeedSub/FeedSubPlugin.php' (length=49)
  190. 'line' => int 77
  191. 'function' => string '_m' (length=2)
  192. 'args' =>
  193. array
  194. 0 => &string 'Feeds' (length=5)
  195. */
  196. static $cached;
  197. $path = $backtrace[0]['file'];
  198. if (!isset($cached[$path])) {
  199. $final = 'statusnet'; // assume default domain
  200. if (DIRECTORY_SEPARATOR !== '/') {
  201. $path = strtr($path, DIRECTORY_SEPARATOR, '/');
  202. }
  203. $plug = strpos($path, '/plugins/');
  204. if ($plug === false) {
  205. // We're not in a plugin; return default domain.
  206. $final = 'statusnet';
  207. } else {
  208. $cut = $plug + 9;
  209. $cut2 = strpos($path, '/', $cut);
  210. if ($cut2) {
  211. $final = substr($path, $cut, $cut2 - $cut);
  212. } else {
  213. // We might be running directly from the plugins dir?
  214. // If so, there's no place to store locale info.
  215. $final = 'statusnet';
  216. }
  217. }
  218. $cached[$path] = $final;
  219. }
  220. return $cached[$path];
  221. }
  222. /**
  223. * Content negotiation for language codes
  224. *
  225. * @param string $httplang HTTP Accept-Language header
  226. *
  227. * @return string language code for best language match
  228. */
  229. function client_prefered_language($httplang)
  230. {
  231. $client_langs = array();
  232. $all_languages = common_config('site', 'languages');
  233. preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
  234. strtolower($httplang), $httplang);
  235. for ($i = 0; $i < count($httplang); $i++) {
  236. if (!empty($httplang[2][$i])) {
  237. // if no q default to 1.0
  238. $client_langs[$httplang[2][$i]] =
  239. ($httplang[6][$i]? (float) $httplang[6][$i] : 1.0 - ($i*0.01));
  240. }
  241. if (!empty($httplang[3][$i]) && empty($client_langs[$httplang[3][$i]])) {
  242. // if a catchall default 0.01 lower
  243. $client_langs[$httplang[3][$i]] =
  244. ($httplang[6][$i]? (float) $httplang[6][$i]-0.01 : 0.99);
  245. }
  246. }
  247. // sort in decending q
  248. arsort($client_langs);
  249. foreach ($client_langs as $lang => $q) {
  250. if (isset($all_languages[$lang])) {
  251. return($all_languages[$lang]['lang']);
  252. }
  253. }
  254. return false;
  255. }
  256. /**
  257. * returns a simple code -> name mapping for languages
  258. *
  259. * @return array map of available languages by code to language name.
  260. */
  261. function get_nice_language_list()
  262. {
  263. $nice_lang = array();
  264. $all_languages = common_config('site', 'languages');
  265. foreach ($all_languages as $lang) {
  266. $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
  267. }
  268. return $nice_lang;
  269. }
  270. /*
  271. * Check whether a language is right-to-left
  272. *
  273. * @param string $lang language code of the language to check
  274. *
  275. * @return boolean true if language is rtl
  276. */
  277. function is_rtl($lang)
  278. {
  279. $all_languages = common_config('site', 'languages');
  280. $lang = $all_languages[$lang];
  281. return ($lang['direction'] == 'rtl');
  282. }
  283. /**
  284. * Get a list of all languages that are enabled in the default config
  285. *
  286. * This should ONLY be called when setting up the default config in common.php.
  287. * Any other attempt to get a list of languages should instead call
  288. * common_config('site','languages')
  289. *
  290. * @return array mapping of language codes to language info
  291. */
  292. function get_all_languages() {
  293. return array(
  294. 'af' => array('q' => 0.8, 'lang' => 'af', 'name' => 'Afrikaans', 'direction' => 'ltr'),
  295. 'ar' => array('q' => 0.8, 'lang' => 'ar', 'name' => 'Arabic', 'direction' => 'rtl'),
  296. 'ast' => array('q' => 1, 'lang' => 'ast', 'name' => 'Asturian', 'direction' => 'ltr'),
  297. 'eu' => array('q' => 1, 'lang' => 'eu', 'name' => 'Basque', 'direction' => 'ltr'),
  298. 'be-tarask' => array('q' => 0.5, 'lang' => 'be-tarask', 'name' => 'Belarusian (Taraškievica orthography)', 'direction' => 'ltr'),
  299. 'br' => array('q' => 0.8, 'lang' => 'br', 'name' => 'Breton', 'direction' => 'ltr'),
  300. 'bg' => array('q' => 0.8, 'lang' => 'bg', 'name' => 'Bulgarian', 'direction' => 'ltr'),
  301. 'my' => array('q' => 1, 'lang' => 'my', 'name' => 'Burmese', 'direction' => 'ltr'),
  302. 'ca' => array('q' => 0.5, 'lang' => 'ca', 'name' => 'Catalan', 'direction' => 'ltr'),
  303. 'zh-cn' => array('q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'),
  304. 'zh-hant' => array('q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'),
  305. 'ksh' => array('q' => 1, 'lang' => 'ksh', 'name' => 'Colognian', 'direction' => 'ltr'),
  306. 'cs' => array('q' => 0.5, 'lang' => 'cs', 'name' => 'Czech', 'direction' => 'ltr'),
  307. 'da' => array('q' => 0.8, 'lang' => 'da', 'name' => 'Danish', 'direction' => 'ltr'),
  308. 'nl' => array('q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'),
  309. 'arz' => array('q' => 0.8, 'lang' => 'arz', 'name' => 'Egyptian Spoken Arabic', 'direction' => 'rtl'),
  310. 'en' => array('q' => 1, 'lang' => 'en', 'name' => 'English', 'direction' => 'ltr'),
  311. 'en-us' => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'),
  312. 'en-gb' => array('q' => 1, 'lang' => 'en_GB', 'name' => 'English (British)', 'direction' => 'ltr'),
  313. 'eo' => array('q' => 0.8, 'lang' => 'eo', 'name' => 'Esperanto', 'direction' => 'ltr'),
  314. 'fi' => array('q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'),
  315. 'fr' => array('q' => 1, 'lang' => 'fr', 'name' => 'French', 'direction' => 'ltr'),
  316. 'fr-fr' => array('q' => 1, 'lang' => 'fr', 'name' => 'French (France)', 'direction' => 'ltr'),
  317. 'fur' => array('q' => 0.8, 'lang' => 'fur', 'name' => 'Friulian', 'direction' => 'ltr'),
  318. 'gl' => array('q' => 0.8, 'lang' => 'gl', 'name' => 'Galician', 'direction' => 'ltr'),
  319. 'ka' => array('q' => 0.8, 'lang' => 'ka', 'name' => 'Georgian', 'direction' => 'ltr'),
  320. 'de' => array('q' => 0.8, 'lang' => 'de', 'name' => 'German', 'direction' => 'ltr'),
  321. 'el' => array('q' => 0.1, 'lang' => 'el', 'name' => 'Greek', 'direction' => 'ltr'),
  322. 'he' => array('q' => 0.5, 'lang' => 'he', 'name' => 'Hebrew', 'direction' => 'rtl'),
  323. 'hu' => array('q' => 0.8, 'lang' => 'hu', 'name' => 'Hungarian', 'direction' => 'ltr'),
  324. 'is' => array('q' => 0.1, 'lang' => 'is', 'name' => 'Icelandic', 'direction' => 'ltr'),
  325. 'id' => array('q' => 1, 'lang' => 'id', 'name' => 'Indonesian', 'direction' => 'ltr'),
  326. 'ia' => array('q' => 0.8, 'lang' => 'ia', 'name' => 'Interlingua', 'direction' => 'ltr'),
  327. 'ga' => array('q' => 0.5, 'lang' => 'ga', 'name' => 'Irish', 'direction' => 'ltr'),
  328. 'it' => array('q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'),
  329. 'ja' => array('q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'),
  330. 'ko' => array('q' => 0.9, 'lang' => 'ko', 'name' => 'Korean', 'direction' => 'ltr'),
  331. 'lv' => array('q' => 1, 'lang' => 'lv', 'name' => 'Latvian', 'direction' => 'ltr'),
  332. 'lt' => array('q' => 1, 'lang' => 'lt', 'name' => 'Lithuanian', 'direction' => 'ltr'),
  333. 'lb' => array('q' => 1, 'lang' => 'lb', 'name' => 'Luxembourgish', 'direction' => 'ltr'),
  334. 'mk' => array('q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'),
  335. 'mg' => array('q' => 1, 'lang' => 'mg', 'name' => 'Malagasy', 'direction' => 'ltr'),
  336. 'ms' => array('q' => 1, 'lang' => 'ms', 'name' => 'Malay', 'direction' => 'ltr'),
  337. 'ml' => array('q' => 0.5, 'lang' => 'ml', 'name' => 'Malayalam', 'direction' => 'ltr'),
  338. 'ne' => array('q' => 1, 'lang' => 'ne', 'name' => 'Nepali', 'direction' => 'ltr'),
  339. 'nb' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
  340. 'no' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
  341. 'nn' => array('q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'),
  342. 'fa' => array('q' => 1, 'lang' => 'fa', 'name' => 'Persian', 'direction' => 'rtl'),
  343. 'pl' => array('q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'),
  344. 'pt' => array('q' => 0.1, 'lang' => 'pt', 'name' => 'Portuguese', 'direction' => 'ltr'),
  345. 'pt-br' => array('q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Portuguese Brazil', 'direction' => 'ltr'),
  346. 'ru' => array('q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'),
  347. 'sr-ec' => array('q' => 1, 'lang' => 'sr-ec', 'name' => 'Serbian', 'direction' => 'ltr'),
  348. 'es' => array('q' => 1, 'lang' => 'es', 'name' => 'Spanish', 'direction' => 'ltr'),
  349. 'sv' => array('q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'),
  350. 'tl' => array('q' => 0.8, 'lang' => 'tl', 'name' => 'Tagalog', 'direction' => 'ltr'),
  351. 'ta' => array('q' => 1, 'lang' => 'ta', 'name' => 'Tamil', 'direction' => 'ltr'),
  352. 'te' => array('q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'),
  353. 'tr' => array('q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'),
  354. 'uk' => array('q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'),
  355. 'hsb' => array('q' => 0.8, 'lang' => 'hsb', 'name' => 'Upper Sorbian', 'direction' => 'ltr'),
  356. 'ur_PK' => array('q' => 1, 'lang' => 'ur', 'name' => 'Urdu (Pakistan)', 'direction' => 'rtl'),
  357. 'vi' => array('q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'),
  358. );
  359. }