Jump to:

17452 Posts in 4473 Topics by 1971 members


SilverStripe Forums » Archive » Hack: Pretty URLs and Language Switcher

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1791 Views
  • Skipper
    Community Member
    16 Posts

    Hack: Pretty URLs and Language Switcher Link to this post

    So here's my solution to the problem that so many have: I wanted pretty URLs for a multilanguage site, no ugly ?lang parameters.

    Four changes:

    1. Tell SS which languages you are ready to take

    Being a complete noob to SS, I had to hardcode this. In your _config.php add this:

    global $allowed_i18n;
    $allowed_i18n = array('en', 'de', 'fr');

    2. Have SS understand which language to present

    I defined that the first URL segment is the language element: http://mysite.com/en/page/
    /sapphire/main.php strips the URL apart, checks if the first segment is in the definition list above and sets the language.
    This has two consequences: You can not use a language called 'db' because this is needed for the admin database
    maintenance operations, and you can not have page names that are the same as your language codes.

    Add this immediately before the default director rules (near line 122 in SS 2.2.2) in /sapphire/main.php

    if(substr($url,0,strlen($baseURL)) == $baseURL) $url = substr($url,strlen($baseURL));

    global $allowed_i18n;
    if(sizeof($allowed_i18n) > 0) {
    $tmp_url = preg_replace( array( '/\/+/','/^\//', '/\/$/'),array('/','',''),$url);
    $tmp_urlParts = split('/+', $tmp_url);
    $lng = array_shift($tmp_urlParts);
    $bSetLang = false;
    if(in_array(strtolower($lng), $allowed_i18n)) {
    $url = "/".join('/', $tmp_urlParts);
    $_REQUEST['url'] = $url;
    $bSetLang = true;
    if(isset($_GET['lang']) && in_array(strtolower($_GET['lang']), $allowed_i18n)) {
    $lng = $_GET['lang'];
    $bSetLang = true;
    if($bSetLang) {
    $_GET['lang'] = $lng;
    $_REQUEST['lang'] = $lng;

    3. Prepare language switcher

    Simply add the following function to your page controller

    function LanguageSwitcher() {
    $langs = i18n::get_existing_content_languages();
    $data = new DataObjectSet();
    foreach(array_keys($langs) as $code) {
    if($code == Translatable::current_lang()) {
    $page = Translatable::get_one_by_lang("SiteTree", $code, "`SiteTree`.ID = " . Controller::curr()->ID);
    $data->push(new ArrayData(array('name' => i18n::get_language_name($code, true),
    'link' => Director::protocolAndHost() . Director::baseURL() . "$code/" . $page->URLSegment,
    'code' => $code)));
    return $data;

    4. Integrate the language switcher into your template

    I use the following snippet in an include file

    <ul class="LanguageChooser">
    <% _t('VIEWIN', 'View this site in:') %>
    <% control LanguageSwitcher %>
    <li><a href="$link">$name</a></li>
    <% end_control %>

    As you will notice I have "borrowed" from others. Standing on the shoulders of giants ...

    Please comment and improve.


    [edit: changed the main.php hack - setting $_GET['lang'] as well]
    [ANOTHER edit: changed the main.php hack - doesn't reset the admin language anymore. that was bad for editing translations]

  • Ingo
    Forum Moderator
    801 Posts

    Re: Hack: Pretty URLs and Language Switcher Link to this post

    Awesome, yeah thats the approach we're tending towards as well - especially when statically caching pages we ran into problems with "lang" as a GET param. Sam has used the new RequestHandlingData functionality in trunk to specify the "code" part as the first chunk of a URL for the ModelAsController rule - no need to hack up main.php.
    I think it still requires the definition of every language separately, emailed Sam to shine some light on what he did.

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.