language.php 16 KB

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