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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Multilingual Content - alternative to 'translatable'


Go to End


44 Posts   16911 Views

Avatar
Kalileo

Community Member, 127 Posts

11 July 2009 at 1:10pm

Eduardo, your welcome.

I agree that the documentation could be better, for me it helped that I develop also with CakePHP (which is another really nice MVC framework, but without a real CMS). There i18n is used similarly, and I had to play with the routes similarly to the multilanguage alternative here to get real multilingual sites up.

For your form translation problem the labels to translate might be in the SS code somewhere, instead of in the templates, and while changing it there is possible and will work, you risk that your changes are reversed after an upgrade.

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

17 July 2009 at 5:01am

Kalileo,

I don't want to abuse of your kindness, but maybe you can help me with the form problem (of course if you have some time)

I used the "alternative to translatable" recipe.
The url's are these:

http://www.ceramicacelec.com/contactenos/
Main language: spanish, no problems here.

http://www.ceramicacelec.com/en/contactenos/
Translated "english" version, it's a standard SS form, but i cannot find a way to show different field names.

Any help would be appreciated!

Thanks in advance my friend! :)

Regards

Eduardo

Avatar
Kalileo

Community Member, 127 Posts

19 July 2009 at 4:30pm

Edited: 21/07/2009 3:32pm

Below is a workaround for the translation of form labels.

Hi Eduardo,

i understand what you are talking bout, this is, no, this was one of the open issues on my list too. The following is assuming that you made your form in the CMS and it is there where you added your Spanish language labels 9such as "Empresa", "Rubro", "Contacto (nombre)", etc.).

The main problem is that our nice SS developers have not yet made this part of the code i18n aware.

The bad news is that you have to patch the Sapphire code to fix this (at least I'm not yet aware of another easy way). So beware if you run an SS update, it will overwrite the fix we make here.

The good news is that it is simple and easy :)

You do not have to be a PHP developer to implement the fix below, but you should at least dare to modify the code following the instructions which I'll give you below.

And you have to be familiar with i18n and how to use it in SS, you need i18n enabled in SS to use it. (See my other posts about i18n in this thread).

This fix is tested in SS 4.3.1, it does not need the multilingual alternative to be implemented to work, but it needs i18n enabled (which you have if you use the multilingual alternative in 4.3.1).

EDIT: The code which we patch is unchanged in SS 4.3.2, thus it should also work in 4.3.2, if you have i18n enabled there, but you do not need it if you use the new translatable module of SS 4.3.2.

We need to patch the code in /sapphire/forms/FormField.php, the function FieldHolder(), which should be at line 334. Replace the code there with this code, or just add the changes manually:

	function FieldHolder() {
		$Title = $this->XML_val('Title');
		$Message = $this->XML_val('Message');
		$MessageType = $this->XML_val('MessageType');
		$RightTitle = $this->XML_val('RightTitle');
		$Type = $this->XML_val('Type');
		$extraClass = $this->XML_val('extraClass');
		$Name = $this->XML_val('Name');
		$Field = $this->XML_val('Field');
		
		/** 3 lines below added by Kalileo 2009-07-19 to enable translations of labels in standard SS forms */
		if (!empty($Title)) {
		    $Title = _t('FieldHolder.' . $Title, $Title );
		}
		
		$titleBlock = (!empty($Title)) ? "<label class=\"left\" for=\"{$this->id()}\">$Title</label>" : "";
		$messageBlock = (!empty($Message)) ? "<span class=\"message $MessageType\">$Message</span>" : "";
		$rightTitleBlock = (!empty($RightTitle)) ? "<label class=\"right\" for=\"{$this->id()}\">$RightTitle</label>" : "";

		/** return below changed by Kalileo 2009-04-19 to avoid nasty IE7 margin inheritance bug: 
                    wrapped $Field in <span> tag "<span>$Field</span>" 
                    IE sucks!!!!!!!!!!  
                */
                return <<<HTML
<div id="$Name" class="field $Type $extraClass">$titleBlock<div class="middleColumn"><span>$Field</span></div>$rightTitleBlock$messageBlock</div>
HTML;
	}

As you can see there is also a bugfix for IE7 included, which is not related to our translation problem.

The only part needed for the translation is the addition of these lines there, inserted at line 344:

		/** 3 lines below added by Kalileo 2009-07-19 to enable translations of labels in standard SS forms */
		if (!empty($Title)) {
		    $Title = _t('FieldHolder.' . $Title, $Title );
		}

Now the labels in the CMS standard forms are i18n aware. But we still have to add the translations. These are in the files under /mysite/lang

For English, my default, I use the file /mysite/lang/en_US.php. If you do not have it, then create it, or just add the relevant lines below.

<?php

global $lang;

$lang['en_US']['FieldHolder']['Your name'] = 'Your name';
$lang['en_US']['FieldHolder']['Your Email'] = 'Your Email';
$lang['en_US']['FieldHolder']['Your phone'] = 'Your phone';
$lang['en_US']['FieldHolder']['Your Message'] = 'Your Message';

?>

For German I use /mysite/lang/de_DE.php.

<?php

/**
 * German (Germany) language pack
 * @package mysite
 * @subpackage i18n
 */

