Search code examples
drupal-7multilingual

Drupal 7 multilingual site: each node URL is duplicated in all languages


I have a Drupal 7 multilingula site with 3 languages: english, arabic, and chinese.

It all work well, but I've just noticed that pages that are not translated have a duplicated URL for each language.

For example say I have www.example.com/node/12 (default language = English). This node doe snot have any translations.

However www.example.com/ar/node/12 and www.example.com/zh/node/12 both exist and point to the English page. They are even being index in Google.

How can I make these URL show a "Page not found" (which should be the expected behavior)?


Solution

  • Well, in the end I solved it with some custom code in hook_init:

    $lang_name = $language->language ;
    if ($lang_name == "ar" || $lang_name == "zh-hans") {
        $has_translation = false;
        if ($is_node) {
          $translation_array = translation_node_get_translations($node->tnid);
          $has_translation = isset($translation_array[$lang_name]);
        }
        if (!$has_translation) {
          $path = drupal_get_path_alias(current_path());
          $installed_languages = language_list();
          $en = $installed_languages["en"];
          drupal_goto($path, array('language' => $en), 301);
        }
      }
    

    So, basically, on each page I check if we're on a language other than English (which means we're in a path starting with /ar or /zh). If that's the case and if we're on a node I check if there's an existing translation for that node and that language. If so we do nothing, but if not we redirect to the default path (without the language prefix).