Translation for node meta data stored in database and shows in backend but I change language in front it shows default language (in my case English).

Comments

tiko created an issue. See original summary.

DamienMcKenna’s picture

Just to confirm the problem, are you expecting the translated content from your node to be output via tokens? How is the node content being translated?

tiko’s picture

I'm just switching with languages in node edit page and fill information directly in Meta tab. After that I see translated meta data in edit page but its not showing in front.
Content translation is organized with entity translation module.

tiko’s picture

I wrote the following code which solves the problem.

function MY_MODULE_html_head_alter(&$head_elements) {
  global $language;
  
  if (arg(0) == 'node') {
    if ($language->language != language_default('language')) {
      $metatag = metatag_metatags_load('node', arg(1));
      
      if (isset($head_elements['metatag_description_0']) && isset($metatag[$language->language]['description'])) {
        $head_elements['metatag_description_0']['#value'] = $metatag[$language->language]['description']['value'];
      }
      
      if (isset($head_elements['metatag_keywords_0']) && isset($metatag[$language->language]['keywords'])) {
        $head_elements['metatag_keywords_0']['#value'] = $metatag[$language->language]['keywords']['value'];
      }
    }
  }
}

It means that after changing the language metatag module can't determine it while adding tags.

DamienMcKenna’s picture

Can you please check the $langcode being used in metatag_entity_view() and let me know if it's the same as $language->language?

function metatag_entity_view($entity, $entity_type, $view_mode, $langcode, $force = FALSE) {
  globals $language;
  drupal_set_message("Entity langcode: {$langcode}");
  drupal_set_message("Page language: " . $language->language);
tiko’s picture

Metatag doesn't recognize languages, for all cases $langcode is en which is my default language.
hook_entity_view() invokes from system.api, it means that system passes wrong language code.

DamienMcKenna’s picture

Status: Active » Postponed (maintainer needs more info)

@tiko: If the system is passing the wrong language code then there's something wrong on the system, not in Metatag.

tiko’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

Thanks anyway.

DamienMcKenna’s picture

Status: Closed (works as designed) » Postponed (maintainer needs more info)

I'm happy to help work out the problem, if you can provide more details on the site? What modules you've installed that are related to languages or translation, or if you might have custom code that affects it?

tiko’s picture

The following modules related to languages: from i18n package - Internationalization, Menu translation, Field translation, String translation, Translation sets, Variable translation, for node localization - entity translation and title modules. There are no any hooks used concerning to languages.
For meta information I'm using metatag module and fill information(tab title, meta keyword and description) for all languages.

When I use

dpm($GLOBALS['language_content'])

in THEME_preprocess_page() I see english for all languages. In addition I would like to add that all other parts e.g. texts, titles, menus are working correctly. And none appropriate meta information is the consequence of wrong $langcode in metatag_entity_view().

DamienMcKenna’s picture

That's strange...

Do the nodes load correctly for each language?

tiko’s picture

Yeah all content is in accordance with language.

global $language;

dpm($language);

gives appropriate outcome as well but $language_content is always English.

I found temporary solution with following code:

function MY_MODULE_init() {
  global $language;

  $GLOBALS['language_content'] = $language;
}
tiko’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)