mirror of
				https://github.com/Icinga/icingaweb2.git
				synced 2025-10-29 18:34:03 +01:00 
			
		
		
		
	Translator::getPreferredLocaleCode($header) can now be used with the HTTP "Accept-Language" header to return the best matching locale using the user's preferations reported by the browser and our available locale stack. Additionally Translator::getLocale and Translator::getLanguage were replaced by Translator::splitLocaleCode to provide a more flexible implemenation in order to identify specific parts of a particular locale or the current one. refs #6074
		
			
				
	
	
		
			211 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			211 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| // {{{ICINGA_LICENSE_HEADER}}}
 | |
| // {{{ICINGA_LICENSE_HEADER}}}
 | |
| 
 | |
| namespace Tests\Icinga\Util;
 | |
| 
 | |
| use Exception;
 | |
| use Icinga\Test\BaseTestCase;
 | |
| use Icinga\Util\Translator;
 | |
| 
 | |
| class TranslatorWithHardcodedLocaleCodes extends Translator
 | |
| {
 | |
|     public static function getAvailableLocaleCodes()
 | |
|     {
 | |
|         return array('en_US', 'de_DE', 'de_AT');
 | |
|     }
 | |
| }
 | |
| 
 | |
| class TranslatorTest extends BaseTestCase
 | |
