Last active
December 23, 2015 13:41
-
-
Save Zauberfisch/9226142 to your computer and use it in GitHub Desktop.
SilverStripe Translatable defaults snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file: mysite/_config/config.yml | |
... | |
Controller: | |
extensions: | |
- 'TranslatableControllerExtension' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment