Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Set locale within the URL for Translatable

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

Page: 1
Go to End
Author Topic: 3085 Views
  • bschmitt
    Avatar
    Community Member
    22 Posts

    Set locale within the URL for Translatable Link to this post

    Hi There!

    I need to extend Silverstripe to set the locale within the URL: e.g. /language-country/pagename

    - /de-de/support
    - /de-at/support
    - /en-us/support

    The following code is just a quick implementation but it works for now…

    1. First I had to add a new controller for extending the "ModelAsController" to handle the changed URL syntax:

    http://pastie.org/681796

    2. Add a rule for calling the new controller within _config.php:

    http://pastie.org/681826

    3. Rewrite the link targets (now with locale) within Page.php:

    http://pastie.org/681835

    4. To allow setting the same page name in translated pages, I had to uncomment some lines within the core (SiteTree.php), which is definitely not the way it should be done. So if there are any new ideas please let me know!

    http://pastie.org/681846

    It took a while to figure out that Versioned::reading_stage is not set when I use my "TranslatedModelAsController" -- if I use "ModelAsController" everything works as expected --therefore I added some lines with a small hack I found in the Silverstripe forums: TranslatedModelAsController > Line 17-22

    Any improvements and comments are welcome!

    Best Bjoern

    Attached Files
  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Set locale within the URL for Translatable Link to this post

    Good to see its possible without too many modifications, thanks for sharing!
    I think you'll get into trouble with the new nestedurls feature in trunk/2.4 though, so thats a "known limitation" I guess.
    Would be nice if you could just "shift off" the first part of the URL after setting the locale, and re-initiating the request as normal.

  • bschmitt
    Avatar
    Community Member
    22 Posts

    Re: Set locale within the URL for Translatable Link to this post

    Thanks Ingo for your hint!

    Is the nestedurls feature in trunk stable enough to have a look on it to adjust my code? I'll have a try to "shift off" the first part of the url and do the rest as normal.

    Best regards and greetings to NZ,
    Bjoern

  • Ironcheese
    Avatar
    Community Member
    33 Posts

    Re: Set locale within the URL for Translatable Link to this post

    Sorry to dig this up again, but i just tried your approach with 2.4 and it throws an error:

    Fatal error: Call to undefined method SiteTree::get_by_url() in /[...]/mysite/TranslatedModelAsController.php on line 96

    Or is there another fix for this issue there? I already tried the pretty_i18n_url recipie and that didnt worked either (on 2.4).

    Since i'm generating 5 translations for every new page automaticlly it looks pretty stupid with urls like
    "/maincategory-en_US/subcategory-en_US/page-en_US/" :S

    awesome would be something like
    "/en/maincategory/subcategory/page/"

    cheers

  • bschmitt
    Avatar
    Community Member
    22 Posts

    Re: Set locale within the URL for Translatable Link to this post

    Yes in 2.4 this proposal causes some errors. I will work on a version for 2.4 when I have time.

    If you want to help, here are the two tasks to do:
    1. Get and set locale from URL => something like that http://pastie.org/1003541
    2. Allow same URL for pages in different langauges, have a look to the

    Please let me know if you were successful

    Thanks and best,
    Bjoern

  • Ironcheese
    Avatar
    Community Member
    33 Posts

    Re: Set locale within the URL for Translatable Link to this post

    Yeah, would be awesome if you could make a 2.4 version. =)

    I'm afraid I'm not that big of a help here, but i will try my best! Your little list of task is a good start, we will see how far i come ^^

    cheers

  • WEB 053
    Avatar
    Community Member
    1 Post

    Re: Set locale within the URL for Translatable Link to this post

    This subject is a very interesting option... I also need this way of URL rewriting... Is there already more information about it for SS 2.4.0?

  • freenet
    Avatar
    Community Member
    22 Posts

    Re: Set locale within the URL for Translatable Link to this post

    There is a working version for 2.4 version??

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