Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[SOLVED] Problem with translations _t() returns always same language


Go to End


5 Posts   3785 Views

Avatar
Dreadmaaan

Community Member, 3 Posts

16 August 2011 at 3:50am

Hey there,

I have an annoying problem using the _t() function in my templates. I'm trying to translate the search result template and for that matter I created two languagefiles in the mysite/lang folder, de_DE.php and en_US.php . But the template only displays the german texts, no matter which language I choose. You can try it out for yourself @ http://sng.cotec-it-systeme.de/

en_US.php

global $lang;

$lang['en_US']['Page_results']['NoResults'] = 'Sorry, your search query did not return any results';
$lang['en_US']['Page_results']['MoreAbout'] = 'more about';
$lang['en_US']['Page_results']['NextPage'] = 'Next page';
$lang['en_US']['Page_results']['PrevPage'] = 'Previous page';
$lang['en_US']['Page_results']['GoToPage'] = 'Go to page';
$lang['en_US']['SearchForm']['SearchResults'] = 'Search results';

de_DE.php

i18n::include_locale_file('sleepNgo', 'en_US');

global $lang;

if(array_key_exists('de_DE', $lang) && is_array($lang['de_DE'])) {
	$lang['de_DE'] = array_merge($lang['en_US'], $lang['de_DE']);
} else {
	$lang['de_DE'] = $lang['en_US'];
}

$lang['de_DE']['Page_results']['NoResults'] = '2Zu Ihrer Suche gibt es leider keine Ergebnisse.';
$lang['de_DE']['Page_results']['MoreAbout'] = '2Mehr zu';
$lang['de_DE']['Page_results']['NextPage'] = '2Nächste Seite';
$lang['de_DE']['Page_results']['PrevPage'] = '2Vorherige Seite';
$lang['de_DE']['Page_results']['GoToPage'] = '2Gehe zu Seite';
$lang['de_DE']['SearchForm']['SearchResults'] = '2Suchergebnisse';

_config.php (snippet)

i18n::set_locale('de_DE');
Translatable::set_default_locale('de_DE');
Translatable::set_allowed_locales(array(
   'de_DE',
   'en_US',
   )
);
Object::add_extension('SiteTree', 'Translatable');
Object::add_extension('SiteConfig', 'Translatable');

Page_results.ss (snippet)

<a class="readMoreLink" href="$Link" title="<% _t('Page_results.MoreAbout','1Mehr zu') %> &quot;{$Title}&quot;">
   <% _t('Page_results.MoreAbout','1Mehr zu') %> &quot;{$Title}&quot;...
</a>

What am I doing wrong?

greetings
Martin

Avatar
MarcusDalgren

Community Member, 288 Posts

16 August 2011 at 4:12am

If you're doing direct template translation then it needs to be Page_results.ss not just Page_results. Also leave out the domain so it says just MoreAbout instead of Page_results.MoreAbout since the _t() function in the template automatically gets its name as the namespace.

So <% _t('MoreAbout','1Mehr zu') %> will become $lang['en_US']['Page_results.ss']['MoreAbout']

Avatar
Dreadmaaan

Community Member, 3 Posts

17 August 2011 at 9:20am

Sadly, this doesn't work either. Again, only the german texts from de_DE.php are being displayed.

If I remove (or rename) the de_DE.php file, the fallback text of the _f() function in the template shows up, no matter which language is currently active. I don't get it, it seems like the english translations aren't even being read.

Any ideas?

Avatar
mv

Community Member, 5 Posts

17 August 2011 at 10:37pm

Hello Dreadmaaan!

Here's a solution to your problem: http://silverstripe.org/template-questions/show/16714?start=8#post306577

Avatar
Dreadmaaan

Community Member, 3 Posts

18 August 2011 at 10:06am

That worked. Thank you so much!