Jump to:

3375 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Translatable DataObjects do not work

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

Page: 1
Go to End
Author Topic: 2029 Views
  • spierala
    Avatar
    Community Member
    80 Posts

    Translatable DataObjects do not work Link to this post

    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

  • lerni
    Avatar
    Community Member
    80 Posts

    Re: Translatable DataObjects do not work Link to this post

    hi

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

    but i guess there is another way?

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Translatable DataObjects do not work Link to this post

    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.

  • spierala
    Avatar
    Community Member
    80 Posts

    Re: Translatable DataObjects do not work Link to this post

    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

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Translatable DataObjects do not work Link to this post

    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...

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Translatable DataObjects do not work Link to this post

    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.

  • elgordo
    Avatar
    Community Member
    69 Posts

    Re: Translatable DataObjects do not work Link to this post

    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

    2029 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.