1

I have just recently installed the silverstripe Translatable to handle a multilingual site for us. I think i have set everything up correctly but cannot be sure. Maybe i'm missing something or maybe i misunderstand the module.

In my site _config.php i have the following

i18n::set_locale('en_US');
i18n::set_locale('fr_FR');
Translatable::set_default_locale('en_US');

SiteTree::add_extension('Translatable');
SiteConfig::add_extension('Translatable');

I have gone thru and created a translation of our home page. I can navigate to the translated page no problem. I can also verify that both the home page and the translation realize they are linked with the following code in Page.ss

<% if Translations %>
    <% loop Translations %>
        $Locale.RFC1766
        $Link   
    <% end_loop %>
<% end_if %>

I also added lang="$ContentLocale" xml:lang="$ContentLocale" xmlns= "http://www.w3.org/1999/xhtml">

As expected on the french verison of the home page the US version shows and vice-versa. However when I access the site from a french computer with a french browser it goes straight to the english page. I would have thought that this would be handled automatically by either i18n part of silverstripe or by translatable. Do i need to handle this myself or am I missing something?

Answer based on Barry's Answer I ended up with the following lines of code in my _config.php

$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE']; //Get the languag from the client
$lang = substr($lang,0,strpos($lang,','));//Strip out the unnecesary stuff after the comma
$lang = str_replace("-","_",$lang);//Replace the hyphen with an underscore.
Translatable::set_default_locale($lang);//Set locale
Andrew MacNaughton
  • 783
  • 2
  • 6
  • 21
  • 1
    `Translatable::set_default_locale()` is for setting the _default_ locale, while `Translatable::set_current_locale()` is for setting the _current_ locale which is used to filter SQL queries by the current locale. You can also use `Translatable::set_allowed_locales()` to add an array of allowed locales on that site. – wmk Mar 31 '16 at 06:07

1 Answers1

1

With silverstripe Translatable there are two key items that answer your question.

  1. each page in the system has a unique URL
  2. You set the default locale

Strictly speaking for both one and two you can pass the locale in as a _GET variable - but that isn't what your visitors will do.

So the simple answer is no Translatable alone will not determine the locale to show - but you can do so by changing the default locale based on any of the methods...

Once you have this just set it within _config.php with...

Translatable::set_default_locale(<locale>);
Community
  • 1
  • 1
Barry
  • 3,303
  • 7
  • 23
  • 42