2015-09-05 00:24:56 +00:00
|
|
|
<?php
|
2016-07-21 15:09:48 +00:00
|
|
|
|
2016-08-09 09:54:42 +00:00
|
|
|
use PrivateBin\I18n;
|
2016-07-21 15:09:48 +00:00
|
|
|
|
2016-08-09 09:54:42 +00:00
|
|
|
class I18nTest extends PHPUnit_Framework_TestCase
|
2015-09-05 00:24:56 +00:00
|
|
|
{
|
|
|
|
private $_translations = array();
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
|
|
|
$this->_translations = json_decode(
|
|
|
|
file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . 'de.json'),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTranslationFallback()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
|
2016-10-29 08:24:08 +00:00
|
|
|
$messageId = 'It does not matter if the message ID exists';
|
2016-08-09 09:54:42 +00:00
|
|
|
I18n::loadTranslations();
|
|
|
|
$this->assertEquals($messageId, I18n::_($messageId), 'fallback to en');
|
2015-09-05 00:24:56 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 15:23:10 +00:00
|
|
|
public function testCookieLanguageDeDetection()
|
|
|
|
{
|
|
|
|
$_COOKIE['lang'] = 'de';
|
2016-08-09 09:54:42 +00:00
|
|
|
I18n::loadTranslations();
|
|
|
|
$this->assertEquals($this->_translations['en'], I18n::_('en'), 'browser language de');
|
|
|
|
$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 french');
|
2015-09-19 15:23:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:21:17 +00:00
|
|
|
public function testBrowserLanguageDeDetection()
|
2015-09-05 00:24:56 +00: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';
|
2016-08-09 09:54:42 +00:00
|
|
|
I18n::loadTranslations();
|
|
|
|
$this->assertEquals($this->_translations['en'], I18n::_('en'), 'browser language de');
|
|
|
|
$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 french');
|
2015-09-06 17:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBrowserLanguageFrDetection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-CH,fr;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
|
2016-08-09 09:54:42 +00:00
|
|
|
I18n::loadTranslations();
|
|
|
|
$this->assertEquals('fr', I18n::_('en'), 'browser language fr');
|
|
|
|
$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');
|
2015-09-06 17:21:17 +00: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 09:54:42 +00:00
|
|
|
I18n::loadTranslations();
|
|
|
|
$this->assertEquals('pl', I18n::_('en'), 'browser language pl');
|
2016-12-25 10:37:45 +00: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');
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
$this->assertEquals('ru', I18n::_('en'), 'browser language ru');
|
|
|
|
$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 17:21:17 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 13:35:39 +00: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();
|
|
|
|
$this->assertEquals('sl', I18n::_('en'), 'browser language sl');
|
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:21:17 +00:00
|
|
|
public function testBrowserLanguageAnyDetection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
|
2016-08-09 09:54:42 +00:00
|
|
|
I18n::loadTranslations();
|
|
|
|
$this->assertTrue(strlen(I18n::_('en')) == 2, 'browser language any');
|
2015-09-05 00:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testVariableInjection()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
|
2016-08-09 09:54:42 +00:00
|
|
|
I18n::loadTranslations();
|
|
|
|
$this->assertEquals('some string + 1', I18n::_('some %s + %d', 'string', 1), 'browser language en');
|
2015-09-05 00:24:56 +00:00
|
|
|
}
|
|
|
|
}
|