mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT: tests for new i18n syntax
This commit is contained in:
parent
7d84aff01e
commit
45c7dfd9f5
@ -2,4 +2,11 @@
|
||||
<% _t('LAYOUTTEMPLATENONAMESPACE',"Layout Template no namespace") %>
|
||||
<% sprintf(_t('i18nTestModule.SPRINTFNAMESPACE','My replacement: %s'),$TestProperty) %>
|
||||
<% sprintf(_t('SPRINTFNONAMESPACE','My replacement no namespace: %s'),$TestProperty) %>
|
||||
<% include i18nTestModuleInclude %>
|
||||
<% include i18nTestModuleInclude %>
|
||||
|
||||
<%t i18nTestModule.NEWMETHODSIG "New _t method signature test" %>
|
||||
<%t i18nTestModule.INJECTIONS_DOES_NOT_EXIST "Hello {name} {greeting}. But it is late, {goodbye}" name="Mark" greeting="welcome" goodbye="bye" %>
|
||||
<%t i18nTestModule.INJECTIONS "Hello {name} {greeting}. But it is late, {goodbye}" name="Paul" greeting="good you are here" goodbye="see you" %>
|
||||
<%t i18nTestModule.INJECTIONS "Hello {name} {greeting}. But it is late, {goodbye}" is "New context (this should be ignored)" name="Steffen" greeting="willkommen" goodbye="wiedersehen" %>
|
||||
<%t i18nTestModule.INJECTIONS name="Cat" greeting='meow' goodbye="meow" %>
|
||||
<%t i18nTestModule.INJECTIONS name=$absoluteBaseURL greeting=$get_locale goodbye="global calls" %>
|
@ -167,7 +167,7 @@ class i18nTest extends SapphireTest {
|
||||
|
||||
function testTemplateTranslation() {
|
||||
$oldLocale = i18n::get_locale();
|
||||
|
||||
|
||||
i18n::set_locale('en_US');
|
||||
i18n::get_translator('core')->getAdapter()->addTranslation(array(
|
||||
'i18nTestModule.MAINTEMPLATE' => 'Main Template',
|
||||
@ -242,6 +242,90 @@ class i18nTest extends SapphireTest {
|
||||
|
||||
i18n::set_locale($oldLocale);
|
||||
}
|
||||
|
||||
function testNewTMethodSignature() {
|
||||
global $lang;
|
||||
$oldLocale = i18n::get_locale();
|
||||
|
||||
i18n::set_locale('en_US');
|
||||
$lang['en_US']['i18nTestModule']['NEWMETHODSIG'] = 'TRANS New _t method signature test';
|
||||
$lang['en_US']['i18nTestModule']['INJECTIONS'] = 'TRANS Hello {name} {greeting}. But it is late, {goodbye}';
|
||||
|
||||
$entity = "i18nTestModule.INJECTIONS";
|
||||
$default = "Hello {name} {greeting}. But it is late, {goodbye}";
|
||||
|
||||
$translated = i18n::_t('i18nTestModule.NEWMETHODSIG',"New _t method signature test");
|
||||
$this->assertContains(
|
||||
"TRANS New _t method signature test",
|
||||
$translated
|
||||
);
|
||||
|
||||
$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)"
|
||||
);
|
||||
|
||||
$translated = i18n::_t($entity, $default, array("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"
|
||||
);
|
||||
|
||||
$translated = i18n::_t($entity, $default, "New context (this should be ignored)", array("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"
|
||||
);
|
||||
|
||||
$translated = i18n::_t($entity, array("name"=>"Cat", "greeting"=>"meow", "goodbye"=>"meow"));
|
||||
$this->assertContains(
|
||||
"TRANS Hello Cat meow. But it is late, meow",
|
||||
$translated, "Testing a translation with just entity and injection array"
|
||||
);
|
||||
|
||||
i18n::set_locale($oldLocale);
|
||||
}
|
||||
|
||||
/**
|
||||
* See @i18nTestModule.ss for the template that is being used for this test
|
||||
* */
|
||||
function testNewTemplateTranslation() {
|
||||
global $lang;
|
||||
$oldLocale = i18n::get_locale();
|
||||
|
||||
i18n::set_locale('en_US');
|
||||
$lang['en_US']['i18nTestModule']['NEWMETHODSIG'] = 'TRANS New _t method signature test';
|
||||
$lang['en_US']['i18nTestModule']['INJECTIONS'] = 'TRANS Hello {name} {greeting}. But it is late, {goodbye}';
|
||||
|
||||
$viewer = new SSViewer('i18nTestModule');
|
||||
$parsedHtml = $viewer->process(new ArrayData(array('TestProperty' => 'TestPropertyValue')));
|
||||
$this->assertContains(
|
||||
"Hello Mark welcome. But it is late, bye\n",
|
||||
$parsedHtml, "Testing fallback to the translation default (but using the injection array)"
|
||||
);
|
||||
$this->assertContains(
|
||||
"TRANS Hello Paul good you are here. But it is late, see you\n",
|
||||
$parsedHtml, "Testing entity, default string and injection array"
|
||||
);
|
||||
$this->assertContains(
|
||||
"TRANS Hello Steffen willkommen. But it is late, wiedersehen\n",
|
||||
$parsedHtml, "Full test of translation, using default, context and injection array"
|
||||
);
|
||||
|
||||
$this->assertContains(
|
||||
"TRANS Hello Cat meow. But it is late, meow\n",
|
||||
$parsedHtml, "Testing a translation with just entity and injection array"
|
||||
);
|
||||
|
||||
//test injected calls
|
||||
$this->assertContains(
|
||||
"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"
|
||||
);
|
||||
|
||||
i18n::set_locale($oldLocale);
|
||||
}
|
||||
|
||||
function testGetLocaleFromLang() {
|
||||
$this->assertEquals('en_US', i18n::get_locale_from_lang('en'));
|
||||
|
@ -175,6 +175,45 @@ class SSTemplateParser extends Parser {
|
||||
$this->Lookup_AddLookupStep($res, $sub, '$$FINAL');
|
||||
}
|
||||
|
||||
|
||||
/*!*
|
||||
# Translatable call
|
||||
|
||||
Translate: "<%t" Entity (Default)? (!("is" "=") "is" Context)? (InjectionVariables)* "%>"
|
||||
InjectionVariables: InjectionName:Word "=" Argument
|
||||
*/
|
||||
|
||||
function Translate__construct(&$res) {
|
||||
$res['php'] = "_t(";
|
||||
}
|
||||
|
||||
function Translate_Entity(&$res, $sub) {
|
||||
$res['php'] .= $sub['php'];
|
||||
Debug::show($sub);
|
||||
}
|
||||
function Translate_Default(&$res, $sub) {
|
||||
Debug::show($sub);
|
||||
}
|
||||
function Translate_Context(&$res, $sub) {
|
||||
Debug::show($sub);
|
||||
}
|
||||
|
||||
function Translate__finalise(&$res) {
|
||||
$res['php'] .= ')';
|
||||
}
|
||||
|
||||
|
||||
function InjectionVariables__construct(&$res) {
|
||||
}
|
||||
|
||||
function InjectionVariables_InjectionName(&$res, $sub) {
|
||||
}
|
||||
|
||||
function InjectionVariables_Argument(&$res, $sub) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*!*
|
||||
|
||||
# Injections are where, outside of a block, a value needs to be inserted into the output. You can either
|
||||
|
Loading…
Reference in New Issue
Block a user