| {
 | |
|     public function setUp()
 | |
|     {
 | |
|         parent::setUp();
 | |
|         Translator::registerDomain('icingatest', BaseTestCase::$testDir . '/res/locale');
 | |
|     }
 | |
| 
 | |
|     public function testWhetherGetAvailableLocaleCodesReturnsAllAvailableLocaleCodes()
 | |
|     {
 | |
|         $this->assertEquals(
 | |
|             array('de_DE', 'fr_FR'),
 | |
|             Translator::getAvailableLocaleCodes(),
 | |
|             'Translator::getAvailableLocaleCodes does not return all available locale codes'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     public function testWhetherSetupLocaleSetsUpTheGivenLocale()
 | |
|     {
 | |
|         Translator::setupLocale('de_DE');
 | |
|         $this->assertContains(
 | |
|             setlocale(LC_ALL, 0),
 | |
|             array('de_DE', 'de_DE.UTF-8'),
 | |
|             'Translator::setupLocale does not properly set up a given locale'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @expectedException \Exception
 | |
|      */
 | |
|     public function testWhetherSetupLocaleThrowsAnExceptionWhenGivenAnInvalidLocale()
 | |
|     {
 | |
|         Translator::setupLocale('foobar');
 | |
|     }
 | |
| 
 | |
|     public function testWhetherSetupLocaleSetsCAsLocaleWhenGivenAnInvalidLocale()
 | |
|     {
 | |
|         try {
 | |
|             Translator::setupLocale('foobar');
 | |
|             $this->fail('Translator::setupLocale does not throw an exception when given an invalid locale');
 | |
|         } catch (Exception $e) {
 | |
|             $this->assertEquals(
 | |
|                 'C',
 | |
|                 setlocale(LC_ALL, 0),
 | |
|                 'Translator::setupLocale does not set C as locale in case the given one is invalid'
 | |
|             );
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function testWhetherTranslateReturnsTheCorrectMessageForTheCurrentLocale()
 | |
|     {
 | |
|         Translator::setupLocale('de_DE');
 | |
| 
 | |
|         $this->assertEquals(
 | |
|             'Lorem ipsum dolor sit amet!',
 | |
|             Translator::translate('Lorem ipsum dolor sit amet', 'icingatest'),
 | |
|             'Translator::translate does not translate the given message correctly to German'
 | |
|         );
 | |
| 
 | |
|         Translator::setupLocale('fr_FR');
 | |
| 
 | |
|         $this->assertEquals(
 | |
|             'Lorem ipsum dolor sit amet?',
 | |
|             Translator::translate('Lorem ipsum dolor sit amet', 'icingatest'),
 | |
|             'Translator::translate does not translate the given message correctly to French'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     public function testWhetherSplitLocaleCodeSplitsValidLocalesCorrectly()
 | |
|     {
 | |
|         $localeObj = Translator::splitLocaleCode('de_DE');
 | |
|         $this->assertEquals(
 | |
|             'de',
 | |
|             $localeObj->language,
 | |
|             'Translator::splitLocaleCode does not split the language code correctly'
 | |
|         );
 | |
|         $this->assertEquals(
 | |
|             'DE',
 | |
|             $localeObj->country,
 | |
|             'Translator::splitLocaleCode does not split the country code correctly'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @depends testWhetherSplitLocaleCodeSplitsValidLocalesCorrectly
 | |
|      */
 | |
|     public function testWhetherSplitLocaleCodeCanHandleEncodingSuffixes()
 | |
|     {
 | |
|         $this->assertEquals(
 | |
|             'US',
 | |
|             Translator::splitLocaleCode('en_US.UTF-8')->country,
 | |
|             'Translator::splitLocaleCode does not handle encoding suffixes correctly'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     public function testWhetherSplitLocaleCodeInterpretsInvalidLocaleCodesAsLanguageCodes()
 | |
|     {
 | |
|         $this->assertEquals(
 | |
|             'de',
 | |
|             Translator::splitLocaleCode('de')->language,
 | |
|             'Translator::splitLocaleCode does not interpret invalid locale codes as language codes'
 | |
|         );
 | |
|         $this->assertEquals(
 | |
|             'en~US',
 | |
|             Translator::splitLocaleCode('en~US')->language,
 | |
|             'Translator::splitLocaleCode does not interpret invalid locale codes as language codes'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @depends testWhetherSplitLocaleCodeSplitsValidLocalesCorrectly
 | |
|      */
 | |
|     public function testWhetherSplitLocaleCodeReturnsTheDefaultLocaleWhenGivenCAsLocale()
 | |
|     {
 | |
|         $cLocaleObj = Translator::splitLocaleCode('C');
 | |
|         $defaultLocaleObj = Translator::splitLocaleCode(Translator::DEFAULT_LOCALE);
 | |
|         $this->assertEquals(
 | |
|             $defaultLocaleObj->language,
 | |
|             $cLocaleObj->language,
 | |
|             'Translator::splitLocaleCode does not return the default language code when given C as locale'
 | |
|         );
 | |
|         $this->assertEquals(
 | |
|             $defaultLocaleObj->country,
 | |
|             $cLocaleObj->country,
 | |
|             'Translator::splitLocaleCode does not return the default country code when given C as locale'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @depends testWhetherSplitLocaleCodeSplitsValidLocalesCorrectly
 | |
|      * @depends testWhetherSplitLocaleCodeInterpretsInvalidLocaleCodesAsLanguageCodes
 | |
|      */
 | |
|     public function testWhetherGetPreferredLocaleCodeFavorsPerfectMatches()
 | |
|     {
 | |
|         $this->assertEquals(
 | |
|             'de_DE',
 | |
|             TranslatorWithHardcodedLocaleCodes::getPreferredLocaleCode('jp,de_DE;q=0.8,de;q=0.6'),
 | |
|             'Translator::getPreferredLocaleCode does not favor perfect matches'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @depends testWhetherSplitLocaleCodeSplitsValidLocalesCorrectly
 | |
|      * @depends testWhetherSplitLocaleCodeInterpretsInvalidLocaleCodesAsLanguageCodes
 | |
|      */
 | |
|     public function testWhetherGetPreferredLocaleCodeReturnsThePreferredSimilarMatchEvenThoughAPerfectMatchWasFound()
 | |
|     {
 | |
|         $this->assertEquals(
 | |
|             'de_DE',
 | |
|             TranslatorWithHardcodedLocaleCodes::getPreferredLocaleCode('de_CH,en_US;q=0.8'),
 | |
|             'Translator::getPreferredLocaleCode does not return the preferred similar match'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @depends testWhetherSplitLocaleCodeSplitsValidLocalesCorrectly
 | |
|      * @depends testWhetherSplitLocaleCodeInterpretsInvalidLocaleCodesAsLanguageCodes
 | |
|      */
 | |
|     public function testWhetherGetPreferredLocaleCodeReturnsAPerfectMatchEvenThoughASimilarMatchWasFound()
 | |
|     {
 | |
|         $this->assertEquals(
 | |
|             'de_AT',
 | |
|             TranslatorWithHardcodedLocaleCodes::getPreferredLocaleCode('de,de_AT;q=0.5'),
 | |
|             'Translator::getPreferredLocaleCode does not return a perfect '
 | |
|             . 'match if a similar match with higher priority was found'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @depends testWhetherSplitLocaleCodeInterpretsInvalidLocaleCodesAsLanguageCodes
 | |
|      */
 | |
|     public function testWhetherGetPreferredLocaleCodeReturnsASimilarMatchIfNoPerfectMatchCouldBeFound()
 | |
|     {
 | |
|         $this->assertEquals(
 | |
|             'de_DE',
 | |
|             TranslatorWithHardcodedLocaleCodes::getPreferredLocaleCode('de,en'),
 | |
|             'Translator::getPreferredLocaleCode does not return the most preferred similar match'
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @depends testWhetherSplitLocaleCodeSplitsValidLocalesCorrectly
 | |
|      */
 | |
|     public function testWhetherGetPreferredLocaleCodeReturnsTheDefaultLocaleIfNoMatchCouldBeFound()
 | |
|     {
 | |
|         $this->assertEquals(
 | |
|             Translator::DEFAULT_LOCALE,
 | |
|             TranslatorWithHardcodedLocaleCodes::getPreferredLocaleCode('fr_FR,jp_JP'),
 | |
|             'Translator::getPreferredLocaleCode does not return the default locale if no match could be found'
 | |
|         );
 | |
|     }
 | |
| }
 |