class LocaleTranslation

Same name in this branch
  1. 10 core/modules/locale/src/Plugin/QueueWorker/LocaleTranslation.php \Drupal\locale\Plugin\QueueWorker\LocaleTranslation
Same name in other branches
  1. 9 core/modules/locale/src/LocaleTranslation.php \Drupal\locale\LocaleTranslation
  2. 9 core/modules/locale/src/Plugin/QueueWorker/LocaleTranslation.php \Drupal\locale\Plugin\QueueWorker\LocaleTranslation
  3. 8.9.x core/modules/locale/src/LocaleTranslation.php \Drupal\locale\LocaleTranslation
  4. 8.9.x core/modules/locale/src/Plugin/QueueWorker/LocaleTranslation.php \Drupal\locale\Plugin\QueueWorker\LocaleTranslation
  5. 11.x core/modules/locale/src/LocaleTranslation.php \Drupal\locale\LocaleTranslation
  6. 11.x core/modules/locale/src/Plugin/QueueWorker/LocaleTranslation.php \Drupal\locale\Plugin\QueueWorker\LocaleTranslation

String translator using the locale module.

Full featured translation system using locale's string storage and database caching.

Hierarchy

  • class \Drupal\locale\LocaleTranslation implements \Drupal\Core\StringTranslation\Translator\TranslatorInterface, \Drupal\Core\DestructableInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait

Expanded class hierarchy of LocaleTranslation

2 files declare their use of LocaleTranslation
LocaleTranslationTest.php in core/modules/locale/tests/src/Unit/LocaleTranslationTest.php
LocaleTranslationTest.php in core/modules/locale/tests/src/Kernel/LocaleTranslationTest.php
1 string reference to 'LocaleTranslation'
locale.services.yml in core/modules/locale/locale.services.yml
core/modules/locale/locale.services.yml
1 service uses LocaleTranslation
string_translator.locale.lookup in core/modules/locale/locale.services.yml
Drupal\locale\LocaleTranslation

File

core/modules/locale/src/LocaleTranslation.php, line 21

Namespace

Drupal\locale
View source
class LocaleTranslation implements TranslatorInterface, DestructableInterface {
    use DependencySerializationTrait;
    
    /**
     * Storage for strings.
     *
     * @var \Drupal\locale\StringStorageInterface
     */
    protected $storage;
    
    /**
     * The configuration factory.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * Cached translations.
     *
     * @var array
     *   Array of \Drupal\locale\LocaleLookup objects indexed by language code
     *   and context.
     */
    protected $translations = [];
    
    /**
     * The cache backend that should be used.
     *
     * @var \Drupal\Core\Cache\CacheBackendInterface
     */
    protected $cache;
    
    /**
     * The lock backend that should be used.
     *
     * @var \Drupal\Core\Lock\LockBackendInterface
     */
    protected $lock;
    
    /**
     * The translate english configuration value.
     *
     * @var bool
     */
    protected $translateEnglish;
    
    /**
     * The language manager.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface
     */
    protected $languageManager;
    
    /**
     * The request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    protected $requestStack;
    
    /**
     * Constructs a translator using a string storage.
     *
     * @param \Drupal\locale\StringStorageInterface $storage
     *   Storage to use when looking for new translations.
     * @param \Drupal\Core\Cache\CacheBackendInterface $cache
     *   The cache backend.
     * @param \Drupal\Core\Lock\LockBackendInterface $lock
     *   The lock backend.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The config factory.
     * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
     *   The language manager.
     * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
     *   The request stack.
     */
    public function __construct(StringStorageInterface $storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
        $this->storage = $storage;
        $this->cache = $cache;
        $this->lock = $lock;
        $this->configFactory = $config_factory;
        $this->languageManager = $language_manager;
        $this->requestStack = $request_stack;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getStringTranslation($langcode, $string, $context) {
        // If the language is not suitable for locale module, just return.
        if ($langcode == LanguageInterface::LANGCODE_SYSTEM || $langcode == 'en' && !$this->canTranslateEnglish()) {
            return FALSE;
        }
        // Strings are cached by langcode, context and roles, using instances of the
        // LocaleLookup class to handle string lookup and caching.
        if (!isset($this->translations[$langcode][$context])) {
            $this->translations[$langcode][$context] = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
        }
        $translation = $this->translations[$langcode][$context]
            ->get($string);
        // If the translation is TRUE, no translation exists, but that string needs
        // to be stored in the persistent cache for performance reasons (so for
        // example, we don't have hundreds of queries to locale tables on each
        // request). That cache is persisted when the request ends, and the lookup
        // service is destroyed.
        return $translation === TRUE ? FALSE : $translation;
    }
    
    /**
     * Gets translate english configuration value.
     *
     * @return bool
     *   TRUE if english should be translated, FALSE if not.
     */
    protected function canTranslateEnglish() {
        if (!isset($this->translateEnglish)) {
            $this->translateEnglish = $this->configFactory
                ->get('locale.settings')
                ->get('translate_english');
        }
        return $this->translateEnglish;
    }
    
    /**
     * {@inheritdoc}
     */
    public function reset() {
        unset($this->translateEnglish);
        $this->translations = [];
    }
    
    /**
     * {@inheritdoc}
     */
    public function destruct() {
        foreach ($this->translations as $context) {
            foreach ($context as $lookup) {
                if ($lookup instanceof DestructableInterface) {
                    $lookup->destruct();
                }
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
LocaleTranslation::$cache protected property The cache backend that should be used.
LocaleTranslation::$configFactory protected property The configuration factory.
LocaleTranslation::$languageManager protected property The language manager.
LocaleTranslation::$lock protected property The lock backend that should be used.
LocaleTranslation::$requestStack protected property The request stack.
LocaleTranslation::$storage protected property Storage for strings.
LocaleTranslation::$translateEnglish protected property The translate english configuration value.
LocaleTranslation::$translations protected property Cached translations.
LocaleTranslation::canTranslateEnglish protected function Gets translate english configuration value.
LocaleTranslation::destruct public function Overrides DestructableInterface::destruct
LocaleTranslation::getStringTranslation public function Overrides TranslatorInterface::getStringTranslation
LocaleTranslation::reset public function Overrides TranslatorInterface::reset
LocaleTranslation::__construct public function Constructs a translator using a string storage.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.