You are here

class LocaleTranslation in Zircon Profile 8.0

Same name in this branch
  1. 8.0 core/modules/locale/src/LocaleTranslation.php \Drupal\locale\LocaleTranslation
  2. 8.0 core/modules/locale/src/Plugin/QueueWorker/LocaleTranslation.php \Drupal\locale\Plugin\QueueWorker\LocaleTranslation
Same name and namespace in other branches
  1. 8 core/modules/locale/src/LocaleTranslation.php \Drupal\locale\LocaleTranslation

String translator using the locale module.

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

Hierarchy

Expanded class hierarchy of LocaleTranslation

1 file declares its use of LocaleTranslation
LocaleTranslationTest.php in core/modules/locale/tests/src/Unit/LocaleTranslationTest.php
Contains \Drupal\Tests\locale\Unit\LocaleTranslationTest.
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 25
Contains \Drupal\locale\LocaleTranslation.

Namespace

Drupal\locale
View source
class LocaleTranslation implements TranslatorInterface, DestructableInterface {

  /**
   * 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 = array();

  /**
   * 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);
    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 = array();
  }

  /**
   * {@inheritdoc}
   */
  public function destruct() {
    foreach ($this->translations as $context) {
      foreach ($context as $lookup) {
        if ($lookup instanceof DestructableInterface) {
          $lookup
            ->destruct();
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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 Performs destruct operations. Overrides DestructableInterface::destruct
LocaleTranslation::getStringTranslation public function Retrieves English string to given language. Overrides TranslatorInterface::getStringTranslation
LocaleTranslation::reset public function Resets translation cache. Overrides TranslatorInterface::reset
LocaleTranslation::__construct public function Constructs a translator using a string storage.