class LocaleTranslation in Drupal 10
Same name in this branch
- 10 core/modules/locale/src/LocaleTranslation.php \Drupal\locale\LocaleTranslation
- 10 core/modules/locale/src/Plugin/QueueWorker/LocaleTranslation.php \Drupal\locale\Plugin\QueueWorker\LocaleTranslation
Same name and namespace in other branches
- 8 core/modules/locale/src/LocaleTranslation.php \Drupal\locale\LocaleTranslation
- 9 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
- class \Drupal\locale\LocaleTranslation implements DestructableInterface, TranslatorInterface uses DependencySerializationTrait
Expanded class hierarchy of LocaleTranslation
1 file declares its use of LocaleTranslation
- 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\localeView 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);
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
LocaleTranslation:: |
protected | property | The cache backend that should be used. | |
LocaleTranslation:: |
protected | property | The configuration factory. | |
LocaleTranslation:: |
protected | property | The language manager. | |
LocaleTranslation:: |
protected | property | The lock backend that should be used. | |
LocaleTranslation:: |
protected | property | The request stack. | |
LocaleTranslation:: |
protected | property | Storage for strings. | |
LocaleTranslation:: |
protected | property | The translate english configuration value. | |
LocaleTranslation:: |
protected | property | Cached translations. | |
LocaleTranslation:: |
protected | function | Gets translate english configuration value. | |
LocaleTranslation:: |
public | function |
Performs destruct operations. Overrides DestructableInterface:: |
|
LocaleTranslation:: |
public | function |
Retrieves English string to given language. Overrides TranslatorInterface:: |
|
LocaleTranslation:: |
public | function |
Resets translation cache. Overrides TranslatorInterface:: |
|
LocaleTranslation:: |
public | function | Constructs a translator using a string storage. |