mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
8a07c56bdf
API Implement enhanced pluralisation Remove Zend_Translate and all Zend dependencies from i18n Deprecated $context from i18n::_t() Warn on missing default string for i18n::_t()
445 lines
16 KiB
PHP
445 lines
16 KiB
PHP
<?php
|
|
|
|
namespace SilverStripe\i18n\Tests;
|
|
|
|
use InvalidArgumentException;
|
|
use SilverStripe\Control\Director;
|
|
use SilverStripe\Core\Convert;
|
|
use SilverStripe\Core\Injector\Injector;
|
|
use SilverStripe\Dev\SapphireTest;
|
|
use SilverStripe\i18n\i18n;
|
|
use SilverStripe\i18n\Messages\MessageProvider;
|
|
use SilverStripe\i18n\Messages\Symfony\SymfonyMessageProvider;
|
|
use SilverStripe\View\ArrayData;
|
|
use SilverStripe\View\SSViewer;
|
|
|
|
class i18nTest extends SapphireTest
|
|
{
|
|
use i18nTestManifest;
|
|
|
|
public function setUp()
|
|
{
|
|
parent::setUp();
|
|
$this->setupManifest();
|
|
}
|
|
|
|
public function tearDown()
|
|
{
|
|
$this->tearDownManifest();
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function testGetExistingTranslations()
|
|
{
|
|
$translations = i18n::get_existing_translations();
|
|
$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
|
|
// As per set of locales loaded from _fakewebroot
|
|
$translations = i18n::get_existing_translations();
|
|
$this->assertEquals(
|
|
[
|
|
'en_GB',
|
|
'en_US',
|
|
'fr_FR',
|
|
'de_AT',
|
|
'de_DE',
|
|
'es_AR',
|
|
'es_ES',
|
|
'mi_NZ',
|
|
],
|
|
array_keys($translations)
|
|
);
|
|
|
|
// 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');
|
|
|
|
// Load into the translator as a literal array data source
|
|
/** @var SymfonyMessageProvider $provider */
|
|
$provider = Injector::inst()->get(MessageProvider::class);
|
|
$provider->getTranslator()->addResource(
|
|
'array',
|
|
[ 'i18nTest_DataObject.MyProperty' => 'MyProperty' ],
|
|
'en_US'
|
|
);
|
|
$provider->getTranslator()->addResource(
|
|
'array',
|
|
[ 'i18nTest_DataObject.MyProperty' => 'Mein Attribut' ],
|
|
'de_DE'
|
|
);
|
|
$provider->getTranslator()->addResource(
|
|
'array',
|
|
[ 'i18nTest_DataObject.MyUntranslatedProperty' => 'Mein Attribut' ],
|
|
'en_US'
|
|
);
|
|
|
|
// Test field labels
|
|
$obj = new i18nTest\TestDataObject();
|
|
$this->assertEquals(
|
|
$obj->fieldLabel('MyProperty'),
|
|
'Mein Attribut'
|
|
);
|
|
$this->assertEquals(
|
|
$obj->fieldLabel('MyUntranslatedProperty'),
|
|
'My Untranslated Property'
|
|
);
|
|
}
|
|
|
|
public function testProvideI18nEntities()
|
|
{
|
|
/** @var SymfonyMessageProvider $provider */
|
|
$provider = Injector::inst()->get(MessageProvider::class);
|
|
$provider->getTranslator()->addResource(
|
|
'array',
|
|
[ 'i18nTest_Object.MyProperty' => 'Untranslated' ],
|
|
'en_US'
|
|
);
|
|
$provider->getTranslator()->addResource(
|
|
'array',
|
|
[ 'i18nTest_Object.my_translatable_property' => 'Übersetzt' ],
|
|
'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();
|
|
|
|
/** @var SymfonyMessageProvider $provider */
|
|
$provider = Injector::inst()->get(MessageProvider::class);
|
|
$provider->getTranslator()->addResource(
|
|
'array',
|
|
[
|
|
'i18nTestModule.MAINTEMPLATE' => 'Main Template',
|
|
'i18nTestModule.ss.SPRINTFNONAMESPACE' => 'My replacement no namespace: %s',
|
|
'i18nTestModule.LAYOUTTEMPLATE' => 'Layout Template',
|
|
'i18nTestModule.ss.LAYOUTTEMPLATENONAMESPACE' => 'Layout Template no namespace',
|
|
'i18nTestModule.SPRINTFNAMESPACE' => 'My replacement: %s',
|
|
'i18nTestModule.WITHNAMESPACE' => 'Include Entity with Namespace',
|
|
'i18nTestModuleInclude.ss.NONAMESPACE' => 'Include Entity without Namespace',
|
|
'i18nTestModuleInclude.ss.SPRINTFINCLUDENAMESPACE' => 'My include replacement: %s',
|
|
'i18nTestModuleInclude.ss.SPRINTFINCLUDENONAMESPACE' => 'My include replacement no namespace: %s'
|
|
],
|
|
'en_US'
|
|
);
|
|
|
|
$viewer = new SSViewer('i18nTestModule');
|
|
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData([
|
|
'TestProperty' => 'TestPropertyValue'
|
|
])));
|
|
$this->assertContains(
|
|
Convert::nl2os("Layout Template\n"),
|
|
$parsedHtml
|
|
);
|
|
$this->assertContains(
|
|
Convert::nl2os("Layout Template no namespace\n"),
|
|
$parsedHtml
|
|
);
|
|
|
|
$provider->getTranslator()->addResource(
|
|
'array',
|
|
[
|
|
'i18nTestModule.MAINTEMPLATE' => 'TRANS Main Template',
|
|
'i18nTestModule.ss.SPRINTFNONAMESPACE' => 'TRANS My replacement no namespace: %s',
|
|
'i18nTestModule.LAYOUTTEMPLATE' => 'TRANS Layout Template',
|
|
'i18nTestModule.ss.LAYOUTTEMPLATENONAMESPACE' => 'TRANS Layout Template no namespace',
|
|
'i18nTestModule.SPRINTFNAMESPACE' => 'TRANS My replacement: %s',
|
|
'i18nTestModule.WITHNAMESPACE' => 'TRANS Include Entity with Namespace',
|
|
'i18nTestModuleInclude.ss.NONAMESPACE' => 'TRANS Include Entity without Namespace',
|
|
'i18nTestModuleInclude.ss.SPRINTFINCLUDENAMESPACE' => 'TRANS My include replacement: %s',
|
|
'i18nTestModuleInclude.ss.SPRINTFINCLUDENONAMESPACE' => 'TRANS My include replacement no namespace: %s',
|
|
'i18nTestModule.PLURALS' => 'An item|{count} items',
|
|
],
|
|
'de_DE'
|
|
);
|
|
|
|
i18n::set_locale('de_DE');
|
|
$viewer = new SSViewer('i18nTestModule');
|
|
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData(array('TestProperty' => 'TestPropertyValue'))));
|
|
$this->assertContains(
|
|
Convert::nl2os("TRANS Main Template\n"),
|
|
$parsedHtml
|
|
);
|
|
$this->assertContains(
|
|
Convert::nl2os("TRANS Layout Template\n"),
|
|
$parsedHtml
|
|
);
|
|
$this->assertContains(
|
|
Convert::nl2os("TRANS Layout Template no namespace\n"),
|
|
$parsedHtml
|
|
);
|
|
$this->assertContains(
|
|
Convert::nl2os("TRANS My replacement: TestPropertyValue\n"),
|
|
$parsedHtml
|
|
);
|
|
$this->assertContains(
|
|
Convert::nl2os("TRANS Include Entity with Namespace\n"),
|
|
$parsedHtml
|
|
);
|
|
$this->assertContains(
|
|
Convert::nl2os("TRANS Include Entity without Namespace\n"),
|
|
$parsedHtml
|
|
);
|
|
$this->assertContains(
|
|
Convert::nl2os("TRANS My include replacement: TestPropertyValue\n"),
|
|
$parsedHtml
|
|
);
|
|
$this->assertContains(
|
|
Convert::nl2os("TRANS My include replacement no namespace: TestPropertyValue\n"),
|
|
$parsedHtml
|
|
);
|
|
// Check plurals
|
|
$this->assertContains('Single: An item', $parsedHtml);
|
|
$this->assertContains('Multiple: 4 items', $parsedHtml);
|
|
$this->assertContains('None: 0 items', $parsedHtml);
|
|
|
|
i18n::set_locale($oldLocale);
|
|
}
|
|
|
|
public function testNewTMethodSignature()
|
|
{
|
|
/** @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}',
|
|
'i18nTestModule.INJECTIONSLEGACY' => 'TRANS Hello %s %s. But it is late, %s',
|
|
],
|
|
'en_US'
|
|
);
|
|
|
|
$entity = "i18nTestModule.INJECTIONS";
|
|
$default = "Hello {name} {greeting}. But it is late, {goodbye}";
|
|
$entityLegacy = 'i18nTestModule.INJECTIONSLEGACY';
|
|
$defaultLegacy = 'TRANS Hello %s %s. But it is late, %s';
|
|
|
|
// Test missing entity key
|
|
$translated = i18n::_t(
|
|
$entity.'_DOES_NOT_EXIST',
|
|
$default,
|
|
array("name"=>"Mark", "greeting"=>"welcome", "goodbye"=>"bye")
|
|
);
|
|
$this->assertContains(
|
|
"Hello Mark welcome. But it is late, bye",
|
|
$translated,
|
|
"Testing fallback to the translation default (but using the injection array)"
|
|
);
|
|
|
|
// Test standard injection
|
|
$translated = i18n::_t(
|
|
$entity,
|
|
$default,
|
|
["name"=>"Paul", "greeting"=>"good you are here", "goodbye"=>"see you"]
|
|
);
|
|
$this->assertContains(
|
|
"TRANS Hello Paul good you are here. But it is late, see you",
|
|
$translated,
|
|
"Testing entity, default string and injection array"
|
|
);
|
|
|
|
// @deprecated 5.0 Passing in context
|
|
$translated = i18n::_t(
|
|
$entity,
|
|
$default,
|
|
"New context (this should be ignored)",
|
|
["name"=>"Steffen", "greeting"=>"willkommen", "goodbye"=>"wiedersehen"]
|
|
);
|
|
$this->assertContains(
|
|
"TRANS Hello Steffen willkommen. But it is late, wiedersehen",
|
|
$translated,
|
|
"Full test of translation, using default, context and injection array"
|
|
);
|
|
|
|
// @deprecated 5.0 Passing in % placeholders (detected in default value)
|
|
// Note: Missing-placeholder substitution no longer functions
|
|
$translated = i18n::_t(
|
|
$entityLegacy, // has %s placeholders
|
|
$defaultLegacy,
|
|
["name"=>"Cat", "greeting2"=>"meow", "goodbye"=>"meow"]
|
|
);
|
|
$this->assertContains(
|
|
"TRANS Hello Cat meow. But it is late, meow",
|
|
$translated,
|
|
"Testing sprintf placeholders with named injections"
|
|
);
|
|
|
|
// Passing in non-associative arrays for placeholders is now an error
|
|
$this->setExpectedException(InvalidArgumentException::class, 'Injection must be an associative array');
|
|
i18n::_t(
|
|
$entity, // has {name} placeholders
|
|
$default,
|
|
["Cat", "meow", "meow"]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* See @i18nTestModule.ss for the template that is being used for this test
|
|
* */
|
|
public function testNewTemplateTranslation()
|
|
{
|
|
/** @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}'
|
|
],
|
|
'en_US'
|
|
);
|
|
|
|
$viewer = new SSViewer('i18nTestModule');
|
|
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData(['TestProperty' => 'TestPropertyValue'])));
|
|
$this->assertContains(
|
|
Convert::nl2os("Hello Mark welcome. But it is late, bye\n"),
|
|
$parsedHtml,
|
|
"Testing fallback to the translation default (but using the injection array)"
|
|
);
|
|
|
|
$this->assertContains(
|
|
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
|
|
$this->assertContains(
|
|
Convert::nl2os(
|
|
"TRANS Hello ".Director::absoluteBaseURL()." ".i18n::get_locale().". But it is late, global calls\n"
|
|
),
|
|
$parsedHtml,
|
|
"Testing a translation with just entity and injection array, but with global variables injected in"
|
|
);
|
|
}
|
|
|
|
public function testGetLocaleFromLang()
|
|
{
|
|
$this->assertEquals('en_US', i18n::get_locale_from_lang('en'));
|
|
$this->assertEquals('de_DE', i18n::get_locale_from_lang('de_DE'));
|
|
$this->assertEquals('xy_XY', i18n::get_locale_from_lang('xy'));
|
|
}
|
|
|
|
public function testValidateLocale()
|
|
{
|
|
$this->assertTrue(i18n::validate_locale('en_US'), 'Known locale in underscore format is valid');
|
|
$this->assertTrue(i18n::validate_locale('en-US'), 'Known locale in dash format is valid');
|
|
$this->assertFalse(i18n::validate_locale('en'), 'Short lang format is not valid');
|
|
$this->assertFalse(i18n::validate_locale('xx_XX'), 'Unknown locale in correct format is not valid');
|
|
$this->assertFalse(i18n::validate_locale(''), 'Empty string is not valid');
|
|
}
|
|
|
|
public function testTranslate()
|
|
{
|
|
/** @var SymfonyMessageProvider $provider */
|
|
$provider = Injector::inst()->get(MessageProvider::class);
|
|
$provider->getTranslator()->addResource(
|
|
'array',
|
|
[ 'i18nTestModule.ENTITY' => 'Entity with "Double Quotes"' ],
|
|
'en_US'
|
|
);
|
|
$provider->getTranslator()->addResource(
|
|
'array',
|
|
[
|
|
'i18nTestModule.ENTITY' => 'Entity with "Double Quotes" (de)',
|
|
'i18nTestModule.ADDITION' => 'Addition (de)',
|
|
],
|
|
'de'
|
|
);
|
|
$provider->getTranslator()->addResource(
|
|
'array',
|
|
[
|
|
'i18nTestModule.ENTITY' => 'Entity with "Double Quotes" (de_AT)',
|
|
],
|
|
'de_AT'
|
|
);
|
|
|
|
|
|
$this->assertEquals(
|
|
'Entity with "Double Quotes"',
|
|
i18n::_t('i18nTestModule.ENTITY', 'Ignored default'),
|
|
'Returns translation in default language'
|
|
);
|
|
|
|
i18n::set_locale('de');
|
|
$this->assertEquals(
|
|
'Entity with "Double Quotes" (de)',
|
|
i18n::_t('i18nTestModule.ENTITY', 'Entity with "Double Quotes"'),
|
|
'Returns translation according to current locale'
|
|
);
|
|
|
|
i18n::set_locale('de_AT');
|
|
$this->assertEquals(
|
|
'Entity with "Double Quotes" (de_AT)',
|
|
i18n::_t('i18nTestModule.ENTITY', 'Entity with "Double Quotes"'),
|
|
'Returns specific regional translation if available'
|
|
);
|
|
$this->assertEquals(
|
|
'Addition (de)',
|
|
i18n::_t('i18nTestModule.ADDITION', 'Addition'),
|
|
'Returns fallback non-regional translation if regional is not available'
|
|
);
|
|
|
|
i18n::set_locale('fr');
|
|
$this->assertEquals(
|
|
'Entity with "Double Quotes" (fr)',
|
|
i18n::_t('i18nTestModule.ENTITY', 'Entity with "Double Quotes"'),
|
|
'Non-specific locales fall back to language-only localisations'
|
|
);
|
|
}
|
|
|
|
public function testGetLanguageName()
|
|
{
|
|
i18n::config()->update(
|
|
'common_languages',
|
|
array('de_CGN' => array('name' => 'German (Cologne)', 'native' => 'Kölsch'))
|
|
);
|
|
$this->assertEquals('German (Cologne)', i18n::get_language_name('de_CGN'));
|
|
$this->assertEquals('Kölsch', i18n::get_language_name('de_CGN', true));
|
|
}
|
|
}
|