I18nTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\I18n;
  4. use PrivateBin\Json;
  5. class I18nMock extends I18n
  6. {
  7. public static function resetAvailableLanguages()
  8. {
  9. self::$_availableLanguages = [];
  10. }
  11. public static function resetPath($path = '')
  12. {
  13. self::$_path = $path;
  14. }
  15. public static function getPath($file = '')
  16. {
  17. return self::_getPath($file);
  18. }
  19. }
  20. class I18nTest extends TestCase
  21. {
  22. private $_translations = [];
  23. public function setUp(): void
  24. {
  25. /* Setup Routine */
  26. $this->_translations = json_decode(
  27. file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . 'de.json'),
  28. true
  29. );
  30. }
  31. public function tearDown(): void
  32. {
  33. unset($_COOKIE['lang'], $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  34. I18n::loadTranslations();
  35. }
  36. public function testTranslationFallback()
  37. {
  38. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  39. $messageId = 'It does not matter if the message ID exists';
  40. I18n::loadTranslations();
  41. $this->assertEquals($messageId, I18n::_($messageId), 'fallback to en');
  42. I18n::getLanguageLabels();
  43. }
  44. public function testCookieLanguageDeDetection()
  45. {
  46. $_COOKIE['lang'] = 'de';
  47. I18n::loadTranslations();
  48. $this->assertEquals($_COOKIE['lang'], I18n::getLanguage(), 'browser language de');
  49. $this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in German');
  50. $this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in German');
  51. $this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in German');
  52. }
  53. public function testBrowserLanguageDeDetection()
  54. {
  55. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-CH,de;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2,fr;q=0.0';
  56. I18n::loadTranslations();
  57. $this->assertEquals('de', I18n::getLanguage(), 'browser language de');
  58. $this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in German');
  59. $this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in German');
  60. $this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in German');
  61. }
  62. public function testBrowserLanguageFrDetection()
  63. {
  64. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-CH,fr;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2,de;q=0.0';
  65. I18n::loadTranslations();
  66. $this->assertEquals('fr', I18n::getLanguage(), 'browser language fr');
  67. $this->assertEquals('0 heure', I18n::_('%d hours', 0), '0 hours in French');
  68. $this->assertEquals('1 heure', I18n::_('%d hours', 1), '1 hour in French');
  69. $this->assertEquals('2 heures', I18n::_('%d hours', 2), '2 hours in French');
  70. }
  71. public function testEqualQualityPreservesBrowserOrder()
  72. {
  73. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr;q=0.8,de;q=0.8';
  74. I18n::loadTranslations();
  75. $this->assertEquals('fr', I18n::getLanguage());
  76. }
  77. public function testBulgarianMonthExpirationTranslation()
  78. {
  79. $_COOKIE['lang'] = 'bg';
  80. I18n::loadTranslations();
  81. $this->assertEquals(
  82. 'Този документ изтича след един месец.',
  83. I18n::_('This document will expire in %d months.', 1)
  84. );
  85. $this->assertEquals(
  86. 'Този документ изтича след 2 месеца.',
  87. I18n::_('This document will expire in %d months.', 2)
  88. );
  89. }
  90. public function testBrowserLanguageLtDetection()
  91. {
  92. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'lt;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  93. I18n::loadTranslations();
  94. $this->assertEquals('lt', I18n::getLanguage(), 'browser language lt');
  95. $this->assertEquals('1 sekundę', I18n::_('%d seconds', 1), '1 second in Lithuanian');
  96. $this->assertEquals('2 sekundės', I18n::_('%d seconds', 2), '2 seconds in Lithuanian');
  97. $this->assertEquals('10 sekundžių', I18n::_('%d seconds', 10), '10 seconds in Lithuanian');
  98. $this->assertEquals('22 sekundės', I18n::_('%d seconds', 22), '22 seconds in Lithuanian');
  99. $this->assertEquals('110 sekundžių', I18n::_('%d seconds', 110), '110 seconds in Lithuanian');
  100. $this->assertEquals('111 sekundžių', I18n::_('%d seconds', 111), '111 seconds in Lithuanian');
  101. $this->assertEquals('120 sekundžių', I18n::_('%d seconds', 120), '120 seconds in Lithuanian');
  102. }
  103. public function testBrowserLanguageNoDetection()
  104. {
  105. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'no;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  106. I18n::loadTranslations();
  107. $this->assertEquals('no', I18n::getLanguage(), 'browser language no');
  108. $this->assertEquals('0 timer', I18n::_('%d hours', 0), '0 hours in Norwegian');
  109. $this->assertEquals('1 time', I18n::_('%d hours', 1), '1 hour in Norwegian');
  110. $this->assertEquals('2 timer', I18n::_('%d hours', 2), '2 hours in Norwegian');
  111. }
  112. public function testBrowserLanguageOcDetection()
  113. {
  114. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'oc;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  115. I18n::loadTranslations();
  116. $this->assertEquals('oc', I18n::getLanguage(), 'browser language oc');
  117. $this->assertEquals('0 ora', I18n::_('%d hours', 0), '0 hours in Occitan');
  118. $this->assertEquals('1 ora', I18n::_('%d hours', 1), '1 hour in Occitan');
  119. $this->assertEquals('2 oras', I18n::_('%d hours', 2), '2 hours in Occitan');
  120. }
  121. public function testBrowserLanguageZhDetection()
  122. {
  123. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'zh;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  124. I18n::loadTranslations();
  125. $this->assertEquals('zh', I18n::getLanguage(), 'browser language zh');
  126. $this->assertEquals('0 小时', I18n::_('%d hours', 0), '0 hours in Chinese');
  127. $this->assertEquals('1 小时', I18n::_('%d hours', 1), '1 hour in Chinese');
  128. $this->assertEquals('2 小时', I18n::_('%d hours', 2), '2 hours in Chinese');
  129. }
  130. public function testBrowserLanguageZhHantDetection()
  131. {
  132. foreach ([
  133. 'zh-Hant,zh;q=0.8,en;q=0.2' => 'zh-tw',
  134. 'zh-Hant-TW,zh;q=0.8,en;q=0.2' => 'zh-tw',
  135. 'zh-TW,en;q=0.2' => 'zh-tw',
  136. 'zh-TW-u-nu-hanidec,en;q=0.2' => 'zh-tw',
  137. 'zh-HK,zh;q=0.8,en;q=0.2' => 'zh-tw',
  138. 'zh-MO,zh;q=0.8,en;q=0.2' => 'zh-tw',
  139. 'zh-Hant,zh-Hans;q=0.8,en;q=0.2' => 'zh-tw',
  140. 'zh-CN,zh-Hant;q=0.8,en;q=0.2' => 'zh',
  141. 'zh-Hans,zh-Hant;q=0.8,en;q=0.2' => 'zh',
  142. 'zh-Hans-CN,en;q=0.2' => 'zh',
  143. 'zh;q=0.9,zh-Hant;q=0.8,en;q=0.2' => 'zh',
  144. ] as $acceptedLanguage => $language) {
  145. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $acceptedLanguage;
  146. I18n::loadTranslations();
  147. $this->assertEquals($language, I18n::getLanguage(), 'browser language ' . $acceptedLanguage);
  148. }
  149. }
  150. public function testBrowserLanguagePlDetection()
  151. {
  152. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  153. I18n::loadTranslations();
  154. $this->assertEquals('pl', I18n::getLanguage(), 'browser language pl');
  155. $this->assertEquals('1 godzina', I18n::_('%d hours', 1), '1 hour in Polish');
  156. $this->assertEquals('2 godziny', I18n::_('%d hours', 2), '2 hours in Polish');
  157. $this->assertEquals('12 godzin', I18n::_('%d hours', 12), '12 hours in Polish');
  158. $this->assertEquals('22 godziny', I18n::_('%d hours', 22), '22 hours in Polish');
  159. $this->assertEquals('1 minuta', I18n::_('%d minutes', 1), '1 minute in Polish');
  160. $this->assertEquals('3 minuty', I18n::_('%d minutes', 3), '3 minutes in Polish');
  161. $this->assertEquals('13 minut', I18n::_('%d minutes', 13), '13 minutes in Polish');
  162. $this->assertEquals('23 minuty', I18n::_('%d minutes', 23), '23 minutes in Polish');
  163. }
  164. public function testBrowserLanguageRuDetection()
  165. {
  166. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  167. I18n::loadTranslations();
  168. $this->assertEquals('ru', I18n::getLanguage(), 'browser language ru');
  169. $this->assertEquals('1 минуту', I18n::_('%d minutes', 1), '1 minute in Russian');
  170. $this->assertEquals('3 минуты', I18n::_('%d minutes', 3), '3 minutes in Russian');
  171. $this->assertEquals('10 минут', I18n::_('%d minutes', 10), '10 minutes in Russian');
  172. $this->assertEquals('21 минуту', I18n::_('%d minutes', 21), '21 minutes in Russian');
  173. }
  174. public function testBrowserLanguageSlDetection()
  175. {
  176. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'sl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  177. I18n::loadTranslations();
  178. $this->assertEquals('sl', I18n::getLanguage(), 'browser language sl');
  179. $this->assertEquals('0 ur', I18n::_('%d hours', 0), '0 hours in Slovene');
  180. $this->assertEquals('1 ura', I18n::_('%d hours', 1), '1 hour in Slovene');
  181. $this->assertEquals('2 uri', I18n::_('%d hours', 2), '2 hours in Slovene');
  182. $this->assertEquals('3 ure', I18n::_('%d hours', 3), '3 hours in Slovene');
  183. $this->assertEquals('11 ur', I18n::_('%d hours', 11), '11 hours in Slovene');
  184. $this->assertEquals('101 ura', I18n::_('%d hours', 101), '101 hours in Slovene');
  185. $this->assertEquals('102 uri', I18n::_('%d hours', 102), '102 hours in Slovene');
  186. $this->assertEquals('104 ure', I18n::_('%d hours', 104), '104 hours in Slovene');
  187. }
  188. public function testBrowserLanguageCsDetection()
  189. {
  190. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'cs;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  191. I18n::loadTranslations();
  192. $this->assertEquals('cs', I18n::getLanguage(), 'browser language cs');
  193. $this->assertEquals('1 hodina', I18n::_('%d hours', 1), '1 hour in Czech');
  194. $this->assertEquals('2 hodiny', I18n::_('%d hours', 2), '2 hours in Czech');
  195. $this->assertEquals('5 minut', I18n::_('%d minutes', 5), '5 minutes in Czech');
  196. $this->assertEquals('14 minut', I18n::_('%d minutes', 14), '14 minutes in Czech');
  197. }
  198. public function testInvalidBrowserLanguageQualityIsIgnored()
  199. {
  200. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de;q=2,en;q=0.8';
  201. $this->assertSame(['0.8' => ['en']], I18n::getBrowserLanguages());
  202. }
  203. public function testBrowserLanguageAnyDetection()
  204. {
  205. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
  206. I18n::loadTranslations();
  207. $this->assertTrue(strlen(I18n::getLanguage()) >= 2, 'browser language any');
  208. }
  209. public function testVariableInjection()
  210. {
  211. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  212. I18n::loadTranslations();
  213. $this->assertEquals('some string + 1', I18n::_('some %s + %d', 'string', 1), 'browser language en');
  214. }
  215. public function testHtmlEntityEncoding()
  216. {
  217. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  218. I18n::loadTranslations();
  219. $input = '&<>"\'/`=';
  220. $result = htmlspecialchars($input, ENT_QUOTES | ENT_HTML5 | ENT_DISALLOWED, 'UTF-8', false);
  221. $this->assertEquals($result, I18n::encode($input), 'encodes HTML entities');
  222. $this->assertEquals('<a>some ' . $result . ' + 1</a>', I18n::_('<a>some %s + %d</a>', $input, 1), 'encodes parameters in translations');
  223. // Message ID should NOT be encoded (it comes from trusted source), only the parameter should be
  224. $this->assertEquals($input . $result, I18n::_($input . '%s', $input), 'encodes only parameters, not message ID');
  225. }
  226. public function testApostropheEncodngInMessage()
  227. {
  228. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr';
  229. I18n::loadTranslations();
  230. // For example, the French translation should not have the apostrophe encoded
  231. // See https://github.com/PrivateBin/PrivateBin/issues/1712
  232. $message = I18n::_('Document does not exist, has expired or has been deleted.');
  233. $this->assertFalse(str_contains($message, '&apos;'), 'French apostrophe should not be encoded in translation message');
  234. $this->assertTrue(str_contains($message, "n'existe"), 'French apostrophe should be present as literal character');
  235. }
  236. public function testFallbackAlwaysPresent()
  237. {
  238. $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_i18n';
  239. if (!is_dir($path)) {
  240. mkdir($path);
  241. }
  242. $languageIterator = new AppendIterator();
  243. $languageIterator->append(new GlobIterator(I18nMock::getPath('??.json')));
  244. $languageIterator->append(new GlobIterator(I18nMock::getPath('???.json'))); // for jbo
  245. $languageIterator->append(new GlobIterator(I18nMock::getPath('??-??.json'))); // for regional variants like zh-tw
  246. $languageCount = 0;
  247. foreach ($languageIterator as $file) {
  248. ++$languageCount;
  249. $this->assertTrue(copy($file->getPathname(), $path . DIRECTORY_SEPARATOR . $file->getBasename()));
  250. }
  251. I18nMock::resetPath($path);
  252. $languagesDevelopment = I18nMock::getAvailableLanguages();
  253. $this->assertEquals($languageCount, count($languagesDevelopment), 'all copied languages detected');
  254. $this->assertTrue(in_array('en', $languagesDevelopment), 'English fallback present');
  255. unlink($path . DIRECTORY_SEPARATOR . 'en.json');
  256. I18nMock::resetAvailableLanguages();
  257. $languagesDeployed = I18nMock::getAvailableLanguages();
  258. $this->assertEquals($languageCount, count($languagesDeployed), 'all copied languages detected, plus fallback');
  259. $this->assertTrue(in_array('en', $languagesDeployed), 'English fallback still present');
  260. I18nMock::resetAvailableLanguages();
  261. I18nMock::resetPath();
  262. Helper::rmDir($path);
  263. }
  264. public function testGetCopyHotkey()
  265. {
  266. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)';
  267. $this->assertEquals('Cmd', I18n::getCopyHotkey(), 'returns Cmd on macOS');
  268. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)';
  269. $this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl on Windows');
  270. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Linux x86_64)';
  271. $this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl on Linux');
  272. unset($_SERVER['HTTP_USER_AGENT']);
  273. $this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl when user agent absent');
  274. }
  275. public function testRightToLeftLanguages()
  276. {
  277. foreach (['ar', 'fa', 'he'] as $language) {
  278. $_COOKIE['lang'] = $language;
  279. I18n::loadTranslations();
  280. $this->assertTrue(I18n::isRtl(), "$language is right-to-left");
  281. }
  282. $_COOKIE['lang'] = 'en';
  283. I18n::loadTranslations();
  284. $this->assertFalse(I18n::isRtl(), 'English is left-to-right');
  285. }
  286. public function testMessageIdsExistInAllLanguages()
  287. {
  288. $messageIds = [];
  289. $languages = [];
  290. foreach (new DirectoryIterator(PATH . 'i18n') as $file) {
  291. $fileNameLength = strlen($file->getFilename());
  292. if ($fileNameLength === 7) { // xx.json
  293. $language = substr($file->getFilename(), 0, 2);
  294. } elseif ($fileNameLength === 8) { // jbo.json
  295. $language = substr($file->getFilename(), 0, 3);
  296. } elseif ($fileNameLength === 10) { // xx-xx.json
  297. $language = substr($file->getFilename(), 0, 5);
  298. } else {
  299. continue;
  300. }
  301. $languageJson = file_get_contents($file->getPathname());
  302. $languageMessageIds = array_keys(Json::decode($languageJson));
  303. $messageIds = array_unique(array_merge($messageIds, $languageMessageIds));
  304. $languages[$language] = $languageMessageIds;
  305. }
  306. foreach ($messageIds as $messageId) {
  307. foreach (array_keys($languages) as $language) {
  308. // most languages don't translate the data size units, ignore those
  309. if ($messageId !== 'B' && strlen($messageId) !== 3 && strpos($messageId, 'B', 2) !== 2) {
  310. $this->assertContains(
  311. $messageId,
  312. $languages[$language],
  313. "message ID '$messageId' exists in translation file $language.json"
  314. );
  315. }
  316. }
  317. }
  318. }
  319. }