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.

All other Modules /

Discuss all other Modules here.

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

[Translatable] translate static content


Go to End


5 Posts   1494 Views

Avatar
Faloude

Community Member, 55 Posts

1 March 2016 at 9:55am

I've installed the translatable module and it's up and running smoothly. For translating dynamic content it works just fine.

But how do I translate static content in the .ss files? For example the "Copyright" line at the bottom. It would be too cumbersome to create a custom CMS field for this single word and enter the right translation on every page.

Avatar
sebastiand

Community Member, 5 Posts

4 March 2016 at 10:23pm

What you are looking for is the '_t()' function in Silverstripe.

You define a variable and a default string for it like so <%t MyTemplateName.MYVARIABLENAME "My default string" %>.

Now you can define language file per language that is placed under mysite/lang/ and named after the language code 'en.yml' for english, 'de.yml' for german, and so on.

In this language file you can add as many translation variables as you want, all structured in the way you call them from the _t() function:

en:
  MyTemplateName:
    MYVARIABLENAME: 'My default string'
    ...

Avatar
Faloude

Community Member, 55 Posts

5 March 2016 at 1:19am

Hey Sebastian, thanks for your reply! So this is what I've done:

I've created an en.yml in mysite/code/:

en:
 mytemplate:
  test: 'hello'

..And a fr.yml (french) in the same location.

fr:
 mytemplate:
  test: 'bonjour'

In my template (HomePage.ss):

<%t mytemplate.test 'hello' %>

In each version (en and fr) of the site it displays "hello" and not the french translation "bonjour".

Additional info:
-ss translatable module installed (and working)
-ss translatable prefix module installed
-deleted an empty file in mysite/lang folder that was named "_MANIFEST_exclude" or something..
-do I need to run http://localhost/dev/tasks/i18nTextCollectorTask/?module=cms ? It just says ".... running" and nothing happens, I've been told that I'm missing PHPunit dependancy, is that relevant?

Cheers

Avatar
sebastiand

Community Member, 5 Posts

5 March 2016 at 1:46am

Edited: 09/03/2016 7:12pm

I forgot to mention one importnant thing (which is mentioned in the translatable docs under https://github.com/silverstripe/silverstripe-translatable/blob/2.1/docs/en/index.md#setting-the-i18n-locale).
You need to pass the current locale to your controller to make it available in your templates.

This is done by adding the following code to your Page_Controller::init() function:

	public function init() {
		parent::init();
		// You can include any CSS or JS required by your project here.
		// See: http://doc.silverstripe.org/framework/en/reference/requirements
		
		// set locale to current locale of page to make it available in templates
		if($this->dataRecord->hasExtension('Translatable')) {
			i18n::set_locale($this->dataRecord->Locale);
		}
	}

Avatar
Faloude

Community Member, 55 Posts

5 March 2016 at 1:58am

Whoah, it actually works! I have no idea what I have technically done here, but I'll find that out later! Cheers mate