2

I have one site with 2 domains one example.de and one example.com its a multilanguage site, so i have urls like this example.de/de/content and example.de/en/content etc, and same on com domain.

Default language is de for this two domains, but for example.com i need english to be default language.

i thought i can change it in settings.php with $conf['language_default'] like this:

$conf['language_default'] = (object) array(
  'language' => 'en', 'name' => 'English', 'native' => 'English',
  'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '',
  'domain' => 'www.example.com', 'prefix' => '', 'weight' => 0, 'javascript' => ''
);

its working but for both domains... . also i managed to redirect users to en on example.com (my home page is a node with nid 89)

function MYMODULE_node_view($node) {

        if ($node->nid == 89) {
            if ($_SERVER['SERVER_NAME'] == 'www.example.com') {
             header('Location: http://www.example.com/en');
            }
        }

    }

but its not an option

Is there any solution to change default language for my example.com domain?

4 Answers 4

6
+100

Though I'm surprised if there's not an option in the user interface, according to language_default, it appears you could set the default language as a $conf variable in the shared settings.php file.

Something like

settings.php:

switch($_SERVER['SERVER_NAME']){

  case 'www.example.com':

    $conf['language_default'] = (object) array(
      'language' => 'en', 'name' => 'English', 'native' => 'English',
      'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '',
      'domain' => 'www.example.com', 'prefix' => '', 'weight' => 0, 'javascript' => ''
    );

    // Example per-domain cache prefix if using memcache
    $conf['memcache_key_prefix'] = 'en_';

  break;
  case 'www.example.de':

    $conf['language_default'] = (object) array(
      'language' => 'de', 'name' => 'German', 'native' => 'German',
      'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '',
      'domain' => 'www.example.de', 'prefix' => '', 'weight' => 0, 'javascript' => ''
    );

    // Ensures cache varies by domain
    $conf['memcache_key_prefix'] = 'de_';

  break;

}

Update: Added example memcache cache key prefix setting - to ensure cache varies per domain, if required.

7
  • Sounds good, but what about caches?
    – Mołot
    Jul 12, 2013 at 8:07
  • i can turn off cache with cacheexclude module for my main page, thanks for answer gonna try it now Jul 12, 2013 at 8:10
  • @neok I'm not saying caches will mess up. I only wanted to say you need to test it thoroughly, unless David Thomas knows they will or will not work OK. If possible, leave them be.
    – Mołot
    Jul 12, 2013 at 8:15
  • 1
    Glad to hear it. As long as cache varies by / is set per domain, it shouldn't be any issue. Jul 12, 2013 at 9:32
  • @DavidThomas I would add default: before case 'www.example.de':. Without break;. For future, to be sure there will be some language set in case of domain name change / direct IP access et cetera.
    – Mołot
    Jul 12, 2013 at 12:59
1

Why do you want to change the default language? It would be enough to change current language depending on the domain. I think the best way is, to alter the current language of all types with the function: hook_url_language_init. This would be an code example:

function custom_api_language_init() {
  // Current path
  $url = $_SERVER['SERVER_NAME'];
  $domains = array('www.example.com' => 'en');

  // Global language object and get languages
  global $language;
  $languages = language_list();


  if (isset($domains[$url])) {

    $langcode = $domains[$url]

    $new_language         = $languages[$langcode];
    $new_language->domain = $url;

    // Set url options
    $options['language'] = $new_language;
    $types = language_types();

    // Set all language types and language domains
    foreach ($types as $type) {
      $GLOBALS[$type] = $new_language;
      $GLOBALS[$type]->domain = $new_language->domain;
    }
  }
}

Here you find a similar thread: How to set multiple domains for one language

4
  • It is possible some modules on his site uses default language for their tasks, not only current one. Making them behave as people expect them to is perfectly valid reason. Also, if he wants to allow users change language in their profile, and leave default to "use site's default", changing current language makes little sense.
    – Mołot
    Jul 17, 2013 at 7:36
  • But why use a default language when it's not the default because it changes?
    – user5950
    Jul 17, 2013 at 7:40
  • 1
    It's default per domain, not default overall. Like different default image in different image fields - no one questions that, right? ;)
    – Mołot
    Jul 17, 2013 at 7:41
  • @Mołot Ok, I see your point.
    – user5950
    Jul 17, 2013 at 7:43
0

I wrote it in a comment, but I believe it may be the correct way to do it, so I post it as an answer. In

/admin/config/regional/language/configure

you can set both User interface text language and Content language based on URL, session, user preference, browser language and default language.

-1

in your template.php use the following code. :

function THEME_preprocess_page(&$vars) {
   global $language;
   if ($_SERVER['SERVER_NAME'] == 'www.example.com' && $language->language != 'de') {
       header('Location: http://www.example.com/en');
   }
}
3
  • as you can see i did the same, its working, but when i change language to de i will be redirected to en again Jul 10, 2013 at 12:03
  • I have update my answer. it work like this ? Jul 10, 2013 at 12:10
  • default language is de so this wont work(i need to set en as default language for com domain ... thats the problem Jul 10, 2013 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.