class TranslationManager
Same name in this branch
- main core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
- 10 core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
- 9 core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
- 8.9.x core/lib/Drupal/Core/StringTranslation/TranslationManager.php \Drupal\Core\StringTranslation\TranslationManager
- 11.x core/modules/locale/src/TranslationManager.php \Drupal\locale\TranslationManager
Plural index implementation.
Hierarchy
- class \Drupal\Core\StringTranslation\TranslationManager implements \Drupal\Core\StringTranslation\TranslationInterface, \Drupal\Core\StringTranslation\Translator\TranslatorInterface
- class \Drupal\locale\TranslationManager uses \Drupal\Core\DependencyInjection\DependencySerializationTrait extends \Drupal\Core\StringTranslation\TranslationManager
Expanded class hierarchy of TranslationManager
File
-
core/
modules/ locale/ src/ TranslationManager.php, line 14
Namespace
Drupal\localeView source
class TranslationManager extends CoreTranslationManager {
use DependencySerializationTrait;
public function __construct(LanguageDefault $default_language, protected PluralFormulaInterface $pluralFormula) {
parent::__construct($default_language);
}
/**
* Returns plural form index for a specific number.
*
* The index is computed from the formula of this language.
*
* @param int $count
* Number to return plural for.
* @param string|null $langcode
* (optional) Language code to translate to a language other than what is
* used to display the page.
*
* @return int
* The numeric index of the plural variant to use for this $langcode and
* $count combination or -1 if the language was not found or does not have a
* plural formula.
*/
protected function getPluralIndex(int $count, ?string $langcode = NULL) : int {
$langcode = $langcode ?: $this->defaultLangcode;
// Retrieve the plural formulas for all languages.
$plural_formulas = $this->pluralFormula
->getFormula($langcode);
// If there is a plural formula for the language, evaluate it for the
// given $count.
if (!empty($plural_formulas)) {
// Plural formulas are stored as an array for 0-199. 100 is the highest
// modulo used but storing 0-99 is not enough because below 100 we often
// find exceptions (1, 2, etc).
$index = $count > 199 ? 100 + $count % 100 : $count;
return $plural_formulas[$index] ?? $plural_formulas['default'];
}
elseif ($langcode == 'en') {
return (int) ($count != 1);
}
else {
return -1;
}
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
|---|---|---|---|---|---|
| DependencySerializationTrait::$_entityStorages | protected | property | An array of entity type IDs keyed by the property name of their storages. | ||
| DependencySerializationTrait::$_serviceIds | protected | property | An array of service IDs keyed by property name used for serialization. | ||
| DependencySerializationTrait::__sleep | public | function | 2 | ||
| DependencySerializationTrait::__wakeup | public | function | 2 | ||
| TranslationManager::$defaultLangcode | protected | property | The default langcode used in translations. | ||
| TranslationManager::$sortedTranslators | protected | property | An array of translators, sorted by priority. | ||
| TranslationManager::$translators | protected | property | An unsorted array of arrays of active translators. | ||
| TranslationManager::addTranslator | public | function | Appends a translation system to the translation chain. | ||
| TranslationManager::doTranslate | protected | function | Translates a string to the current language or to a given language. | ||
| TranslationManager::formatPlural | public | function | Formats a string containing a count of items. | Overrides TranslationInterface::formatPlural | |
| TranslationManager::getPluralIndex | protected | function | Returns plural form index for a specific number. | Overrides TranslationManager::getPluralIndex | |
| TranslationManager::getStringTranslation | public | function | Retrieves English string to given language. | Overrides TranslatorInterface::getStringTranslation | |
| TranslationManager::reset | public | function | Resets translation cache. | Overrides TranslatorInterface::reset | |
| TranslationManager::selectPluralForm | public | function | Selects the plural form for a given count. | Overrides TranslationInterface::selectPluralForm | |
| TranslationManager::setDefaultLangcode | public | function | Sets the default langcode. | ||
| TranslationManager::sortTranslators | protected | function | Sorts translators according to priority. | ||
| TranslationManager::translate | public | function | Translates a string to the current language or to a given language. | Overrides TranslationInterface::translate | |
| TranslationManager::translateString | public | function | Translates a TranslatableMarkup object to a string. | Overrides TranslationInterface::translateString | |
| TranslationManager::__construct | public | function | Overrides TranslationManager::__construct |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.