i18n::include_locale_file('mysite', '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']['FieldHolder']['Your name'] = 'Ihr Name';
$lang['de_DE']['FieldHolder']['Your Email'] = 'Ihre E-mail';
$lang['de_DE']['FieldHolder']['Your phone'] = 'Ihr Telefon';
$lang['de_DE']['FieldHolder']['Your Message'] = 'Ihre Nachricht';

?>

For Thai I use /mysite/lang/th_TH.php.

<?php

/**
 * Thai (Thailand) language pack
 * @package mysite
 * @subpackage i18n
 */

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

global $lang;

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

$lang['th_TH']['FieldHolder']['Your name'] = 'ชื่อ';
$lang['th_TH']['FieldHolder']['Your Email'] = 'ที่อยู่อีเมล';
$lang['th_TH']['FieldHolder']['Your phone'] = 'หมายเลขโทรศัพท์';
$lang['th_TH']['FieldHolder']['Your Message'] = 'รายละเอียด';

?>

You get the concept? Isn't it easy :)

Now you only need to replace the Your name, Your Email, etc with exactly the label text as you have entered it in the CMS for the form fields. In your (Eduardo's) case it will be "Empresa", "Rubro", "Contacto (nombre)"

$lang['en_US']['FieldHolder']['Empresa'] = 'Business';
$lang['en_US']['FieldHolder']['Rubro'] = 'Item';
$lang['en_US']['FieldHolder']['Contacto (nombre)'] = 'Contact (name)';

The code is implemented in a SS 4.3.1 site about wastewater treatment systems in Thailand in English, German, and Thai.

EDIT: As said, you do not need this if you use translatable in SS 4.3.2

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

21 July 2009 at 7:52am

thanks for your extensive explanation!

I've patched spahire file as you've said here.
no problems.

The problem i'm having now, is that i've added the fields to ENGLISH en_US language file, but i get those fields in the spanish version! (in both versions actually!)
Seems like the main language is not set properly.

Then i've added this line into _config.php in the saphire folder:

i18n::set_locale('es_ES');

But no changes! i think that it has to do with the i18n activation, i didn't find how to enable it properly...

:(

If you can help me again, it will be great.

Thank you very much!

Eduardo
documentation is EXTREMELY messy, i'm lost in this problem :(

Avatar
Kalileo

Community Member, 127 Posts

21 July 2009 at 2:33pm

Eduardo,

you should not only have a language file for the translations, in your case /mysite/lang/en_US.php but also for your default language, in your case /mysite/lang/es_ES.php, containing all the labels. See my example with the default in English. Notice the difference for the default language file and the translation language files.

In order to activate i18n you need to have in your /mysite/_config.php the line

/**
* Enabling i18n
*/
i18n::enable();

best before you have your "i18n::set_locale('es_ES');".

After that you rebuild your database with /dev/build

If you have that already, or if that did not help, then, to help debugging, modify temporarily the function FieldHolder() in /sapphire/forms/FormField.php again, changing this

     /** 3 lines below added by Kalileo 2009-07-19 to enable translations of labels in standard SS forms */ 
      if (!empty($Title)) { 
       $Title = _t('FieldHolder.' . $Title, $Title ); 
      }

to
     /** 3 lines below added by Kalileo 2009-07-19 to enable translations of labels in standard SS forms */ 
      if (!empty($Title)) { 
       $Title = _t('FieldHolder.' . $Title, $Title . ' - default' ); 
      }

Notice the added " . ' - default'" there.

Now, if the translations are not taken / not found in the translation files, you see a " - default" added to all labels, such as "Empresa - default.

If you see that that means that your modification of /sapphire/forms/FormField.php was effective but the translations in the language files are not found. This will give you a hint as to where to look next.

In case got the labels like Empresa - default please post your Spanish and your English language file, and your function FieldHolder() in /sapphire/forms/FormField.php here, may be I or someone else can spot the error.

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

21 July 2009 at 4:33pm

Kalileo,

Thank you very much, again...
But this workaround doesn't seem to work here, i mean, seems like i'm doing something wrong, and i have plenty of errors and malfunctions on the site...
I'm going to give up this for this moment, i'm confused with the two implementations and this pissed me off.

Is there a way to do this with the new version of SS from the scratch? (i mean the form fields also in the translation)

THANK YOU VERY MUCH, i really apreciate your help and dedication, if you need help on desing, count on me.

Regards

Eduardo

Avatar
Kalileo

Community Member, 127 Posts

21 July 2009 at 4:43pm

Eduardo, your site seems to be not really live yet, so yes, if you can, redo it is SS 2.3.2 and use the new Translatable module.

There you do not need this workaround, just activate translatable. On a first look SS 2.3.2 seems to add another form for each translation, so you can give it the translated label names right in the CMS admin screen.

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

21 July 2009 at 4:46pm

Kalileo,

The site is live, the english part insn't linked, but active!
If you use the urls with the /en/ prefix, you'll see it...

I've tried to use the new module, but i find instructions not friendly, that's why i've used this alternative.
Maybe i'll duplicate the form and hardcode the navigation on the top, i know is not the best solution, but since i don't understand the new instructtions, that will be the easier way.

Thank you very much for your help

Regards

Eduardo