Skip to content

Instantly share code, notes, and snippets.

@UndefinedOffset
Last active August 29, 2015 14:01
Show Gist options
  • Save UndefinedOffset/8af174c9d52cab2d02bf to your computer and use it in GitHub Desktop.
Save UndefinedOffset/8af174c9d52cab2d02bf to your computer and use it in GitHub Desktop.
SilverStripe Translatable with url rules a simple version of https://github.com/webbuilders-group/silverstripe-translatablerouting/
#### Add the following to the .htaccess in the SilverStripe root just before the default RewriteConds for routing to framework/main.php ####
RewriteCond %{REQUEST_URI} ^/fr/(.*)$ #You may need to play with this depending on what the base url of your site is
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule .* framework/main.php?url=%1&locale=fr_FR&%{QUERY_STRING} [L]
#### Add the following just bellow the first default rewrite condition "RewriteCond %{REQUEST_URI} ^(.*)$" ####
RewriteCond %{REQUEST_URI} !^/fr/(.*)$ #Should match the path in the first line of the first block
<?php
/**
* Return the link for this {@link SiteTree} object, with the {@link Director::baseURL()} included.
* @param {string} $action Optional controller action (method). Note: URI encoding of this parameter is applied automatically through template casting, don't encode the passed parameter. Please use {@link Controller::join_links()} instead to append GET parameters.
* @return {string}
*/
public function Link($action=null) {
if($this->Locale=='fr_FR') {
return Controller::join_links(Director::baseURL(), 'fr', $this->RelativeLink($action));
}
return parent::Link($action);
}
/**
* Return the link for this {@link SiteTree} object relative to the SilverStripe root. By default, it this page is the current home page, and there is no action specified then this will return a link to the root of the site. However, if you set the $action parameter to TRUE then the link will not be rewritten
* and returned in its full form.
* @param {string} $action See {@link Link()}
* @return {string}
*
* @uses Translatable::get_homepage_link_by_locale()
*/
public function RelativeLink($action=null) {
if($this->ParentID && self::nested_urls()) {
$base=$this->Parent()->RelativeLink($this->URLSegment);
} else {
$base=$this->URLSegment;
}
//Unset base for homepage URLSegments in their default language. Homepages with action parameters or in different languages need to retain their URLSegment. We can only do this if the homepage is on the root level.
if(!$action && $base==Translatable::get_homepage_link_by_locale($this->Locale) && !$this->ParentID) {
$base = null;
}
return Controller::join_links($base, '/', $action);
}
?>
<?php
public function init() {
parent::init();
// If we've accessed the homepage as /home/, then we should redirect to /.
if($this->dataRecord && $this->dataRecord instanceof SiteTree && $this->dataRecord->Locale=='fr_FR' && RootURLController::get_homepage_link()==trim($this->dataRecord->RelativeLink(true), '/') && $_GET['url']!='' && (!isset($this->urlParams['Action']) || !$this->urlParams['Action']) && !$_POST && !$_FILES && !$this->redirectedTo()) {
$getVars=$_GET;
unset($getVars['url']);
unset($getVars['locale']);
if($getVars) {
$url='fr/?'.http_build_query($getVars);
}else {
$url='fr/';
}
$this->redirect($url, 301);
return;
}
}
?>
@UndefinedOffset
Copy link
Author

Note in my code I'm using the locale fr_FR and the language in the url as fr (though it is kinda irrelevant in this case), so replace as needed.

For google sitemaps you should be able to pull the routes, class, and template from translatablerouting.

This could be used for the default locale though I don't recommend it, this should be used for your alternate languages if you want the default language of the site to not have the language or locale in the url. If you want all of the site's languages to appear in the url I highly recommend you use translatablerouting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment