Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to default to another language based on domain

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.

like image 948
Michael Avatar asked Jan 24 '11 17:01

Michael


2 Answers

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
}
like image 176
Linus Kleen Avatar answered Nov 01 '22 10:11

Linus Kleen


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;
    }
}
like image 41
Michael Avatar answered Nov 01 '22 09:11

Michael