How to default to another language based on domain
Asked Answered
C

3

5

i'm using Joomla with the JoomFish translation component. This website has german and chinese translations. What i'm trying to figure out is how to get the .de domain to default to the german language translation ( which would load if visited as domain.com/de or domain.com/cn ).

Does anyone know a way to do this with maybe .htaccess ( some kind of redirect )? Or possibly PHP? Maybe set some kind of session variable based on the domain (PHP_URL_HOST) ?

Right now i have apache2 setup with the wwww.domain.com as the main virtual host, and the .de and .cn as aliases.

Collinsworth answered 24/1, 2011 at 17:24 Comment(1)
That Thank You appears quite big ;)Sparge
A
5

Welcome to SO!

Provided, your Apache has mod_setenvif enabled, add this to your .htaccess file:

# Site accessed via "example.de" or "example.cn"
SetEnvIf Host "\.de$" SITE_LANGUAGE=de
SetEnvIf Host "\.cn$" SITE_LANGUAGE=zh

# URL dependent
SetEnvIf Request_URI "^/de/" SITE_LANGUAGE=de
SetEnvIf Request_URI "^/cn/" SITE_LANGUAGE=zh

Then, in your PHP script you can query SITE_LANGUAGE:

switch($_SERVER['SITE_LANGUAGE']) {
   case 'de':
      // german stuff
   case 'zh':
      // chinese stuff
}
Allure answered 24/1, 2011 at 17:29 Comment(1)
Thank you! This pretty much did it. All i had to do is edit /plugins/system/jfrouter.php at about line 145 added your code with the same logic as line 145.Collinsworth
C
3

Thank you! After adding the htaccess code, all I had to do is edit /plugins/system/jfrouter.php at about around 145:

if (isset($_SERVER['SITE_LANGUAGE'])) {
    switch($_SERVER['SITE_LANGUAGE']) {
        case 'de':
            $client_lang = 'de';
            $lang_known = true; 
            JRequest::setVar('lang', 'de' );
            break;
        case 'zh':
            $client_lang = 'zh';
            $lang_known = true;
            JRequest::setVar('lang', 'zh' );
            break;
    }
}
Collinsworth answered 24/1, 2011 at 18:28 Comment(0)
E
0

What if you had a select case in php based on a get request?

Something like domain.com/?lang=de in conjunction with

$lang = "default";
if (isset($_GET['lang'])) {
    $lang = $_GET['lang'];
}   
if ($lang != "default") {
    if($lang == "de") {
        ....

in your code. .... would be where you load your translator or language resource.

Eudoca answered 24/1, 2011 at 17:32 Comment(1)
I guess that isn't a switch case but you get the idea.Eudoca

© 2022 - 2024 — McMap. All rights reserved.