You are here

public function LocaleConfigManager::translateString in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/locale/src/LocaleConfigManager.php \Drupal\locale\LocaleConfigManager::translateString()
  2. 10 core/modules/locale/src/LocaleConfigManager.php \Drupal\locale\LocaleConfigManager::translateString()

Translates string using the localization system.

So far we only know how to translate strings from English so the source string should be in English. Unlike regular t() translations, strings will be added to the source tables only if this is marked as default data.

Parameters

string $name: Name of the configuration location.

string $langcode: Language code to translate to.

string $source: The source string, should be English.

string $context: The string context.

Return value

string|false Translated string if there is a translation, FALSE if not.

2 calls to LocaleConfigManager::translateString()
LocaleConfigManager::getStringTranslation in core/modules/locale/src/LocaleConfigManager.php
Get the translation object for the given source/context and language.
LocaleConfigManager::processTranslatableData in core/modules/locale/src/LocaleConfigManager.php
Process the translatable data array with a given language.

File

core/modules/locale/src/LocaleConfigManager.php, line 366

Class

LocaleConfigManager
Manages configuration supported in part by interface translation.

Namespace

Drupal\locale

Code

public function translateString($name, $langcode, $source, $context) {
  if ($source) {

    // If translations for a language have not been loaded yet.
    if (!isset($this->translations[$name][$langcode])) {

      // Preload all translations for this configuration name and language.
      $this->translations[$name][$langcode] = [];
      foreach ($this->localeStorage
        ->getTranslations([
        'language' => $langcode,
        'type' => 'configuration',
        'name' => $name,
      ]) as $string) {
        $this->translations[$name][$langcode][$string->context][$string->source] = $string;
      }
    }
    if (!isset($this->translations[$name][$langcode][$context][$source])) {

      // There is no translation of the source string in this config location
      // to this language for this context.
      if ($translation = $this->localeStorage
        ->findTranslation([
        'source' => $source,
        'context' => $context,
        'language' => $langcode,
      ])) {

        // Look for a translation of the string. It might have one, but not
        // be saved in this configuration location yet.
        // If the string has a translation for this context to this language,
        // save it in the configuration location so it can be looked up faster
        // next time.
        $this->localeStorage
          ->createString((array) $translation)
          ->addLocation('configuration', $name)
          ->save();
      }
      else {

        // No translation was found. Add the source to the configuration
        // location so it can be translated, and the string is faster to look
        // for next time.
        $translation = $this->localeStorage
          ->createString([
          'source' => $source,
          'context' => $context,
        ])
          ->addLocation('configuration', $name)
          ->save();
      }

      // Add an entry, either the translation found, or a blank string object
      // to track the source string, to this configuration location, language,
      // and context.
      $this->translations[$name][$langcode][$context][$source] = $translation;
    }

    // Return the string only when the string object had a translation.
    if ($this->translations[$name][$langcode][$context][$source]
      ->isTranslation()) {
      return $this->translations[$name][$langcode][$context][$source]
        ->getString();
    }
  }
  return FALSE;
}