You are here

public function LocaleConfigManager::getStringTranslation in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/locale/src/LocaleConfigManager.php \Drupal\locale\LocaleConfigManager::getStringTranslation()

Get the translation object for the given source/context and language.

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

\Drupal\locale\TranslationString|false The translation object if the string was not empty or FALSE otherwise.

File

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

Class

LocaleConfigManager
Manages configuration supported in part by interface translation.

Namespace

Drupal\locale

Code

public function getStringTranslation($name, $langcode, $source, $context) {
  if ($source) {
    $this
      ->translateString($name, $langcode, $source, $context);
    if ($string = $this->translations[$name][$langcode][$context][$source]) {
      if (!$string
        ->isTranslation()) {
        $conditions = [
          'lid' => $string->lid,
          'language' => $langcode,
        ];
        $translation = $this->localeStorage
          ->createTranslation($conditions);
        $this->translations[$name][$langcode][$context][$source] = $translation;
        return $translation;
      }
      else {
        return $string;
      }
    }
  }
  return FALSE;
}