2008-03-20 04:00:49 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\i18n\Tests;
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
use InvalidArgumentException;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Core\Convert;
|
2017-01-18 04:58:48 +01:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\i18n\i18n;
|
2017-01-18 04:58:48 +01:00
|
|
|
use SilverStripe\i18n\Messages\MessageProvider;
|
|
|
|
use SilverStripe\i18n\Messages\Symfony\SymfonyMessageProvider;
|
2024-09-23 04:31:50 +02:00
|
|
|
use SilverStripe\Model\ArrayData;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\SSViewer;
|
2024-09-18 03:53:44 +02:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2011-11-26 14:34:11 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class i18nTest extends SapphireTest
|
|
|
|
{
|
2017-01-18 04:58:48 +01:00
|
|
|
use i18nTestManifest;
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2017-01-18 04:58:48 +01:00
|
|
|
$this->setupManifest();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function tearDown(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-01-18 04:58:48 +01:00
|
|
|
$this->tearDownManifest();
|
2016-12-16 05:34:21 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetExistingTranslations()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$translations = i18n::getSources()->getKnownLocales();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(isset($translations['en_US']), 'Checking for en translation');
|
|
|
|
$this->assertEquals($translations['en_US'], 'English (United States)');
|
|
|
|
$this->assertTrue(isset($translations['de_DE']), 'Checking for de_DE translation');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetClosestTranslation()
|
|
|
|
{
|
|
|
|
// Validate necessary assumptions for this test
|
2017-01-18 04:58:48 +01:00
|
|
|
// As per set of locales loaded from _fakewebroot
|
2017-01-26 05:20:08 +01:00
|
|
|
$translations = i18n::getSources()->getKnownLocales();
|
2017-01-18 04:58:48 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'en_GB',
|
|
|
|
'en_US',
|
|
|
|
'fr_FR',
|
|
|
|
'de_AT',
|
|
|
|
'de_DE',
|
2017-01-25 04:35:13 +01:00
|
|
|
'ja_JP',
|
2017-01-26 05:20:08 +01:00
|
|
|
'mi_NZ',
|
2017-01-25 04:35:13 +01:00
|
|
|
'pl_PL',
|
2017-01-18 04:58:48 +01:00
|
|
|
'es_AR',
|
|
|
|
'es_ES',
|
|
|
|
],
|
2022-04-14 03:12:59 +02:00
|
|
|
array_keys($translations ?? [])
|
2017-01-18 04:58:48 +01:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Test indeterminate locales
|
|
|
|
$this->assertEmpty(i18n::get_closest_translation('zz_ZZ'));
|
|
|
|
|
|
|
|
// Test english fallback
|
|
|
|
$this->assertEquals('en_US', i18n::get_closest_translation('en_US'));
|
|
|
|
$this->assertEquals('en_GB', i18n::get_closest_translation('en_GB'));
|
|
|
|
$this->assertEquals('en_US', i18n::get_closest_translation('en_ZZ'));
|
|
|
|
|
|
|
|
// Test spanish fallbacks
|
|
|
|
$this->assertEquals('es_AR', i18n::get_closest_translation('es_AR'));
|
|
|
|
$this->assertEquals('es_ES', i18n::get_closest_translation('es_ES'));
|
|
|
|
$this->assertEquals('es_ES', i18n::get_closest_translation('es_XX'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDataObjectFieldLabels()
|
|
|
|
{
|
|
|
|
i18n::set_locale('de_DE');
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// Load into the translator as a literal array data source
|
|
|
|
/** @var SymfonyMessageProvider $provider */
|
|
|
|
$provider = Injector::inst()->get(MessageProvider::class);
|
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
2018-01-16 19:39:30 +01:00
|
|
|
[ i18nTest\TestDataObject::class . '.MyProperty' => 'MyProperty' ],
|
2016-12-16 05:34:21 +01:00
|
|
|
'en_US'
|
|
|
|
);
|
2017-01-18 04:58:48 +01:00
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
2018-01-16 19:39:30 +01:00
|
|
|
[ i18nTest\TestDataObject::class . '.MyProperty' => 'Mein Attribut' ],
|
2016-12-16 05:34:21 +01:00
|
|
|
'de_DE'
|
|
|
|
);
|
2017-01-18 04:58:48 +01:00
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
2018-01-16 19:39:30 +01:00
|
|
|
[ i18nTest\TestDataObject::class . '.MyUntranslatedProperty' => 'Mein Attribut' ],
|
2017-01-18 04:58:48 +01:00
|
|
|
'en_US'
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// Test field labels
|
|
|
|
$obj = new i18nTest\TestDataObject();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-04-27 06:54:42 +02:00
|
|
|
'Mein Attribut',
|
|
|
|
$obj->fieldLabel('MyProperty')
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2019-01-07 17:26:14 +01:00
|
|
|
'My untranslated property',
|
2017-04-27 06:54:42 +02:00
|
|
|
$obj->fieldLabel('MyUntranslatedProperty')
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testProvideI18nEntities()
|
|
|
|
{
|
2017-01-18 04:58:48 +01:00
|
|
|
/** @var SymfonyMessageProvider $provider */
|
|
|
|
$provider = Injector::inst()->get(MessageProvider::class);
|
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
2018-01-16 19:39:30 +01:00
|
|
|
[ i18nTest\TestObject::class . '.MyProperty' => 'Untranslated' ],
|
2016-12-16 05:34:21 +01:00
|
|
|
'en_US'
|
|
|
|
);
|
2017-01-18 04:58:48 +01:00
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
2018-01-16 19:39:30 +01:00
|
|
|
[ i18nTest\TestObject::class . '.my_translatable_property' => 'Übersetzt' ],
|
2016-12-16 05:34:21 +01:00
|
|
|
'de_DE'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
i18nTest\TestObject::$my_translatable_property,
|
|
|
|
'Untranslated'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
i18nTest\TestObject::my_translatable_property(),
|
|
|
|
'Untranslated'
|
|
|
|
);
|
|
|
|
|
|
|
|
i18n::set_locale('en_US');
|
|
|
|
$this->assertEquals(
|
|
|
|
i18nTest\TestObject::my_translatable_property(),
|
|
|
|
'Untranslated',
|
|
|
|
'Getter returns original static value when called in default locale'
|
|
|
|
);
|
|
|
|
|
|
|
|
i18n::set_locale('de_DE');
|
|
|
|
$this->assertEquals(
|
|
|
|
i18nTest\TestObject::my_translatable_property(),
|
|
|
|
'Übersetzt',
|
|
|
|
'Getter returns translated value when called in another locale'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTemplateTranslation()
|
|
|
|
{
|
|
|
|
$oldLocale = i18n::get_locale();
|
2022-11-15 06:20:54 +01:00
|
|
|
i18n::config()->set('missing_default_warning', false);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
/** @var SymfonyMessageProvider $provider */
|
|
|
|
$provider = Injector::inst()->get(MessageProvider::class);
|
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
'i18nTestModule.MAINTEMPLATE' => 'Main Template',
|
2017-02-04 20:41:31 +01:00
|
|
|
'REPLACEMENTNONAMESPACE' => 'My replacement no namespace: {replacement}',
|
2017-01-18 04:58:48 +01:00
|
|
|
'i18nTestModule.LAYOUTTEMPLATE' => 'Layout Template',
|
2017-02-04 20:41:31 +01:00
|
|
|
'LAYOUTTEMPLATENONAMESPACE' => 'Layout Template no namespace',
|
|
|
|
'i18nTestModule.REPLACEMENTNAMESPACE' => 'My replacement: {replacement}',
|
2017-01-18 04:58:48 +01:00
|
|
|
'i18nTestModule.WITHNAMESPACE' => 'Include Entity with Namespace',
|
2017-02-04 20:41:31 +01:00
|
|
|
'NONAMESPACE' => 'Include Entity without Namespace',
|
|
|
|
'i18nTestModuleInclude_ss.REPLACEMENTINCLUDENAMESPACE' => 'My include replacement: {replacement}',
|
|
|
|
'REPLACEMENTINCLUDENONAMESPACE' => 'My include replacement no namespace: {replacement}'
|
2017-01-18 04:58:48 +01:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
'en_US'
|
|
|
|
);
|
|
|
|
|
|
|
|
$viewer = new SSViewer('i18nTestModule');
|
2017-01-18 04:58:48 +01:00
|
|
|
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData([
|
|
|
|
'TestProperty' => 'TestPropertyValue'
|
|
|
|
])));
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("Layout Template\n"),
|
|
|
|
$parsedHtml
|
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("Layout Template no namespace\n"),
|
|
|
|
$parsedHtml
|
|
|
|
);
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
'i18nTestModule.MAINTEMPLATE' => 'TRANS Main Template',
|
2017-02-04 20:41:31 +01:00
|
|
|
'REPLACEMENTNONAMESPACE' => 'TRANS My replacement no namespace: {replacement}',
|
2017-01-18 04:58:48 +01:00
|
|
|
'i18nTestModule.LAYOUTTEMPLATE' => 'TRANS Layout Template',
|
2017-02-04 20:41:31 +01:00
|
|
|
'LAYOUTTEMPLATENONAMESPACE' => 'TRANS Layout Template no namespace',
|
|
|
|
'i18nTestModule.REPLACEMENTNAMESPACE' => 'TRANS My replacement: {replacement}',
|
2017-01-18 04:58:48 +01:00
|
|
|
'i18nTestModule.WITHNAMESPACE' => 'TRANS Include Entity with Namespace',
|
2017-02-04 20:41:31 +01:00
|
|
|
'NONAMESPACE' => 'TRANS Include Entity without Namespace',
|
|
|
|
'i18nTestModuleInclude_ss.REPLACEMENTINCLUDENAMESPACE' => 'TRANS My include replacement: {replacement}',
|
|
|
|
'REPLACEMENTINCLUDENONAMESPACE' => 'TRANS My include replacement no namespace: {replacement}',
|
2017-01-18 04:58:48 +01:00
|
|
|
'i18nTestModule.PLURALS' => 'An item|{count} items',
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
'de_DE'
|
|
|
|
);
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
i18n::set_locale('de_DE');
|
2016-12-16 05:34:21 +01:00
|
|
|
$viewer = new SSViewer('i18nTestModule');
|
2020-04-20 19:58:09 +02:00
|
|
|
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData(['TestProperty' => 'TestPropertyValue'])));
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("TRANS Main Template\n"),
|
|
|
|
$parsedHtml
|
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("TRANS Layout Template\n"),
|
|
|
|
$parsedHtml
|
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("TRANS Layout Template no namespace\n"),
|
|
|
|
$parsedHtml
|
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("TRANS My replacement: TestPropertyValue\n"),
|
|
|
|
$parsedHtml
|
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("TRANS Include Entity with Namespace\n"),
|
|
|
|
$parsedHtml
|
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("TRANS Include Entity without Namespace\n"),
|
|
|
|
$parsedHtml
|
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("TRANS My include replacement: TestPropertyValue\n"),
|
|
|
|
$parsedHtml
|
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("TRANS My include replacement no namespace: TestPropertyValue\n"),
|
|
|
|
$parsedHtml
|
|
|
|
);
|
2017-01-18 04:58:48 +01:00
|
|
|
// Check plurals
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('Single: An item', $parsedHtml);
|
|
|
|
$this->assertStringContainsString('Multiple: 4 items', $parsedHtml);
|
|
|
|
$this->assertStringContainsString('None: 0 items', $parsedHtml);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
i18n::set_locale($oldLocale);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewTMethodSignature()
|
|
|
|
{
|
2017-01-18 04:58:48 +01:00
|
|
|
/** @var SymfonyMessageProvider $provider */
|
|
|
|
$provider = Injector::inst()->get(MessageProvider::class);
|
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
'i18nTestModule.NEWMETHODSIG' => 'TRANS New _t method signature test',
|
2017-02-04 20:41:31 +01:00
|
|
|
'i18nTestModule.INJECTIONS' => 'TRANS Hello {name} {greeting}. But it is late, {goodbye}'
|
2017-01-18 04:58:48 +01:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
'en_US'
|
|
|
|
);
|
|
|
|
|
|
|
|
$entity = "i18nTestModule.INJECTIONS";
|
|
|
|
$default = "Hello {name} {greeting}. But it is late, {goodbye}";
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// Test missing entity key
|
2016-12-16 05:34:21 +01:00
|
|
|
$translated = i18n::_t(
|
2018-01-16 19:39:30 +01:00
|
|
|
$entity . '_DOES_NOT_EXIST',
|
2016-12-16 05:34:21 +01:00
|
|
|
$default,
|
2020-04-20 19:58:09 +02:00
|
|
|
["name"=>"Mark", "greeting"=>"welcome", "goodbye"=>"bye"]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
"Hello Mark welcome. But it is late, bye",
|
|
|
|
$translated,
|
|
|
|
"Testing fallback to the translation default (but using the injection array)"
|
|
|
|
);
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// Test standard injection
|
2016-12-16 05:34:21 +01:00
|
|
|
$translated = i18n::_t(
|
|
|
|
$entity,
|
|
|
|
$default,
|
2017-01-18 04:58:48 +01:00
|
|
|
["name"=>"Paul", "greeting"=>"good you are here", "goodbye"=>"see you"]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
"TRANS Hello Paul good you are here. But it is late, see you",
|
|
|
|
$translated,
|
|
|
|
"Testing entity, default string and injection array"
|
|
|
|
);
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// @deprecated 5.0 Passing in context
|
2016-12-16 05:34:21 +01:00
|
|
|
$translated = i18n::_t(
|
|
|
|
$entity,
|
|
|
|
$default,
|
|
|
|
"New context (this should be ignored)",
|
2017-01-18 04:58:48 +01:00
|
|
|
["name"=>"Steffen", "greeting"=>"willkommen", "goodbye"=>"wiedersehen"]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
"TRANS Hello Steffen willkommen. But it is late, wiedersehen",
|
|
|
|
$translated,
|
|
|
|
"Full test of translation, using default, context and injection array"
|
|
|
|
);
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// Passing in non-associative arrays for placeholders is now an error
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->expectExceptionMessage(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Injection must be an associative array');
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
i18n::_t(
|
|
|
|
$entity, // has {name} placeholders
|
|
|
|
$default,
|
|
|
|
["Cat", "meow", "meow"]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See @i18nTestModule.ss for the template that is being used for this test
|
|
|
|
* */
|
|
|
|
public function testNewTemplateTranslation()
|
|
|
|
{
|
2022-11-15 06:20:54 +01:00
|
|
|
i18n::config()->set('missing_default_warning', false);
|
2017-01-25 04:35:13 +01:00
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
/** @var SymfonyMessageProvider $provider */
|
|
|
|
$provider = Injector::inst()->get(MessageProvider::class);
|
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
'i18nTestModule.NEWMETHODSIG' => 'TRANS New _t method signature test',
|
|
|
|
'i18nTestModule.INJECTIONS' => 'TRANS Hello {name} {greeting}. But it is late, {goodbye}'
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
'en_US'
|
|
|
|
);
|
|
|
|
|
|
|
|
$viewer = new SSViewer('i18nTestModule');
|
2017-01-18 04:58:48 +01:00
|
|
|
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData(['TestProperty' => 'TestPropertyValue'])));
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("Hello Mark welcome. But it is late, bye\n"),
|
|
|
|
$parsedHtml,
|
|
|
|
"Testing fallback to the translation default (but using the injection array)"
|
|
|
|
);
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os("TRANS Hello Paul good you are here. But it is late, see you\n"),
|
|
|
|
$parsedHtml,
|
|
|
|
"Testing entity, default string and injection array"
|
|
|
|
);
|
|
|
|
|
|
|
|
//test injected calls
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(
|
2016-12-16 05:34:21 +01:00
|
|
|
Convert::nl2os(
|
2018-01-16 19:39:30 +01:00
|
|
|
"TRANS Hello " . Director::absoluteBaseURL() . " " . i18n::get_locale() . ". But it is late, global calls\n"
|
2016-12-16 05:34:21 +01:00
|
|
|
),
|
|
|
|
$parsedHtml,
|
|
|
|
"Testing a translation with just entity and injection array, but with global variables injected in"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetLocaleFromLang()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertEquals('en_US', i18n::getData()->localeFromLang('en'));
|
|
|
|
$this->assertEquals('de_DE', i18n::getData()->localeFromLang('de_DE'));
|
|
|
|
$this->assertEquals('xy_XY', i18n::getData()->localeFromLang('xy'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateLocale()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertTrue(i18n::getData()->validate('en_US'), 'Known locale in underscore format is valid');
|
|
|
|
$this->assertTrue(i18n::getData()->validate('en-US'), 'Known locale in dash format is valid');
|
|
|
|
$this->assertFalse(i18n::getData()->validate('en'), 'Short lang format is not valid');
|
|
|
|
$this->assertFalse(i18n::getData()->validate('xx_XX'), 'Unknown locale in correct format is not valid');
|
|
|
|
$this->assertFalse(i18n::getData()->validate(''), 'Empty string is not valid');
|
2017-04-27 08:31:03 +02:00
|
|
|
$this->assertTrue(i18n::getData()->validate('de_DE'), 'Known locale where language is same as region');
|
|
|
|
$this->assertTrue(i18n::getData()->validate('fr-FR'), 'Known locale where language is same as region');
|
|
|
|
$this->assertTrue(i18n::getData()->validate('zh_cmn'), 'Known locale with all lowercase letters');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTranslate()
|
|
|
|
{
|
2017-01-18 04:58:48 +01:00
|
|
|
/** @var SymfonyMessageProvider $provider */
|
|
|
|
$provider = Injector::inst()->get(MessageProvider::class);
|
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
|
|
|
[ 'i18nTestModule.ENTITY' => 'Entity with "Double Quotes"' ],
|
2016-12-16 05:34:21 +01:00
|
|
|
'en_US'
|
|
|
|
);
|
2017-01-18 04:58:48 +01:00
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
'i18nTestModule.ENTITY' => 'Entity with "Double Quotes" (de)',
|
|
|
|
'i18nTestModule.ADDITION' => 'Addition (de)',
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
'de'
|
|
|
|
);
|
2017-01-18 04:58:48 +01:00
|
|
|
$provider->getTranslator()->addResource(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
'i18nTestModule.ENTITY' => 'Entity with "Double Quotes" (de_AT)',
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
'de_AT'
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Entity with "Double Quotes"',
|
2017-01-18 04:58:48 +01:00
|
|
|
i18n::_t('i18nTestModule.ENTITY', 'Ignored default'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Returns translation in default language'
|
|
|
|
);
|
|
|
|
|
|
|
|
i18n::set_locale('de');
|
|
|
|
$this->assertEquals(
|
|
|
|
'Entity with "Double Quotes" (de)',
|
2017-01-18 04:58:48 +01:00
|
|
|
i18n::_t('i18nTestModule.ENTITY', 'Entity with "Double Quotes"'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Returns translation according to current locale'
|
|
|
|
);
|
|
|
|
|
|
|
|
i18n::set_locale('de_AT');
|
|
|
|
$this->assertEquals(
|
|
|
|
'Entity with "Double Quotes" (de_AT)',
|
2017-01-18 04:58:48 +01:00
|
|
|
i18n::_t('i18nTestModule.ENTITY', 'Entity with "Double Quotes"'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Returns specific regional translation if available'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Addition (de)',
|
2017-01-18 04:58:48 +01:00
|
|
|
i18n::_t('i18nTestModule.ADDITION', 'Addition'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Returns fallback non-regional translation if regional is not available'
|
|
|
|
);
|
|
|
|
|
|
|
|
i18n::set_locale('fr');
|
|
|
|
$this->assertEquals(
|
2017-01-18 04:58:48 +01:00
|
|
|
'Entity with "Double Quotes" (fr)',
|
|
|
|
i18n::_t('i18nTestModule.ENTITY', 'Entity with "Double Quotes"'),
|
|
|
|
'Non-specific locales fall back to language-only localisations'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-18 03:53:44 +02:00
|
|
|
public static function pluralisationDataProvider()
|
2017-01-25 04:35:13 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
// English - 2 plural forms
|
|
|
|
['en_NZ', 0, '0 months'],
|
|
|
|
['en_NZ', 1, 'A month'],
|
|
|
|
['en_NZ', 2, '2 months'],
|
|
|
|
['en_NZ', 5, '5 months'],
|
|
|
|
['en_NZ', 10, '10 months'],
|
|
|
|
// Polish - 4 plural forms
|
|
|
|
['pl_PL', 0, '0 miesięcy'],
|
|
|
|
['pl_PL', 1, '1 miesiąc'],
|
|
|
|
['pl_PL', 2, '2 miesiące'],
|
|
|
|
['pl_PL', 5, '5 miesięcy'],
|
|
|
|
['pl_PL', 10, '10 miesięcy'],
|
|
|
|
// Japanese - 1 plural form
|
|
|
|
['ja_JP', 0, '0日'],
|
|
|
|
['ja_JP', 1, '1日'],
|
|
|
|
['ja_JP', 2, '2日'],
|
|
|
|
['ja_JP', 5, '5日'],
|
|
|
|
['ja_JP', 10, '10日'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $locale
|
|
|
|
* @param int $count
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
2024-09-18 03:53:44 +02:00
|
|
|
#[DataProvider('pluralisationDataProvider')]
|
2017-01-25 04:35:13 +01:00
|
|
|
public function testPluralisation($locale, $count, $expected)
|
|
|
|
{
|
|
|
|
i18n::set_locale($locale);
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
_t('Month.PLURALS', 'A month|{count} months', ['count' => $count]),
|
|
|
|
"Plural form in locale $locale with count $count should be $expected"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testGetLanguageName()
|
|
|
|
{
|
2022-11-15 06:20:54 +01:00
|
|
|
i18n::config()->merge(
|
2016-12-16 05:34:21 +01:00
|
|
|
'common_languages',
|
2020-04-20 19:58:09 +02:00
|
|
|
['de_CGN' => ['name' => 'German (Cologne)', 'native' => 'Kölsch']]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertEquals('German', i18n::getData()->languageName('de_CGN'));
|
|
|
|
$this->assertEquals('Deutsch', i18n::with_locale('de_CGN', function () {
|
|
|
|
return i18n::getData()->languageName('de_CGN');
|
|
|
|
}));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2008-10-29 22:07:17 +01:00
|
|
|
}
|