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.

Data Model Questions /

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

Translatable DataObjects do not work


Go to End


7 Posts   3312 Views

Avatar
spierala

Community Member, 80 Posts

6 May 2010 at 4:26am

Edited: 06/05/2010 11:59pm

hello,

i try to make my dataobjects translatable.

I add
Object::add_extension('News', 'Translatable');
to my config.php

After rebuilding the db, there is a new attribute to the News table named "Locale".
All rows got the value "de_De" cause this is my default language.

When I create a new page in another language e.g. english...the news tab contains no news. Fine.
Then I try to add a news, but it won´t appear in the english site, but in the german site.

In the database I can see all items got the de_DE locale, even if I added them in "englisch mode" in the ss backend. If I edit the locale in the db manually for an entry to en_GB then the item is shown correctly under the news tab of a english page.

What went wrong here?
Many thx,
florian

//i use ss.2.3.3
//same behavior in ss.2.4

Avatar
lerni

Community Member, 81 Posts

13 May 2010 at 7:11am

hi

solved it with this one:
http://www.silverstripe.org/dataobjectmanager-module-forum/show/280629#post280629

but i guess there is another way?

Avatar
MarcusDalgren

Community Member, 288 Posts

17 May 2010 at 10:09am

Yes you can set the correct locale with onBeforeWrite();

so

public function onBeforeWrite() {
$this->Locale = Translatable::get_current_locale();
}

However you will still need the fix from the link lerni provided to make sure that the CMS keeps track of which locale you are in properly.

Avatar
spierala

Community Member, 80 Posts

18 May 2010 at 3:55am

wow smurkas! that worked! many thx!

I knew this function before, but somehow I relied on silverstripe multilingual functionality to do something like this for me :)

why this is not documented somewhere?

florian

Avatar
Ingo

Forum Moderator, 801 Posts

25 May 2010 at 9:44pm

If you look at Translatable->onBeforeWrite(), thats exactly what it does:

// If language is not set explicitly, set it to current_locale.
		// This might be a bit overzealous in assuming the language
		// of the content, as a "single language" website might be expanded
		// later on. See {@link requireDefaultRecords()} for batch setting
		// of empty Locale columns on each dev/build call.
		if(!$this->owner->Locale) {
			$this->owner->Locale = Translatable::get_current_locale();
		}

Note sure why that didnt get picked up in your case...

Avatar
MarcusDalgren

Community Member, 288 Posts

26 May 2010 at 12:54am

Unless they're using the patch from #4199 then the Locale won't be set properly. However your'e correct in that it should've been picked up from Translatable. Trying this locally right now it doesn't work if Locale isn't set on the DataObject. For some reason the line in Translatable doesn't work for DataObjects.

Actually right now I can't get it to work at all. The fix I provided here has worked for me before but the basic version from Translatable should work as well. No idea why it doesn't.

Avatar
elgordo

Community Member, 70 Posts

11 November 2010 at 9:30pm

hi

I've tested the fixes mentioned in the post and now I can see the correct translation locale appearing in the table for my DataObject (in this case RailwayStation) in the database. However...

i) I have already created several pages in English, creating railway stations along the way
ii) I switch to another language, and the railway stations now show as blank in the form editing interface
iii) I can add new railway stations, the locale is correct, but I have no way of telling which translated versions are referring to the same item

e.g. I create a station called "Paddington, London" and then I switch to French, no station is visible. I add a French version called "Paddington Londres" when translating the document in French. I cannot see how in the database that the 2 records refer to the same place