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   16908 Views

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

28 March 2009 at 7:55pm

Hello!

everything works fine until when i add:

protected function getField($field) {
$lang = MultiLingual::currentLang();
$langField = $field.'_'.$lang;
if(isset($this->record[$langField])) {
return $this->record[$langField];
} else {
return isset($this->record[$field]) ? $this->record[$field] : null;
}
}

it's a bit messy because the page.php i have installed in SS 2.3.1 looks pretty different that the one in this example... Could i get the "final looking" files to try to find the error ?

Regards!

Eduardo

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

28 March 2009 at 8:12pm

i'm trying to use your code on version 2.3.1 and everything works fine until i add the following code:

protected function getField($field) {
$lang = MultiLingual::currentLang();
$langField = $field.'_'.$lang;
if(isset($this->record[$langField])) {
return $this->record[$langField];
} else {
return isset($this->record[$field]) ? $this->record[$field] : null;
}
}

it's a bit messy because the page.php i have installed in SS 2.3.1 looks pretty different that the one in this example... Could i get the "final looking" files to try to find the error ?

What i see when i enter this code is a blank screen (white).
If i remove it, i see pages fine, i see the "english" tab on the cms, and the site appears to load fine, but when i try to load a page after i enter this code, things seem don't work here...

hope you can help me, i see this working very nice until i reach to this point :(

Regards

Eduardo

Regards!

Eduardo

Avatar
dab

Community Member, 50 Posts

28 March 2009 at 9:50pm

I use this recipe and it works fine, but forms dont know that they should use different languages, how i may use different strings when i use forms on py pages?

Avatar
CriaturaCreativaStudio

Community Member, 73 Posts

29 March 2009 at 5:40am

can anyone please help me on this tweak? it doesnt work here........ :(

Avatar
schellmax

Community Member, 126 Posts

31 March 2009 at 11:19pm

Edited: 31/03/2009 11:20pm

hi eduardo, was busy till now,
looks as though since 2.3 you have to declare 'getField' method as public, not private:

 public function getField($field) {	...

if this doesn't help: could you post your page class?

@dab: what do you mean by 'using forms'?

Avatar
dab

Community Member, 50 Posts

1 April 2009 at 8:27am

schellmax: i use multilanguages tabs in CMS, so i need to make forms on my pages also with different languages labels/validation messages. I use recipe from http://doc.silverstripe.com/doku.php?id=recipes:multilingual_content

Avatar
Kalileo

Community Member, 127 Posts

14 April 2009 at 6:35pm

Edited: 15/04/2009 6:06pm

Just implemented the recipe at Alternative multilingual recipe on a SS 2.3.1 site, with 3 language versions, and it works fine.

Big thanks to the author(s)! Lack of exactly this functionality was the main roadblock which stopped me from using SS before. Now I'm quite excited, with this multilanguage feature in a CMS such as SS my life will be easier :)

2 hiccups though, here they are with workarounds:

1) The homepages in the different languages don't work, as described above by schellmax, http://domain.tld/en/home and http://domain.tld/de/home etc all jump back to http://domain.tld/ - which is not what I want and not what I understand as the desired behavior described in the recipe. Couldn't fix it in the code without applying the core code patch as described by schellmax, but as an alternative workaround I changed the homepage to http://domain.tld/index - the language versions are now at http://domain.tld/de/index and http://domain.tld/en/index etc. Disadvantage: The real homepage is now at http://domain.tld/index and not at http://domain.tld/ . Entering http://domain.tld gets redirected to http://domain.tld/index. But that could certainly be fixed somehow, haven't looked at it yet.

2) Whenever a translated field is a HtmlEditorField and the page has been opened and saved (without entering content in that field) at least once in the CMS admin area, then these fields are saved with "<p></p>" in them, thus isset() and even empty() will report them as set and not empty, resulting in NOT showing the (untranslated) original field if there is no translation entered yet.

Yes, that sucks. But here is a workaround, the function getField($field) needs to strip the tags and check then if the translated field is empty, before deciding to serve it or the untranslated one. Here's the function getField($field) modified accordingly:

function getField($field) {           
	$lang = MultiLingual::currentLang();
	$langField = $field.'_'.$lang;
	if(!empty($this->record[$langField]) ) {
		$tempField = trim(strip_tags($this->record[$langField]));
		if (!empty($tempField)) { 
			return $this->record[$langField];
		}
	} 
	return isset($this->record[$field]) ? $this->record[$field] : null;
}  

... and then there are still the forms, as dab already mentions, or more precisely the untranslated labels of the form fields of the usual contact form, haven't looked at that yet.

Avatar
Kalileo

Community Member, 127 Posts

15 April 2009 at 6:04pm

i18n

If you have i18n enabled, and if you want to have the locale set automatically, you can use this modified MultiLingual.php:

class MultiLingual extends ModelAsController {  
        static $lang;   
	
	/** added by Kalileo 2009-04-15 to support i18n - START */
        // change or add your locales here as you need
	var $languages = array(
		'de' => 'de_DE',
		'th' => 'th_TH',
		'en' => 'en_US',    
		);
	/** added by Kalileo - END */
	
        function init() {
                $baseUrl = Director::baseUrl();
                $requestUri = $_SERVER['REQUEST_URI'];
                $lang = substr($requestUri, strlen($baseUrl), 2);               
                self::$lang = $lang;
		
		/** added by Kalileo 2009-04-15 to support i18n - START */
		if      (!empty($this->languages[$lang])) {
			i18n::set_locale($this->languages[$lang]); //Setting the locale
		}
		else {
			i18n::set_locale('en_US'); // default locale is English (USA)
		}		
		/** added by Kalileo - END */
		
                //prevents default behaviour of redirecting to '/' for '/en/'
                if($this->URLParams['URLSegment'] == "" || $this->URLParams['URLSegment'] == 'home') {
                        $urlparams = array('URLSegment' => 'home', 'Action' => ' ');
                        $this->setURLParams($urlparams);
                }               
                parent::init();
        }
        static function currentLang() {
                return self::$lang;
        }       
}

I tried to add this as a comment to the Alternative multilingual recipe, but it wouldn't let me.