Skip to content

Instantly share code, notes, and snippets.

@Zauberfisch
Last active December 23, 2015 13:41
Show Gist options
  • Save Zauberfisch/9226142 to your computer and use it in GitHub Desktop.
Save Zauberfisch/9226142 to your computer and use it in GitHub Desktop.
SilverStripe Translatable defaults snippets
<?php
// file: mysite/_config.php
...
// limit the CMS user to the following locales
Translatable::set_allowed_locales(array(
'de_DE',
'en_US',
'es_ES',
'it_IT',
));
// set default locale
i18n::set_locale('de_DE');
Translatable::set_default_locale('de_DE');
# file: mysite/_config/config.yml
...
Controller:
extensions:
- 'TranslatableControllerExtension'
<?php
// file: mysite/code/TranslatableControllerExtension.php
class TranslatableControllerExtension extends Extension {
/**
* save the current controller to ensure we have access to it,
* this is necessary because Security crates a fake Page_Controller to render templates
* @var Controller
*/
protected static $actual_current_controller;
/**
* hook into Controller->init() to set current locale
*/
public function onBeforeInit() {
static::$actual_current_controller = $this->owner;
$locale = Session::get('current_locale');
if ($locale && !static::$actual_current_controller->is_a('ContentController')) {
// current controller is a static controller, such as Security
// lets set the default locale to what we have stored in session
// the reason we set default locale rather than current local is, that default
// locale will only be used if there is no ?locale=xx_XX
// so we keep locale switching in tact
try {
Translatable::set_default_locale($locale);
} catch (InvalidArgumentException $e) {
}
}
$locale = Translatable::get_current_locale();
i18n::set_locale($locale);
Session::set('current_locale', $locale);
}
/**
* Returns a list of all locales that have at least 1 Page, with a Link to the current Page in all languages if available, or the home page otherwise.
* Example usage in template: <% loop getAvailableLocales %><a href="$Link" title="$NativeTitle">$ShortCode</a><% end_loop %>
* Eg: The Page has German and English and you are on /about-us-de it will give you: "DE EN" with the links to /about-us-de and /about-us-en
*/
public function getAvailableLocales() {
$return = ArrayList::create();
$isStaticRoute = !static::$actual_current_controller->is_a('ContentController');
$request = static::$actual_current_controller->getRequest();
$page = $isStaticRoute ? null : static::$actual_current_controller->data();
foreach (Translatable::get_existing_content_languages('SiteTree') as $code => $val) {
$short = i18n::get_lang_from_locale($code);
$link = false;
if ($isStaticRoute) {
// static controller, just preserve the URL and append ?locale=xx_XX
$getVars = $request->getVars();
unset($getVars['locale'], $getVars['url']);
$getVars['locale'] = $code;
$link = array($request->getURL());
foreach ($getVars as $k => $v) {
$link[] = "?$k=$v";
}
$link = call_user_func_array('Controller::join_links', $link);
} elseif ($page && $page->hasMethod('hasTranslation') && $page->hasTranslation($code) && $translation = $page->getTranslation($code)) {
$link = $translation->Link();
}
if (!$link) {
// no link, simply create one to the homepage of the locale
$homeLink = Translatable::get_homepage_link_by_locale($code);
$link = $homeLink ? $homeLink : "/home?locale=$code";
}
$return->push(ArrayData::create(array(
'Code' => $code,
'ShortCode' => $short,
'Title' => i18n::get_language_name($short, false),
'NativeTitle' => i18n::get_language_name($short, true),
'Link' => $link,
'Current' => $code == Translatable::get_current_locale(),
)));
}
return $return;
}
}
<%-- file: mysite/templates/Includes/LanguageNavigation.ss or themes/yourtheme/Includes/LanguageNavigation.ss --%>
<% loop getAvailableLocales %>
<%-- $Title: full name of the local in english. eg: German --%>
<%-- $NativeTitle: full name of the local in nativ language. eg: Deutsch --%>
<%-- $Code: locale code. eg: de_DE --%>
<%-- $ShortCode: language code. eg: de (will also be de for de_AT) --%>
<a href="$Link" class="<% if $Current %>current<% end_if %>" title="$NativeTitle">$ShortCode</a>
<% end_loop %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment