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

Translating text in Template files


Go to End


2 Posts   2225 Views

Avatar
dianaAvila

Community Member, 12 Posts

29 April 2015 at 7:04am

Hi,

I've been stuck with this for a couple of days now. I've been following the translatable and i18n documentation and tutorials, but it seems like I'm still missing something.
SiteTree translations work fine, and I have a working language switcher in place too. My problem is that we have some hardcoded string directly in the template files and I'm not getting anywhere near to get them translated,

Here's my configuration: (site needs EN and FR)
_config.php:
i18n::set_locale('en_US');
Translatable::set_default_locale('en_US');
SiteConfig::add_extension('Translatable');
..
Page.php
class Page_Controller extends ContentController {
public function init() {
parent::init();
// set locale for each page
if($this->dataRecord->hasExtension('Translatable')) {
i18n::set_locale($this->dataRecord->Locale);
}

I have fr_FR.php and en_US.php files
<?php
global $lang;
$lang['fr_FR']['ProductCategoryPage']['HEADING'] = 'fr text.';
?>

and
<?php
global $lang;
$lang['en_US']['ProductCategoryPage']['HEADING'] = 'en text.';
?>

and the template has: <%t ProductCategoryPage.HEADING %>

but I don't get the string anything on the browser.

I added phpUnit to my composer file, but I'm still greatly confused with the text collector.

I would greatly appreciate any help

Avatar
dianaAvila

Community Member, 12 Posts

30 April 2015 at 3:27am

Edited: 30/04/2015 3:28am

I found this post, of someone on a similar situation as I was: http://www.silverstripe.org/community/forums/all-other-modules/show/51701#post401392
and after trying to follow this tutorial: http://www.balbuss.com/internationalize/
This worked for me:
instead of creating language files as php under the lang folder (en_US.php) I created YAML files (en_US.yml). I have no idea why but the translated strings are working so far.