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.

Template Questions

one.com and i18n


Reply

6 Posts   870 Views

Avatar
dacar

24 April 2013 at 6:45am (Last edited: 24 April 2013 6:38pm), Community Member, 168 Posts

Hi,

has anybody had trouble with SS3.05, one.com and i18n? I can not get it working.

mysite/_config.php:
i18n::set_locale('da_DK');
global $allowed_i18n;
$allowed_i18n = array(
   'dk' => "da_DK",
   'en' => "en_US",
   'de' => "de_DE"
);

i18n::$common_locales = array(
'da_DK' => array('Danish', 'Danish'),
'en_US' => array('English (US)', 'English (US)'),
'de_DE' => array('German', 'Deutsch')
);
Translatable::set_default_locale('da_DK');
Translatable::set_allowed_locales(array('de_DE', 'da_DK', 'en_US'));
Object::add_extension('SiteTree', 'Translatable');
Object::add_extension('SiteConfig', 'Translatable');

mysite/code/page.php

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

mysite/lang/de_DE.php

<?php
global $lang;
//Page.ss translated fields
$lang['de_DE']['Page.ss']['HOME'] = 'Start';
$lang['de_DE']['Page.ss']['ABOUT'] = '&Uuml;ber uns';
$lang['de_DE']['Page.ss']['SERVICES'] = 'Service';
$lang['de_DE']['Page.ss']['WORKS'] = 'Unsere Arbeit';
$lang['de_DE']['Page.ss']['CONTACT'] = 'Kontakt';
$lang['de_DE']['Otherthemefilename.ss']['TRANSLATABLEFIELD'] = 'Translatable field translation';

$lang['de_DE']['Page.ss']['MORE'] = 'mehr';
$lang['de_DE']['Page.ss']['PREV'] = 'zurück';
$lang['de_DE']['Page.ss']['NEXT'] = 'weiter';
$lang['de_DE']['Page.ss']['EXPERTISE'] = 'Unsere Expertise';
$lang['de_DE']['Page.ss']['ALL'] = 'Alle Kategorien';
$lang['de_DE']['Page.ss']['CONTACTFORM'] = 'Kontakt';
$lang['de_DE']['Page.ss']['NAME'] = 'Name';
$lang['de_DE']['Page.ss']['EMAIL'] = 'Email';
$lang['de_DE']['Page.ss']['MESSAGE'] = 'Nachricht';
$lang['de_DE']['Page.ss']['SENDAMESSAGE'] = 'Absenden';

$lang['de_DE']['Page.ss']['SHOWINPAGE'] = 'Zeige Seiten auf deutsch.';
$lang['de_DE']['Page']['SHOWINPAGE'] = 'Zeige Seiten auf deutsch.';
$lang['de_DE']['SHOWINPAGE'] = 'Zeige Seiten auf deutsch.';

$lang['de_DE']['Page']['SERVICES'] = 'Service';
$lang['de_DE']['Top']['SERVICES'] = 'Service';
?>

themes/project/templates/page.ss:

<% _t('Page.ss.SERVICES','Services') %> OR
<% _t('Page.SERVICES','Services') %> OR
<% _t('SERVICES','Services') %>

The t-function will not be translated???

But <% sprintf(_t('SHOWINPAGE','Show page in %s'),$Locale.Nice) %> Is translated to "Show page in german".

Can anybody help?

Avatar
dacar

24 April 2013 at 6:27pm Community Member, 168 Posts

i have just put an echo to the init function to show the current locale:

      if($this->dataRecord->hasExtension('Translatable')) {
         i18n::set_locale($this->dataRecord->Locale);
         //i18n::set_locale(Translatable::get_current_locale());
         //i18n::set_locale('de_DE');
         echo i18n::get_locale();
      }

At this point everything works fine. Why does Silverstripe not find my translation files?

Avatar
dacar

24 April 2013 at 6:36pm Community Member, 168 Posts

de.yml doesn't work, too:

de:
Page:
HOME: 'Start'
ABOUT: '&Uuml;ber uns'
SERVICES: 'Service'
WORKS: 'Unsere Arbeit'
CONTACT: 'Kontakt'
MORE: 'mehr'
PREV: 'zurück'
NEXT: 'weiter'
EXPERTISE: 'Unsere Expertise'
ALL: 'Alle Kategorien'
CONTACTFORM: 'Kontakt'
NAME: 'Name'
EMAIL: 'Email'
MESSAGE: 'Nachricht'
SENDAMESSAGE: 'Absenden'

Avatar
dacar

24 April 2013 at 6:46pm Community Member, 168 Posts

SOLVED!!!

Sorry for spamming you. I haven' read the docs carefully. on [url]http://doc.silverstripe.org/framework/en/topics/i18n[/url] it says:
$entity: Unique identifier, composed by a namespace and an entity name, with a dot separating them. Both are arbitrary names, although by convention we use the name of the containing class or template. Use this identifier to reference the same translation elsewhere in your code.

It has to be: <% _t('Page.HOME','Home') %>

Avatar
BigMoose

11 April 2014 at 7:32am Community Member, 19 Posts

Hi dacar,
I am struggling with this myself. I used translatable with SS 2.x, but am trying to get things to work in SS3 with the add-on module.

I get your solution within the Page.ss template was to use <% _t('Page.HOME','Home') %>. However, what sort of file did you use to hold the actual translation? Did you use the mysite/lang/de_DE.php to hold this information, or did you use de.yml?

If you used de_DE.php, how did you structure it?
$lang['de_DE']['Page.ss']['HOME'] = 'Start'; OR
$lang['de_DE']['Page']['HOME'] = 'Start';

or something completely different?

I have been trying various things but cannot get my translations to work.

Avatar
BigMoose

11 April 2014 at 8:38am Community Member, 19 Posts

Ok, I got this solved for myself here.

I have a template for a contact page called ContactPage.ss
In my ContactPage.ss template file I have something like this:

<p><%t ContactPage.EMAIL "Email:" %> info@whatever.com</p>
<p><%t ContactPage.PHONE "Phone:" %> 555-555-5555</p>
<p><%t ContactPage.TOLL_FREE "Toll Free:" %>1-800-555-5555</p>
<p><%t ContactPage.FAX "Fax:" %>333-333-3333</p>

I have been making translations in french, and so I have created a file: mysite/lang/fr.yml

The code in this file looks like:

fr:
ContactPage:
EMAIL: 'Courriel:'
PHONE: 'T&eacute;l&eacute;phone:'
TOLL_FREE: 'Sans frais:'
FAX: 'T&eacute;l&eacute;copieur:'

I had problems at first because I did not indent things in the Yaml file. I needed to use the 2 spaces indent or else it would not work.