I continue testing this release candidate. Say we have a site with two languages. I've found that we can change the site locale (the Translatable one) by simply offering a link to a page in one language or another, that's nice. Here's my modified LanguageSwitcher function:
function LanguageSwitcher() {
if($this->URLSegment == "Security") return "";
$langs = Translatable::get_existing_content_languages();
$data = new DataObjectSet();
foreach(array_keys($langs) as $code) {
if($code == Translatable::get_current_locale()) {
continue;
}
if (!$this->hasTranslation($code)) {
// If no translation, link to home page
//echo "noTrans";
$data->push(new ArrayData(array('name' => i18n::get_language_name(i18n::get_lang_from_locale($code), true),
'link' => Director::baseURL() . Translatable::get_homepage_urlsegment_by_locale($code),
'code' => $code)));
} else {
// If translation, link to translated page
//echo "yesTrans";
$page = $this->getTranslation($code);
$data->push(new ArrayData(array('name' => i18n::get_language_name(i18n::get_lang_from_locale($code), true),
'link' => Director::baseURL() . $page->URLSegment,
'code' => $code)));
}
}
return $data;
}
Nevertheless, if we don't append ?locale=$code to the link and use $_GET['locale'] in Page_controller init(), the i18n locale does not change and, for example, modules do not get translated or date format do not localize:
global $allowed_locales;
if((isset($_GET['locale'])) && array_key_exists($_GET['locale'], $allowed_locales)) {
Translatable::set_current_locale($_GET['locale']);
i18n::set_locale($_GET['locale']);
} else {
Translatable::set_current_locale(self::get_current_locale());
i18n::set_locale(self::get_current_locale());
}
It bothers me because I'd like to always have ‘pretty’ URLs, and for the moment I'm obliged to pass the locale at least once when changing languages. Also, if an user gets directly to a page in the non-default language, with a link not containing explicitly the locale, the content of the page will be for sure translated, but the translatable entities won't.
I know Translatable and i18n locales are different things, but there's certainly a proper way of setting both locales at the same time, could somebody help? I'm sure I'm missing something.
Thanks in advance, and congrats for the wonderful work.