Hi,
I am currently building my first multilingual Silverstripe site. Here is an alternative way (compare recipes:pretty_i18n_urls) to build pretty URLs of the kind en.domain.com/news/ instead of domain.com/news/?lang=en
It does not require any changes in the Silverstripe core code, but you may have to add some DNS entries for your domain.
All the magic happens in the .htaccess file. Simply add the red lines to your .htaccess file and adjust them to your needs:
### SILVERSTRIPE START ###
RewriteEngine On
RewriteBase /
# MULTILINGUAL SUBDOMAIN SETTINGS
RewriteCond %{HTTP_HOST} ^(en|mi)\.domain\.com
RewriteRule ^(.*) $1?lang=%1&%{QUERY_STRING}
RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)$
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
### SILVERSTRIPE END ###
(en|mi) are the language codes for the available translations of your site. In this case it's English and MÄÂori, for e.g. English, French and German just use (en|fr|de) instead.
In the same line also replace \.domain\.com with your domain name.
This simply translates the URL en.domain.com/news/ to domain.com/news/?lang=en. No links or references to files and images need to be changed.
URLs such as www.domain.com, domain.com, www.domain.com/news and domain.com/news will display your default language.
I had to add CNAME entries to my DNS records to make it work, i.e.:
Name: mi.domain.com
TTL: 3600
Type: CNAME
Value: domain.com. (add the trailing dot ".")
And the same for en.domain.com
(It may take a couple of minutes to a few hours until these new DNS entries are propagated and you can see the effect.)
This method solves a couple of problems I had with multilingual pages:
* The URL is unique for each language (no browser caching problems and no need to set "HTTP::set_cache_age(0);" in the _config.php file which would slow down everything)
* I would like to use the existing pretty_i18n_urls recipe but I never liked that "you need to have distinct page names for all languages. /en/news and /fr/news will give you the news page from the default language. SS does not honor language/page combinations, it.seems" It is no problem to have the same page name for all languages with the method described here.
* I quite like that I don't have to touch core code (or any PHP code).
This method is not yet much tested but so far it works for me without problems. Any comments and suggestions are welcome.
Just as a thought (this is completely untested): I think with another .htaccess rewrite rule it should be quite easy to enable translated images. You would just have to check a folder that contains language specific images: if the image exists then use it, otherwise use the image of the default language.
I hope this is helpful.
Cheers!
Anatol