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.

Customising the CMS /

Set locale within the URL for Translatable


Reply


8 Posts   3307 Views

Avatar
bschmitt

Community Member, 22 Posts

4 November 2009 at 6:42am

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:

[url]http://pastie.org/681796[/url]

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

[url]http://pastie.org/681826[/url]

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

[url]http://pastie.org/681835[/url]

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!

[url]http://pastie.org/681846[/url]

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
Avatar
Ingo

Forum Moderator, 801 Posts

6 November 2009 at 8:23am

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.

Avatar
bschmitt

Community Member, 22 Posts

7 November 2009 at 12:17am

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

Avatar
Ironcheese

Community Member, 33 Posts

12 June 2010 at 10:10am

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

Avatar
bschmitt

Community Member, 22 Posts

14 June 2010 at 8:58pm

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

Avatar
Ironcheese

Community Member, 33 Posts

15 June 2010 at 4:09am

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

Avatar
WEB 053

Community Member, 1 Post

23 July 2010 at 5:23pm

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?

Avatar
freenet

Community Member, 22 Posts

18 October 2010 at 11:50pm

There is a working version for 2.4 version??