You are here

function metatag_translate_metatag in Metatag 7

Translates the metatag if i18n_string integration is enabled.

Parameters

string $string: String in default language or array of strings to be translated.

string $tag_name: The internal name of the meta tag being translated.

mixed $context: A context string to use with i18n, or the $options array from a Metatag:: getValue() method; if the latter it will be used to generate the full context.

string $langcode: The language code to submit instead of the current page's language.

bool $update: Whether or not to create/update records in {locales_source}.

Return value

string The translated string if i18n_string is enabled, otherwise just returns the original string.

See also

i18n_string_translate()

5 calls to metatag_translate_metatag()
DrupalDateIntervalMetaTag::getValue in ./metatag.inc
Get the string value of this meta tag.
DrupalDefaultMetaTag::getValue in ./metatag.inc
Get the string value of this meta tag.
DrupalListMetaTag::getValue in ./metatag.inc
Get the string value of this meta tag.
DrupalTextMetaTag::getValue in ./metatag.inc
Get the string value of this meta tag.
metatag_translate_metatags in ./metatag.module
Translate a set of metatags to the current language.

File

./metatag.module, line 3081
Primary hook implementations for Metatag.

Code

function metatag_translate_metatag($string, $tag_name, $context, $langcode = NULL, $update = TRUE) {
  if (module_exists('i18n_string') && !variable_get('metatag_i18n_disabled', FALSE)) {

    // By default do not add meta tags to admin pages. To enable meta tags on
    // admin pages set the 'metatag_tag_admin_pages' variable to TRUE.
    static $page_is_admin;
    if (is_null($page_is_admin)) {
      $page_is_admin = FALSE;
      if (path_is_admin(current_path()) && !variable_get('metatag_tag_admin_pages', FALSE)) {
        $page_is_admin = TRUE;
      }
    }
    if ($page_is_admin) {
      return $string;
    }

    // If the context is an array then it is the $options from the meta tag
    // generator and needs some custom tailoring. Doing it this way to avoid an
    // unnecessary entity_extract_ids() call when i18n isn't being used.
    if (is_array($context)) {

      // Optionally disable output generation.
      if (!variable_get('metatag_i18n_translate_output', FALSE)) {
        return $string;
      }

      // Output generation was enabled, so continue as normal.
      $new_context = 'output:';
      if (drupal_is_front_page()) {
        $new_context .= 'frontpage';
      }
      elseif (!empty($context['entity_type']) && !empty($context['entity'])) {
        list($entity_id, $revision_id, $bundle) = entity_extract_ids($context['entity_type'], $context['entity']);
        $new_context .= $context['entity_type'] . ':' . $entity_id;
      }
      else {

        // Trim this to avoid SQL errors on the {locales_source} table.
        // length = 255 - strlen('metatag:output:page:') - strlen(metatag);
        $strlen = 255 - strlen('metatag:output:page:' . $tag_name);
        $new_context .= 'page:' . drupal_substr(current_path(), 0, $strlen);
      }
      $context = $new_context;
    }
    $options = array(
      // Automatically create/update the {locales_source} record if one wasn't
      // found.
      'update' => $update,
      // Translate the string.
      'translate' => TRUE,
    );

    // If the langcode was passed in, add it to the options passed to i18n.
    if (!empty($langcode)) {
      $options['langcode'] = $langcode;
    }

    // By default disable the watchdog logging of translation messages.
    $options['watchdog'] = variable_get('metatag_i18n_enable_watchdog', FALSE);

    // Triggers hook_metatag_i18n_context_alter() - allows the i18n string to
    // be altered before being used.
    drupal_alter('metatag_i18n_context', $context, $tag_name);

    // If the context was erased just send back the original string - it's
    // unlikely, but it could happen.
    if (empty($context)) {
      return $string;
    }

    // The 'name' is split up by i18n_string into two components - the textgroup
    // is the first item, the others are joined together with a ':' separator
    // to make the context. In order to have the contexts show with "metatag" as
    // the first part of the context, it has to be added twice to the name.
    $name = array(
      'metatag',
      $context,
      $tag_name,
    );

    // Notify i18n of the string, and obtain a translation if one is available.
    return i18n_string($name, $string, $options);
  }
  else {
    return $string;
  }
}