Jump to:

23358 Posts in 17875 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Translatable custom data object?

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

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

Page: 1 2 3
Go to End
Author Topic: 3375 Views
  • spyrosk
    Avatar
    Community Member
    16 Posts

    Re: Translatable custom data object? Link to this post

    I believe I have the right stuff on my _config.php as I am able to translate pages and view the sitetree per language. I haven't added yet the language flags as I am trying to make this work in the back-end first.
    I am testing code to implement this functionality right now:
    Create ProjectPage's with a hasOne relationship to ProjectCountry.
    ProjectCountry is a dataObject with 2 fields, name and countryCode.
    And I want to create a couple of entries in the countryCode, translated both in English and Greek. Then when a user tries to add a ProductPage I want him to be able to select the corresponding ProjectCountry while viewing the list in his selected language.
    Is this making sense to you?

    Thank you again.

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Translatable custom data object? Link to this post

    That is great! My advice would be to focus entirely on a solution with pages right now - so complete what you have to the point that the website can operate in different languages.

    As I said I cannot help with translation of dataobjects yet because I have only briefly looked at the module... that is why I advise using the stuff tried, tested and documented with pages right now

  • spyrosk
    Avatar
    Community Member
    16 Posts

    Re: Translatable custom data object? Link to this post

    Thank you Barry, well I could not get it to work as I wanted using pages so I'll keep trying with translatableModelAdmin.
    Have you implemented something like this in the past using Pages?
    In any case thank you very much!

    Spyros

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Translatable custom data object? Link to this post

    I have used translations to provide plain page translations. But I do need to start having dataobject translatable too - but not yet!

    Please post back your solution when it's working I am very interested in this area.

  • spyrosk
    Avatar
    Community Member
    16 Posts

    Re: Translatable custom data object? Link to this post

    I will post it since I haven't found any tutorial that accomplishes this and I suppose there are a lot of people interested.

  • hammuh
    Avatar
    Community Member
    15 Posts

    Re: Translatable custom data object? Link to this post

    Hi Spyrosk,

    I'm in the same situation. The page translation is no problem with :

    Object::add_extension('SiteTree', 'Translatable');
    Object::add_extension('SiteConfig', 'Translatable');
    Translatable::set_allowed_locales( array('nl_NL', 'en_US', 'es_ES'));

    But the translations of the DataObject managed in ModelAdmin is much more difficult.

    A possible solution is adding a field to your DataObjects called "Lang" or something like that. Make it a dropdown field with the available languages. In my case nl_NL, en_EN and es_ES. Everytime you create a new DataObject, you select what language it is.

    In your template you can put this:

    <% control ObjectByLang %>
    <!-- whatever you want -->
    <% end_control %>

    In your controller:

    public function ObjectByLang() {
    $currentReadingLocale = Translatable::get_current_locale();

    $MyObject= DataObject::get(
       $callerClass = "classname",
       $filter = "Lang = '$currentReadingLocale'",
       $sort = "",
       $join = "",
       $limit = ""
    );
    return $MyObject;
    }

    Only with this solution you have to create 3 different DataObject instead of one.

    If you already have another solution, I'll hope you ost it back!

  • spyrosk
    Avatar
    Community Member
    16 Posts

    Re: Translatable custom data object? Link to this post

    Hi hammuh,
    first of all I'm sorry I didn't reply for so long but I haven't really solved this. The workaround I used was to create two separate installations of SS on different directories, one for each language.
    This means that now I have to edit the code twice for each change, but it is easier to edit content that doesn't apply to both languages, i.e. some data should only be visible to visitors from country X.
    I will try your solution in the future where a 1 to 1 relationship (on translated content) is required.
    Thank you very much for your help.
    Spyros

  • Chris_Bryer
    Avatar
    Community Member
    35 Posts

    Re: Translatable custom data object? Link to this post

    hey spyrosk,
    you may have the same problem others (including me) have had.
    this requires 2 patches, entered in bug tracker here

    -Chris

    3375 Views
Page: 1 2 3
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.