4714 Posts in 1056 Topics by 671 members
Jump to:If this is your first visit, you will need to register before you can post. However, you can browse all messages below.
| Page: 1 | go to end | Reply | |
| Author | Topic: Hack: Pretty URLs and Language Switcher | 307 views |

8 September 2008 at 7:02am
So here's my solution to the problem that so many have: I wanted pretty URLs for a multilanguage site, no ugly ?lang parameters.
Four changes:
1. Tell SS which languages you are ready to take
Being a complete noob to SS, I had to hardcode this. In your _config.php add this:
2. Have SS understand which language to present
I defined that the first URL segment is the language element: http://mysite.com/en/page/
/sapphire/main.php strips the URL apart, checks if the first segment is in the definition list above and sets the language.
This has two consequences: You can not use a language called 'db' because this is needed for the admin database
maintenance operations, and you can not have page names that are the same as your language codes.
Add this immediately before the default director rules (near line 122 in SS 2.2.2) in /sapphire/main.php
global $allowed_i18n;
if(sizeof($allowed_i18n) > 0) {
$tmp_url = preg_replace( array( '/\/+/','/^\//', '/\/$/'),array('/','',''),$url);
$tmp_urlParts = split('/+', $tmp_url);
$lng = array_shift($tmp_urlParts);
$bSetLang = false;
if(in_array(strtolower($lng), $allowed_i18n)) {
$url = "/".join('/', $tmp_urlParts);
$_REQUEST['url'] = $url;
$bSetLang = true;
}
if(isset($_GET['lang']) && in_array(strtolower($_GET['lang']), $allowed_i18n)) {
$lng = $_GET['lang'];
$bSetLang = true;
}
if($bSetLang) {
$_GET['lang'] = $lng;
$_REQUEST['lang'] = $lng;
}
}
3. Prepare language switcher
Simply add the following function to your page controller
4. Integrate the language switcher into your template
I use the following snippet in an include file
As you will notice I have "borrowed" from others. Standing on the shoulders of giants ...
Please comment and improve.
Cheers.
[edit: changed the main.php hack - setting $_GET['lang'] as well]
[ANOTHER edit: changed the main.php hack - doesn't reset the admin language anymore. that was bad for editing translations]
Last edited: 13 September 2008 at 12:28am

12 September 2008 at 9:46am
Awesome, yeah thats the approach we're tending towards as well - especially when statically caching pages we ran into problems with "lang" as a GET param. Sam has used the new RequestHandlingData functionality in trunk to specify the "code" part as the first chunk of a URL for the ModelAsController rule - no need to hack up main.php.
I think it still requires the definition of every language separately, emailed Sam to shine some light on what he did.
| 307 views | |||
| go to top | Reply |