You are here

function i18n_string_translate in Internationalization 7

Get translation for user defined string.

This function is intended to return translations for plain strings that have NO text format

Parameters

array|string name: Array or string concatenated with ':' that contains textgroup and string context

array|string $string: A string in the default language, a string wth format (array with keys value and format),or an array of strings (without format) to be translated.

array $options: An associative array of additional options, with the following keys:

  • 'langcode' (defaults to the current language) The language code to translate to a language other than what is used to display the page.
  • 'filter' Filtering callback to apply to the translated string only
  • 'format' Input format to apply to the translated string only
  • 'callback' Callback to apply to the result (both to translated or untranslated string
  • 'sanitize' Whether to filter the translation applying the text format if any, default is TRUE
  • 'sanitize default' Whether to filter the default value if no translation found, default is FALSE

Return value

string

12 calls to i18n_string_translate()
i18nStringTestCase::testStringsAPI in i18n_string/i18n_string.test
Test base i18n_string API
i18n_contact_form_contact_site_form_alter in i18n_contact/i18n_contact.module
Implements hook_form_FORM_ID_alter().
i18n_field_translate_allowed_values in i18n_field/i18n_field.module
Returns the array of translated allowed values for a list field.
i18n_field_translate_default in i18n_field/i18n_field.module
Translate field default.
i18n_node_tokens in i18n_node/i18n_node.tokens.inc
Implements hook_tokens().

... See full list

1 string reference to 'i18n_string_translate'
i18n_string in ./i18n.module
Translate or update user defined string. Entry point for i18n_string API if enabled.

File

i18n_string/i18n_string.module, line 592
Internationalization (i18n) package - translatable strings.

Code

function i18n_string_translate($name, $string, $options = array()) {
  if (is_array($string) && isset($string['value'])) {
    $string = $string['value'];
  }
  if (is_array($string)) {
    return i18n_string_translate_list($name, $string, $options);
  }
  else {
    $options['langcode'] = $langcode = isset($options['langcode']) ? $options['langcode'] : i18n_langcode();
    if (i18n_string_translate_langcode($langcode)) {
      list($textgroup, $context) = i18n_string_context($name);
      $translation = i18n_string_textgroup($textgroup)
        ->context_translate($context, $string, $options);

      // Add for l10n client if available, we pass translation object that contains the format
      i18n_string_l10n_client_add($translation, $langcode);
      return $translation
        ->format_translation($langcode, $options);
    }
    else {

      // If we don't want to translate to this language, format and return
      $options['sanitize'] = !empty($options['sanitize default']);
      return i18n_string_format($string, $options);
    }
  }
}