in drupal 5, this was so easy. i just enabled english as default language for my admin account but default language for the site is Chinese.

it seems, in drupal 6, i can not do this any more.

I have tried to set

1. Default Language is Chinese.
2. "Path prefix with language fallback"
3. Prefix for English is "en"
4. No prefix for Chinese.

however, the default language to anonymous users is still English without any prefix.

Could anyone tell me, is there anything i did wrong? or this is a problem for everyone?

Thanks!

Comments

spacereactor’s picture

i want to do the something, anyone got advise?

toemaz’s picture

I encountered the same problem for Chinese, but also with other languages which have a dash in the iso code like pt-pt, pt-br, zh-hans, ... Looking for an answer as well.

toemaz’s picture

toemaz’s picture

Backport from D7 patch to D6
Edit: updated for #53

/**
 * Identify language from the Accept-language HTTP header we got.
 */
function language_from_browser() {
  // Get enabled language and create an array of valid language objects.
  $languages = language_list('enabled');
  $languages = $languages['1'];
  
  if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    return;
  }

  // RFC 2616 (section 14.4) defines the Accept-Language header as followed:
  //   Accept-Language = "Accept-Language" ":"
  //                  1#( language-range [ ";" "q" "=" qvalue ] )
  //   language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
  // Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5"
  $browser_langs = array();
  if (preg_match_all('@([a-zA-Z-]+|\*)(?:;q=([0-9.]+))?(?:$|\s*,\s*)@', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches, PREG_SET_ORDER)) {
    foreach ($matches as $match) {
      // We can safely use strtolower() here, tags are ASCII.
      // RFC2616 mandates that the decimal part is no more than three digits,
      // so we multiply the qvalue by 1000 to avoid floating point comparisons.
      $langcode = strtolower($match[1]);
      $qvalue = isset($match[2]) ? (float) $match[2] : 1;
      $browser_langs[$langcode] = (int) ($qvalue * 1000);
    }
  }

  // Some browsers (especially some versions of Internet Explorer) sometimes
  // send a specific language tag (fr-CA) without the corresponding generic
  // tag (fr). In that case, we assume that the lowest value of the specific
  // tags is the value of the generic language.
  arsort($browser_langs);
  foreach ($browser_langs as $langcode => $qvalue) {
    $generic_tag = strtok($langcode, '-');
    if (!isset($browser_langs[$generic_tag])) {
      $browser_langs[$generic_tag] = $qvalue;
    }
  }

  // Find the enabled language with the greatest qvalue, following the rules
  // of RFC 2616 (section 14.4). If several languages have the same qvalue,
  // prefer the one with the greatest weight.
  $best_match = NULL;
  $max_qvalue = 0;
  foreach ($languages as $langcode => $language) {
    // Language tags are case insensitive (RFC2616, sec 3.10).
    $langcode = strtolower($langcode);

    // If nothing matches below, the default qvalue is the one of the wildcard
    // language, if set, or is 0 (which will never match).
    $qvalue = isset($browser_langs['*']) ? $browser_langs['*'] : 0;

    // Find the longest possible prefix of the browser-supplied language
    // ('the language-range') that matches this site language ('the language tag').
    $prefix = $langcode;
    do {
      if (isset($browser_langs[$prefix])) {
        $qvalue = $browser_langs[$prefix];
        break;
      }
    }
    while ($prefix = substr($prefix, 0, strrpos($prefix, '-')));

    // Find the best match.
    if ($qvalue > $max_qvalue) {
      $best_match = $language->language;
      $max_qvalue = $qvalue;
    }
  }
  
  return $languages[$best_match];
}
drutube’s picture

There's not feedback from anyone on this. I'm wondering if anyone has any experiences to share. We're going to try and implement so I'll let you know what happens.

cesarmiquel’s picture

Yes, this patched worked fine for me. I don't understand why this isn't ported to the latest D6 release. The issue is that IE sends a particular language code in HTTP_ACCEPT_LANGUAGE (i.e. 'en-US') but does not send the corresponding generic code (i.e. 'en'). This modification to includes/language.inc works for me in this cases.

cesarmiquel’s picture

Correction to my previous statement: this patch solves http://drupal.org/node/221712 but I can't tell you if it solves this particular issue with Chinese which seems to have particularities. In a nut shell: check 221712 and see if that is the issue you are having. Sorry for the confussion.