language.php 16 KB

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