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 - _t function - Am I missing something?


Go to End


6 Posts   3200 Views

Avatar
Richard von Fohrenburg

Community Member, 7 Posts

27 April 2014 at 10:30pm

Hey there,

would like to ask if someone could help me out with this one.
I just found out how to use the t-function, though I don't get it working.

This is how my _config.php looks like

Translatable::set_default_locale('de_DE');
i18n::set_locale('de_DE');
i18n::set_locale('en_GB');
i18n::set_locale('it_IT');
i18n::set_locale('fr_FR');
Translatable::set_allowed_locales(array(
   'de_DE',
   'en_GB',
   'it_IT',
   'fr_FR'
   )
);
SiteTree::add_extension('Translatable');
SiteConfig::add_extension('Translatable');

My header has the $ContentLocale

<html lang="$ContentLocale" xmlns="http://www.w3.org/1999/xhtml">

This is the function in my template file HomePage.ss

<p><% _t('HomePage.ss.LEISTUNGSPROFIL','LEISTUNGSPROFIL') %></p>

This is the content of my Page.php

<?php
class Page extends SiteTree {

	private static $db = array(
		'SideBar'=>'HTMLText',
	);
	public function getCMSFields() {
	$fields = parent::getCMSFields();
	$fields->addFieldToTab('Root.Main',new HTMLEditorField('SideBar','Sidebar Inhalt'));
	return $fields;
	} 

	private static $has_one = array(
	);

}
class Page_Controller extends ContentController {

	/**
	 * An array of actions that can be accessed via a request. Each array element should be an action name, and the
	 * permissions or conditions required to allow the user to access it.
	 *
	 * <code>
	 * array (
	 *     'action', // anyone can access this action
	 *     'action' => true, // same as above
	 *     'action' => 'ADMIN', // you must have ADMIN permissions to access this action
	 *     'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
	 * );
	 * </code>
	 *
	 * @var array
	 */
	private static $allowed_actions = array (
	);

	public function init() {
		parent::init();
			if($this->dataRecord->hasExtension('Translatable')) {
			i18n::set_locale($this->dataRecord->Locale);
    		}
		}
}

And lastly I created a "lang" folder in "mysite" folder which consists of 3 files:

en_GB.php
fr_FR.php
it_IT.php

<?php

$lang['en_GB']['HomePage.ss']['LEISTUNGSPROFIL'] = 'LEISTUNGSPROFIL English';

So, what am I doing wrong? Is everything it it's right place? Does the lang folder belong into mysite?
Would be awesome if someone could help me out.

You can check out the non working translation on the HomePage of greenrhythm.org

Best regards,

Richard

Avatar
Richard von Fohrenburg

Community Member, 7 Posts

29 April 2014 at 10:34pm

UP ... anyone?

Avatar
swaiba

Forum Moderator, 1899 Posts

30 April 2014 at 5:51am

It can be a bit tricky... Ingos ss book covers it... but this I think does a very good job (more info in the comments too)

http://www.ssbits.com/tutorials/2011/using-translatable-to-create-a-simple-multilingual-site/

Avatar
Richard von Fohrenburg

Community Member, 7 Posts

30 April 2014 at 10:55am

It is tricky .... I am a damn designer not a coder :)
Thx for the link though, at least my config is corrected now and I found a way to translate static links.

Also updated my page.php etc., I'm pretty sure everything is set up correctly.
I don't get it ...

Avatar
geekdenz

Community Member, 37 Posts

3 August 2014 at 10:57pm

In templates you should use
<%t NAMESPACE.MYTRANSLATEDSTRING "My Translated String" %>

Then in your project you can have a file under
mysite/lang/en.yml
mysite/lang/de.yml

etc or in yourmodule/lang/en.yml

The _t function has the same parameters but needs to be called in PHP.

In en.yml you would have something like
en:
NAMESPACE:
MYTRANSLATEDSTRING: My Translated String

Similarly for German:
de:
NAMESPACE:
MYTRANSLATEDSTRING: Meine Uebersetzte Zeichenkette

Avatar
dianaAvila

Community Member, 12 Posts

30 April 2015 at 3:24am

I was in a similar situation and clueless about how to get _t() working even after following a few tutorials.
What 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.