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


8 Posts   3137 Views


4 November 2009 at 6:42am Community Member, 22 Posts

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:


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


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


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!


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

6 November 2009 at 8:23am Forum Moderator, 801 Posts

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.


7 November 2009 at 12:17am Community Member, 22 Posts

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,


12 June 2010 at 10:10am Community Member, 33 Posts

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



14 June 2010 at 8:58pm Community Member, 22 Posts

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


15 June 2010 at 4:09am Community Member, 33 Posts

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


WEB 053

23 July 2010 at 5:23pm Community Member, 1 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?


18 October 2010 at 11:50pm Community Member, 22 Posts

There is a working version for 2.4 version??