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

[SOLVED] Custom Form and site Multilanguage


Go to End


5 Posts   2435 Views

Avatar
biapar

Forum Moderator, 435 Posts

25 February 2011 at 1:53am

Hi,

I've a multilanguage site and I've created an input form with Form class in a Page class.

How translate field label?

Thank you

Avatar
dompie

Community Member, 88 Posts

26 February 2011 at 2:36am

Edited: 26/02/2011 2:36am

Translating fields can be done with the _t function, e.g.

public function MyForm(){
   $fields = new FieldSet(
   new TextField('Name', _t('MyformPage.FIRST_LAST_NAME', 'Name'), '', 200),
   ...);
   return new Form(....);
}

Avatar
biapar

Forum Moderator, 435 Posts

26 February 2011 at 4:39am

How Do I use director::redirect in a multilanguage site?

That is from that form I have to redirect to a page in the site that is page-it or page-en...

Avatar
biapar

Forum Moderator, 435 Posts

28 February 2011 at 6:53am

In my page controller where there is form, I write:

public function init(){
parent::init();

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

and for labels, I use _t('DisponibilitaPage.PRIVACY','Accetto Regolamento Privacy') in my Form costructor.

I my include template , I use for label <% _t('ROOM','Camera') %>

Language files was saved into lang folder under mysite. So, for this template, I use $lang['it_IT']['MytemplateUnderInclude.ss']['ROOM'] = 'Camera' and for class, I use $lang['it_IT']['DisponibilitaPage']['PRIVACY'] = 'Accetto Regolamento Privacy';

Bye

Avatar
Gelert

Community Member, 14 Posts

1 March 2011 at 1:16am

Edited: 01/03/2011 1:29am

Thank you, that's brilliant. Solved my template translation issues too!

I now have Italian and English text dropping into my custom form.

I added the above

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

to mysite/page.php

And then in mysite/lang folder I have:
en_GB.php
it_IT.php

mysite/lang/en_GB.php:

global $lang;
$lang['en_GB']['Sidebar.ss']['SIDEBARTITLE'] = 'English Title';

mysite/lang/it_IT.php:

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

global $lang;

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

$lang['it_IT']['Sidebar.ss']['SIDEBARTITLE'] = 'Italian Title';

then in themes/mytheme/templates/Includes/Sidebar.ss I have:

<h3><% _t('SIDEBARTITLE','My English Title') %></h3>

This needs better documentation from Silverstripe itself. That little dataRecord>hasExtension bit in the Page Controller is totally KEY to it all working.

Yet it's never mentioned anywhere! (I'm looking at you Silverstripe Documentation: http://doc.silverstripe.org/sapphire/en/topics/i18n).

Update: now I know what I'm looking for I've found it on this page [Which I had read several times] (http://doc.silverstripe.org/sapphire/en/topics/translation) but says:

Unser the heading: Setting the i18n locale
You can set the i18n locale value which is used to format dates, currencies and other regionally different values to the same as your current page locale.

That doesn't say to me include this code to get your lang/<locale>.php files working. Thanks to the wonderful users of the SS Forum these bits of code are sorted out.

It's so difficult from a new user point of view.

When I've finished the project I'm on, I might write my own tutorial about languages, filtering page data objects and custom side bar forms.

All I don't know how to do now is make my Custom Form a "sticky" form. So if you select to filter by "Price Range 123" the drop down remembers that selection when the search is displayed instead of jumping back to the top item "Any". I guess I'll just be happy it's all working and get on with creating the rest of the site now.