language.php 16 KB

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