2015-09-04 20:24:56 -04:00
|
|
|
<?php
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2020-10-10 06:08:58 -04:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-09 05:54:42 -04:00
|
|
|
use PrivateBin\I18n;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2020-10-10 06:08:58 -04:00
|
|
|
class I18nTest extends TestCase
|
2015-09-04 20:24:56 -04:00
|
|
|
{
|
|
|
|
private $_translations = array();
|
|
|
|
|
2020-10-10 06:22:20 -04:00
|
|
|
public function setUp(): void
|
2015-09-04 20:24:56 -04:00
|
|
|
{
|
|
|
|
/* Setup Routine */
|
|
|
|
$this->_translations = json_decode(
|
|
|
|
file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . 'de.json'),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-10 06:22:20 -04:00
|
|
|
public function tearDown(): void
|
2015-09-04 20:24:56 -04:00
|
|
|
{
|
2020-10-11 04:31:24 -04:00
|
|
|
unset($_COOKIE['lang'], $_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTranslationFallback()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
|
2016-10-29 04:24:08 -04:00
|
|
|
$messageId = 'It does not matter if the message ID exists';
|
2016-08-09 05:54:42 -04:00
|
|
|
I18n::loadTranslations();
|
|
|
|
$this->assertEquals($messageId, I18n::_($messageId), 'fallback to en');
|
2019-05-11 16:18:35 -04:00
|
|
|
I18n::getLanguageLabels();
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
|
|
|
|
2015-09-19 11:23:10 -04:00
|
|
|
public function testCookieLanguageDeDetection()
|
|
|
|
{
|
|
|
|
$_COOKIE['lang'] = 'de';
|
2016-08-09 05:54:42 -04:00
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertEquals($_COOKIE['lang'], I18n::getLanguage(), 'browser language de');
|
2017-01-08 01:56:56 -05:00
|
|
|
$this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in German');
|
|
|
|
$this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in German');
|
|
|
|
$this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in German');
|
2015-09-19 11:23:10 -04:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:21:17 -04:00
|
|
|
public function testBrowserLanguageDeDetection()
|
2015-09-04 20:24:56 -04:00
|
|
|
{
|
2019-05-11 16:18:35 -04:00
|
|
|
$_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';
|
2016-08-09 05:54:42 -04:00
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertEquals('de', I18n::getLanguage(), 'browser language de');
|
2017-01-08 01:56:56 -05:00
|
|
|
$this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in German');
|
|
|
|
$this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in German');
|
|
|
|
$this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in German');
|
2015-09-06 13:21:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBrowserLanguageFrDetection()
|
|
|
|
{
|
2019-05-11 16:18:35 -04:00
|
|
|
$_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';
|
2016-08-09 05:54:42 -04:00
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertEquals('fr', I18n::getLanguage(), 'browser language fr');
|
2017-01-08 01:56:56 -05:00
|
|
|
$this->assertEquals('0 heure', I18n::_('%d hours', 0), '0 hours in French');
|
|
|
|
$this->assertEquals('1 heure', I18n::_('%d hours', 1), '1 hour in French');
|
|
|
|
$this->assertEquals('2 heures', I18n::_('%d hours', 2), '2 hours in French');
|
|
|
|
}
|
|
|
|
|
2017-01-10 14:37:14 -05:00
|
|
|
public function testBrowserLanguageNoDetection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'no;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
|
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertEquals('no', I18n::getLanguage(), 'browser language no');
|
2017-01-10 14:37:14 -05:00
|
|
|
$this->assertEquals('0 timer', I18n::_('%d hours', 0), '0 hours in Norwegian');
|
|
|
|
$this->assertEquals('1 time', I18n::_('%d hours', 1), '1 hour in Norwegian');
|
|
|
|
$this->assertEquals('2 timer', I18n::_('%d hours', 2), '2 hours in Norwegian');
|
|
|
|
}
|
|
|
|
|
2017-01-08 01:56:56 -05:00
|
|
|
public function testBrowserLanguageOcDetection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'oc;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
|
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertEquals('oc', I18n::getLanguage(), 'browser language oc');
|
2017-01-08 01:56:56 -05:00
|
|
|
$this->assertEquals('0 ora', I18n::_('%d hours', 0), '0 hours in Occitan');
|
|
|
|
$this->assertEquals('1 ora', I18n::_('%d hours', 1), '1 hour in Occitan');
|
|
|
|
$this->assertEquals('2 oras', I18n::_('%d hours', 2), '2 hours in Occitan');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBrowserLanguageZhDetection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'zh;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
|
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertEquals('zh', I18n::getLanguage(), 'browser language zh');
|
2017-01-08 01:56:56 -05:00
|
|
|
$this->assertEquals('0 小时', I18n::_('%d hours', 0), '0 hours in Chinese');
|
|
|
|
$this->assertEquals('1 小时', I18n::_('%d hours', 1), '1 hour in Chinese');
|
|
|
|
$this->assertEquals('2 小时', I18n::_('%d hours', 2), '2 hours in Chinese');
|
2015-09-06 13:21:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBrowserLanguagePlDetection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
|
2016-08-09 05:54:42 -04:00
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertEquals('pl', I18n::getLanguage(), 'browser language pl');
|
2017-01-08 01:56:56 -05:00
|
|
|
$this->assertEquals('1 godzina', I18n::_('%d hours', 1), '1 hour in Polish');
|
|
|
|
$this->assertEquals('2 godzina', I18n::_('%d hours', 2), '2 hours in Polish');
|
|
|
|
$this->assertEquals('12 godzinę', I18n::_('%d hours', 12), '12 hours in Polish');
|
|
|
|
$this->assertEquals('22 godzina', I18n::_('%d hours', 22), '22 hours in Polish');
|
|
|
|
$this->assertEquals('1 minut', I18n::_('%d minutes', 1), '1 minute in Polish');
|
|
|
|
$this->assertEquals('3 minut', I18n::_('%d minutes', 3), '3 minutes in Polish');
|
|
|
|
$this->assertEquals('13 minut', I18n::_('%d minutes', 13), '13 minutes in Polish');
|
|
|
|
$this->assertEquals('23 minut', I18n::_('%d minutes', 23), '23 minutes in Polish');
|
2016-12-25 05:37:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBrowserLanguageRuDetection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
|
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertEquals('ru', I18n::getLanguage(), 'browser language ru');
|
2017-01-08 01:56:56 -05:00
|
|
|
$this->assertEquals('1 минуту', I18n::_('%d minutes', 1), '1 minute in Russian');
|
|
|
|
$this->assertEquals('3 минуты', I18n::_('%d minutes', 3), '3 minutes in Russian');
|
|
|
|
$this->assertEquals('10 минут', I18n::_('%d minutes', 10), '10 minutes in Russian');
|
|
|
|
$this->assertEquals('21 минуту', I18n::_('%d minutes', 21), '21 minutes in Russian');
|
2015-09-06 13:21:17 -04:00
|
|
|
}
|
|
|
|
|
2017-01-01 08:35:39 -05:00
|
|
|
public function testBrowserLanguageSlDetection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'sl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
|
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertEquals('sl', I18n::getLanguage(), 'browser language sl');
|
2017-01-08 01:56:56 -05:00
|
|
|
$this->assertEquals('0 ura', I18n::_('%d hours', 0), '0 hours in Slowene');
|
|
|
|
$this->assertEquals('1 uri', I18n::_('%d hours', 1), '1 hour in Slowene');
|
|
|
|
$this->assertEquals('2 ure', I18n::_('%d hours', 2), '2 hours in Slowene');
|
|
|
|
$this->assertEquals('3 ur', I18n::_('%d hours', 3), '3 hours in Slowene');
|
|
|
|
$this->assertEquals('11 ura', I18n::_('%d hours', 11), '11 hours in Slowene');
|
|
|
|
$this->assertEquals('101 uri', I18n::_('%d hours', 101), '101 hours in Slowene');
|
|
|
|
$this->assertEquals('102 ure', I18n::_('%d hours', 102), '102 hours in Slowene');
|
|
|
|
$this->assertEquals('104 ur', I18n::_('%d hours', 104), '104 hours in Slowene');
|
2017-01-01 08:35:39 -05:00
|
|
|
}
|
|
|
|
|
2019-06-23 06:06:36 -04:00
|
|
|
public function testBrowserLanguageCsDetection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'cs;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
|
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertEquals('cs', I18n::getLanguage(), 'browser language cs');
|
2022-12-12 14:51:06 -05:00
|
|
|
$this->assertEquals('1 hodina', I18n::_('%d hours', 1), '1 hour in Czech');
|
2019-06-23 06:06:36 -04:00
|
|
|
$this->assertEquals('2 hodiny', I18n::_('%d hours', 2), '2 hours in Czech');
|
|
|
|
$this->assertEquals('5 minut', I18n::_('%d minutes', 5), '5 minutes in Czech');
|
|
|
|
$this->assertEquals('14 minut', I18n::_('%d minutes', 14), '14 minutes in Czech');
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:21:17 -04:00
|
|
|
public function testBrowserLanguageAnyDetection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
|
2016-08-09 05:54:42 -04:00
|
|
|
I18n::loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
$this->assertTrue(strlen(I18n::getLanguage()) >= 2, 'browser language any');
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testVariableInjection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
|
2016-08-09 05:54:42 -04:00
|
|
|
I18n::loadTranslations();
|
|
|
|
$this->assertEquals('some string + 1', I18n::_('some %s + %d', 'string', 1), 'browser language en');
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
2017-03-25 07:46:08 -04:00
|
|
|
|
2020-02-01 02:09:30 -05:00
|
|
|
public function testHtmlEntityEncoding()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
|
|
|
|
I18n::loadTranslations();
|
2020-02-01 03:39:14 -05:00
|
|
|
$input = '&<>"\'/`=';
|
2020-02-01 03:15:14 -05:00
|
|
|
$result = htmlspecialchars($input, ENT_QUOTES | ENT_HTML5 | ENT_DISALLOWED, 'UTF-8', false);
|
|
|
|
$this->assertEquals($result, I18n::encode($input), 'encodes HTML entities');
|
|
|
|
$this->assertEquals('<a>some ' . $result . ' + 1</a>', I18n::_('<a>some %s + %d</a>', $input, 1), 'encodes parameters in translations');
|
|
|
|
$this->assertEquals($result . $result, I18n::_($input . '%s', $input), 'encodes message ID as well, when no link');
|
2020-02-01 02:09:30 -05:00
|
|
|
}
|
|
|
|
|
2017-03-25 07:46:08 -04:00
|
|
|
public function testMessageIdsExistInAllLanguages()
|
|
|
|
{
|
|
|
|
$messageIds = array();
|
2017-03-25 08:19:11 -04:00
|
|
|
$languages = array();
|
|
|
|
$dir = dir(PATH . 'i18n');
|
2017-03-25 07:46:08 -04:00
|
|
|
while (false !== ($file = $dir->read())) {
|
|
|
|
if (strlen($file) === 7) {
|
2017-11-13 16:05:29 -05:00
|
|
|
$language = substr($file, 0, 2);
|
|
|
|
$languageMessageIds = array_keys(
|
2017-03-25 08:19:11 -04:00
|
|
|
json_decode(
|
|
|
|
file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . $file),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$messageIds = array_unique(array_merge($messageIds, $languageMessageIds));
|
|
|
|
$languages[$language] = $languageMessageIds;
|
2017-03-25 07:46:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($messageIds as $messageId) {
|
|
|
|
foreach (array_keys($languages) as $language) {
|
2017-03-25 08:19:11 -04:00
|
|
|
// most languages don't translate the data size units, ignore those
|
|
|
|
if ($messageId !== 'B' && strlen($messageId) !== 3 && strpos($messageId, 'B', 2) !== 2) {
|
2022-10-25 00:58:42 -04:00
|
|
|
$this->assertContains(
|
2017-03-25 08:19:11 -04:00
|
|
|
$messageId,
|
|
|
|
$languages[$language],
|
|
|
|
"message ID '$messageId' exists in translation file $language.json"
|
|
|
|
);
|
|
|
|
}
|
2017-03-25 07:46:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|