Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
171
core/lib/Drupal/Core/Language/Language.php
Normal file
171
core/lib/Drupal/Core/Language/Language.php
Normal file
|
@ -0,0 +1,171 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Language\Language.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Language;
|
||||
|
||||
/**
|
||||
* An object containing the information for an interface language.
|
||||
*
|
||||
* @see \Drupal\Core\Language\LanguageManager::getLanguage()
|
||||
*/
|
||||
class Language implements LanguageInterface {
|
||||
|
||||
/**
|
||||
* The values to use to instantiate the default language.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $defaultValues = array(
|
||||
'id' => 'en',
|
||||
'name' => 'English',
|
||||
'direction' => self::DIRECTION_LTR,
|
||||
'weight' => 0,
|
||||
'locked' => FALSE,
|
||||
);
|
||||
|
||||
// Properties within the Language are set up as the default language.
|
||||
|
||||
/**
|
||||
* The human readable English name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = '';
|
||||
|
||||
/**
|
||||
* The ID, langcode.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $id = '';
|
||||
|
||||
/**
|
||||
* The direction, left-to-right, or right-to-left.
|
||||
*
|
||||
* Defined using constants, either self::DIRECTION_LTR or self::DIRECTION_RTL.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $direction = self::DIRECTION_LTR;
|
||||
|
||||
/**
|
||||
* The weight, used for ordering languages in lists, like selects or tables.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $weight = 0;
|
||||
|
||||
/**
|
||||
* Locked indicates a language used by the system, not an actual language.
|
||||
*
|
||||
* Examples of locked languages are, LANGCODE_NOT_SPECIFIED, und, and
|
||||
* LANGCODE_NOT_APPLICABLE, zxx, which are usually shown in language selects
|
||||
* but hidden in places like the Language configuration and cannot be deleted.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $locked = FALSE;
|
||||
|
||||
/**
|
||||
* Constructs a new class instance.
|
||||
*
|
||||
* @param array $values
|
||||
* An array of property values, keyed by property name, used to construct
|
||||
* the language.
|
||||
*/
|
||||
public function __construct(array $values = array()) {
|
||||
// Set all the provided properties for the language.
|
||||
foreach ($values as $key => $value) {
|
||||
if (property_exists($this, $key)) {
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
// If some values were not set, set sane defaults of a predefined language.
|
||||
if (!isset($values['name']) || !isset($values['direction'])) {
|
||||
$predefined = LanguageManager::getStandardLanguageList();
|
||||
if (isset($predefined[$this->id])) {
|
||||
if (!isset($values['name'])) {
|
||||
$this->name = $predefined[$this->id][0];
|
||||
}
|
||||
if (!isset($values['direction']) && isset($predefined[$this->id][2])) {
|
||||
$this->direction = $predefined[$this->id][2];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDirection() {
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getWeight() {
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isDefault() {
|
||||
return static::getDefaultLangcode() == $this->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isLocked() {
|
||||
return (bool) $this->locked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort language objects.
|
||||
*
|
||||
* @param \Drupal\Core\Language\LanguageInterface[] $languages
|
||||
* The array of language objects keyed by langcode.
|
||||
*/
|
||||
public static function sort(&$languages) {
|
||||
uasort($languages, function (LanguageInterface $a, LanguageInterface $b) {
|
||||
$a_weight = $a->getWeight();
|
||||
$b_weight = $b->getWeight();
|
||||
if ($a_weight == $b_weight) {
|
||||
return strnatcasecmp($a->getName(), $b->getName());
|
||||
}
|
||||
return ($a_weight < $b_weight) ? -1 : 1;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default langcode.
|
||||
*
|
||||
* @return string
|
||||
* The current default langcode.
|
||||
*/
|
||||
protected static function getDefaultLangcode() {
|
||||
$language = \Drupal::service('language.default')->get();
|
||||
return $language->getId();
|
||||
}
|
||||
|
||||
}
|
59
core/lib/Drupal/Core/Language/LanguageDefault.php
Normal file
59
core/lib/Drupal/Core/Language/LanguageDefault.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Language\LanguageDefault.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Language;
|
||||
|
||||
/**
|
||||
* Provides a simple get and set wrapper to the default language object.
|
||||
*
|
||||
* The default language must be provided without dependencies since it is both
|
||||
* configured and a dependency of the configuration system. The LanguageDefault
|
||||
* object is a container service. The default values are stored on the container
|
||||
* by \Drupal\Core\DrupalKernel::buildContainer(). This allows services to
|
||||
* override this parameter in a ServiceProvider, for example,
|
||||
* \Drupal\language\LanguageServiceProvider::alter().
|
||||
*/
|
||||
class LanguageDefault {
|
||||
|
||||
/**
|
||||
* The default language.
|
||||
*
|
||||
* @var \Drupal\Core\Language\LanguageInterface
|
||||
*/
|
||||
protected $language;
|
||||
|
||||
/**
|
||||
* Constructs the default language object.
|
||||
*
|
||||
* @param array $values
|
||||
* The properties used to construct the default language.
|
||||
*/
|
||||
public function __construct(array $values) {
|
||||
$this->set(new Language($values));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default language.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageInterface
|
||||
* The default language.
|
||||
*/
|
||||
public function get() {
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default language.
|
||||
*
|
||||
* @param \Drupal\Core\Language\LanguageInterface $language
|
||||
* The default language.
|
||||
*/
|
||||
public function set(LanguageInterface $language) {
|
||||
$this->language = $language;
|
||||
}
|
||||
|
||||
}
|
154
core/lib/Drupal/Core/Language/LanguageInterface.php
Normal file
154
core/lib/Drupal/Core/Language/LanguageInterface.php
Normal file
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Language\LanguageInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Language;
|
||||
|
||||
/**
|
||||
* Defines a language.
|
||||
*/
|
||||
interface LanguageInterface {
|
||||
|
||||
/**
|
||||
* Special system language code (only applicable to UI language).
|
||||
*
|
||||
* Refers to the language used in Drupal and module/theme source code. Drupal
|
||||
* uses the built-in text for English by default, but if configured to allow
|
||||
* translation/customization of English, we need to differentiate between the
|
||||
* built-in language and the English translation.
|
||||
*/
|
||||
const LANGCODE_SYSTEM = 'system';
|
||||
|
||||
/**
|
||||
* The language code used when no language is explicitly assigned (yet).
|
||||
*
|
||||
* Should be used when language information is not available or cannot be
|
||||
* determined. This special language code is useful when we know the data
|
||||
* might have linguistic information, but we don't know the language.
|
||||
*
|
||||
* See http://www.w3.org/International/questions/qa-no-language#undetermined.
|
||||
*/
|
||||
const LANGCODE_NOT_SPECIFIED = 'und';
|
||||
|
||||
/**
|
||||
* The language code used when the marked object has no linguistic content.
|
||||
*
|
||||
* Should be used when we explicitly know that the data referred has no
|
||||
* linguistic content.
|
||||
*
|
||||
* See http://www.w3.org/International/questions/qa-no-language#nonlinguistic.
|
||||
*/
|
||||
const LANGCODE_NOT_APPLICABLE = 'zxx';
|
||||
|
||||
/**
|
||||
* Language code referring to the default language of data, e.g. of an entity.
|
||||
*
|
||||
* See the BCP 47 syntax for defining private language tags:
|
||||
* http://www.rfc-editor.org/rfc/bcp/bcp47.txt
|
||||
*/
|
||||
const LANGCODE_DEFAULT = 'x-default';
|
||||
|
||||
/**
|
||||
* Language code referring to site's default language.
|
||||
*/
|
||||
const LANGCODE_SITE_DEFAULT = 'site_default';
|
||||
|
||||
/**
|
||||
* The language state when referring to configurable languages.
|
||||
*/
|
||||
const STATE_CONFIGURABLE = 1;
|
||||
|
||||
/**
|
||||
* The language state when referring to locked languages.
|
||||
*/
|
||||
const STATE_LOCKED = 2;
|
||||
|
||||
/**
|
||||
* The language state used when referring to all languages.
|
||||
*/
|
||||
const STATE_ALL = 3;
|
||||
|
||||
/**
|
||||
* The language state used when referring to the site's default language.
|
||||
*/
|
||||
const STATE_SITE_DEFAULT = 4;
|
||||
|
||||
/**
|
||||
* The type of language used to define the content language.
|
||||
*/
|
||||
const TYPE_CONTENT = 'language_content';
|
||||
|
||||
/**
|
||||
* The type of language used to select the user interface.
|
||||
*/
|
||||
const TYPE_INTERFACE = 'language_interface';
|
||||
|
||||
/**
|
||||
* The type of language used for URLs.
|
||||
*/
|
||||
const TYPE_URL = 'language_url';
|
||||
|
||||
/**
|
||||
* Language written left to right. Possible value of $language->direction.
|
||||
*/
|
||||
const DIRECTION_LTR = 'ltr';
|
||||
|
||||
/**
|
||||
* Language written right to left. Possible value of $language->direction.
|
||||
*/
|
||||
const DIRECTION_RTL = 'rtl';
|
||||
|
||||
/**
|
||||
* Gets the name of the language.
|
||||
*
|
||||
* @return string
|
||||
* The human-readable name of the language (in the language that was
|
||||
* used to construct this object).
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Gets the ID (language code).
|
||||
*
|
||||
* @return string
|
||||
* The language code.
|
||||
*/
|
||||
public function getId();
|
||||
|
||||
/**
|
||||
* Gets the text direction (left-to-right or right-to-left).
|
||||
*
|
||||
* @return string
|
||||
* Either self::DIRECTION_LTR or self::DIRECTION_RTL.
|
||||
*/
|
||||
public function getDirection();
|
||||
|
||||
/**
|
||||
* Gets the weight of the language.
|
||||
*
|
||||
* @return int
|
||||
* The weight, used to order languages with larger positive weights sinking
|
||||
* items toward the bottom of lists.
|
||||
*/
|
||||
public function getWeight();
|
||||
|
||||
/**
|
||||
* Returns whether this language is the default language.
|
||||
*
|
||||
* @return bool
|
||||
* Whether the language is the default language.
|
||||
*/
|
||||
public function isDefault();
|
||||
|
||||
/**
|
||||
* Returns whether this language is locked.
|
||||
*
|
||||
* @return bool
|
||||
* Whether the language is locked or not.
|
||||
*/
|
||||
public function isLocked();
|
||||
|
||||
}
|
414
core/lib/Drupal/Core/Language/LanguageManager.php
Normal file
414
core/lib/Drupal/Core/Language/LanguageManager.php
Normal file
|
@ -0,0 +1,414 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Language\LanguageManager.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Language;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
|
||||
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||
use Drupal\Core\StringTranslation\TranslationWrapper;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Class responsible for providing language support on language-unaware sites.
|
||||
*/
|
||||
class LanguageManager implements LanguageManagerInterface {
|
||||
use DependencySerializationTrait;
|
||||
|
||||
/**
|
||||
* The string translation service.
|
||||
*
|
||||
* @var \Drupal\Core\StringTranslation\TranslationInterface
|
||||
*/
|
||||
protected $translation;
|
||||
|
||||
/**
|
||||
* A static cache of translated language lists.
|
||||
*
|
||||
* Array of arrays to cache the result of self::getLanguages() keyed by the
|
||||
* language the list is translated to (first level) and the flags provided to
|
||||
* the method (second level).
|
||||
*
|
||||
* @var \Drupal\Core\Language\LanguageInterface[]
|
||||
*
|
||||
* @see \Drupal\Core\Language\LanguageManager::getLanguages()
|
||||
*/
|
||||
protected $languages = array();
|
||||
|
||||
/**
|
||||
* The default language object.
|
||||
*
|
||||
* @var \Drupal\Core\Language\LanguageDefault
|
||||
*/
|
||||
protected $defaultLanguage;
|
||||
|
||||
/**
|
||||
* Constructs the language manager.
|
||||
*
|
||||
* @param \Drupal\Core\Language\LanguageDefault $default_language
|
||||
* The default language.
|
||||
*/
|
||||
public function __construct(LanguageDefault $default_language) {
|
||||
$this->defaultLanguage = $default_language;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setTranslation(TranslationInterface $translation) {
|
||||
$this->translation = $translation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates a string to the current language or to a given language.
|
||||
*
|
||||
* @see \Drupal\Core\StringTranslation\TranslationInterface()
|
||||
*/
|
||||
protected function t($string, array $args = array(), array $options = array()) {
|
||||
return $this->translation ? $this->translation->translate($string, $args, $options) : SafeMarkup::format($string, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isMultilingual() {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLanguageTypes() {
|
||||
return array(LanguageInterface::TYPE_INTERFACE, LanguageInterface::TYPE_CONTENT, LanguageInterface::TYPE_URL);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefinedLanguageTypesInfo() {
|
||||
// This needs to have the same return value as
|
||||
// language_language_type_info(), so that even if the Language module is
|
||||
// not defined, users of this information, such as the Views module, can
|
||||
// access names and descriptions of the default language types.
|
||||
return array(
|
||||
LanguageInterface::TYPE_INTERFACE => array(
|
||||
'name' => $this->t('Interface text'),
|
||||
'description' => $this->t('Order of language detection methods for interface text. If a translation of interface text is available in the detected language, it will be displayed.'),
|
||||
'locked' => TRUE,
|
||||
),
|
||||
LanguageInterface::TYPE_CONTENT => array(
|
||||
'name' => $this->t('Content'),
|
||||
'description' => $this->t('Order of language detection methods for content. If a version of content is available in the detected language, it will be displayed.'),
|
||||
'locked' => TRUE,
|
||||
),
|
||||
LanguageInterface::TYPE_URL => array(
|
||||
'locked' => TRUE,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCurrentLanguage($type = LanguageInterface::TYPE_INTERFACE) {
|
||||
return $this->getDefaultLanguage();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function reset($type = NULL) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultLanguage() {
|
||||
return $this->defaultLanguage->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE) {
|
||||
$static_cache_id = $this->getCurrentLanguage()->getId();
|
||||
if (!isset($this->languages[$static_cache_id][$flags])) {
|
||||
// If this language manager is used, there are no configured languages.
|
||||
// The default language and locked languages comprise the full language
|
||||
// list.
|
||||
$default = $this->getDefaultLanguage();
|
||||
$languages = array($default->getId() => $default);
|
||||
$languages += $this->getDefaultLockedLanguages($default->getWeight());
|
||||
|
||||
// Filter the full list of languages based on the value of $flags.
|
||||
$this->languages[$static_cache_id][$flags] = $this->filterLanguages($languages, $flags);
|
||||
}
|
||||
return $this->languages[$static_cache_id][$flags];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getNativeLanguages() {
|
||||
// In a language unaware site we don't have translated languages.
|
||||
return $this->getLanguages();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLanguage($langcode) {
|
||||
$languages = $this->getLanguages(LanguageInterface::STATE_ALL);
|
||||
return isset($languages[$langcode]) ? $languages[$langcode] : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLanguageName($langcode) {
|
||||
if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
|
||||
return $this->t('None');
|
||||
}
|
||||
if ($language = $this->getLanguage($langcode)) {
|
||||
return $language->getName();
|
||||
}
|
||||
if (empty($langcode)) {
|
||||
return $this->t('Unknown');
|
||||
}
|
||||
return $this->t('Unknown (@langcode)', array('@langcode' => $langcode));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultLockedLanguages($weight = 0) {
|
||||
$languages = array();
|
||||
|
||||
$locked_language = array(
|
||||
'default' => FALSE,
|
||||
'locked' => TRUE,
|
||||
);
|
||||
// This is called very early while initializing the language system. Prevent
|
||||
// early t() calls by using the TranslationWrapper.
|
||||
$languages[LanguageInterface::LANGCODE_NOT_SPECIFIED] = new Language(array(
|
||||
'id' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
||||
'name' => new TranslationWrapper('Not specified'),
|
||||
'weight' => ++$weight,
|
||||
) + $locked_language);
|
||||
|
||||
$languages[LanguageInterface::LANGCODE_NOT_APPLICABLE] = new Language(array(
|
||||
'id' => LanguageInterface::LANGCODE_NOT_APPLICABLE,
|
||||
'name' => new TranslationWrapper('Not applicable'),
|
||||
'weight' => ++$weight,
|
||||
) + $locked_language);
|
||||
|
||||
return $languages;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isLanguageLocked($langcode) {
|
||||
$language = $this->getLanguage($langcode);
|
||||
return ($language ? $language->isLocked() : FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFallbackCandidates(array $context = array()) {
|
||||
return array(LanguageInterface::LANGCODE_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLanguageSwitchLinks($type, Url $url) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function getStandardLanguageList() {
|
||||
// This list is based on languages available from localize.drupal.org. See
|
||||
// http://localize.drupal.org/issues for information on how to add languages
|
||||
// there.
|
||||
//
|
||||
// The "Left-to-right marker" comments and the enclosed UTF-8 markers are to
|
||||
// make otherwise strange looking PHP syntax natural (to not be displayed in
|
||||
// right to left). See https://www.drupal.org/node/128866#comment-528929.
|
||||
return array(
|
||||
'af' => array('Afrikaans', 'Afrikaans'),
|
||||
'am' => array('Amharic', 'አማርኛ'),
|
||||
'ar' => array('Arabic', /* Left-to-right marker "" */ 'العربية', LanguageInterface::DIRECTION_RTL),
|
||||
'ast' => array('Asturian', 'Asturianu'),
|
||||
'az' => array('Azerbaijani', 'Azərbaycanca'),
|
||||
'be' => array('Belarusian', 'Беларуская'),
|
||||
'bg' => array('Bulgarian', 'Български'),
|
||||
'bn' => array('Bengali', 'বাংলা'),
|
||||
'bo' => array('Tibetan', 'བོད་སྐད་'),
|
||||
'bs' => array('Bosnian', 'Bosanski'),
|
||||
'ca' => array('Catalan', 'Català'),
|
||||
'cs' => array('Czech', 'Čeština'),
|
||||
'cy' => array('Welsh', 'Cymraeg'),
|
||||
'da' => array('Danish', 'Dansk'),
|
||||
'de' => array('German', 'Deutsch'),
|
||||
'dz' => array('Dzongkha', 'རྫོང་ཁ'),
|
||||
'el' => array('Greek', 'Ελληνικά'),
|
||||
'en' => array('English', 'English'),
|
||||
'eo' => array('Esperanto', 'Esperanto'),
|
||||
'es' => array('Spanish', 'Español'),
|
||||
'et' => array('Estonian', 'Eesti'),
|
||||
'eu' => array('Basque', 'Euskera'),
|
||||
'fa' => array('Persian, Farsi', /* Left-to-right marker "" */ 'فارسی', LanguageInterface::DIRECTION_RTL),
|
||||
'fi' => array('Finnish', 'Suomi'),
|
||||
'fil' => array('Filipino', 'Filipino'),
|
||||
'fo' => array('Faeroese', 'Føroyskt'),
|
||||
'fr' => array('French', 'Français'),
|
||||
'fy' => array('Frisian, Western', 'Frysk'),
|
||||
'ga' => array('Irish', 'Gaeilge'),
|
||||
'gd' => array('Scots Gaelic', 'Gàidhlig'),
|
||||
'gl' => array('Galician', 'Galego'),
|
||||
'gsw-berne' => array('Swiss German', 'Schwyzerdütsch'),
|
||||
'gu' => array('Gujarati', 'ગુજરાતી'),
|
||||
'he' => array('Hebrew', /* Left-to-right marker "" */ 'עברית', LanguageInterface::DIRECTION_RTL),
|
||||
'hi' => array('Hindi', 'हिन्दी'),
|
||||
'hr' => array('Croatian', 'Hrvatski'),
|
||||
'ht' => array('Haitian Creole', 'Kreyòl ayisyen'),
|
||||
'hu' => array('Hungarian', 'Magyar'),
|
||||
'hy' => array('Armenian', 'Հայերեն'),
|
||||
'id' => array('Indonesian', 'Bahasa Indonesia'),
|
||||
'is' => array('Icelandic', 'Íslenska'),
|
||||
'it' => array('Italian', 'Italiano'),
|
||||
'ja' => array('Japanese', '日本語'),
|
||||
'jv' => array('Javanese', 'Basa Java'),
|
||||
'ka' => array('Georgian', 'ქართული ენა'),
|
||||
'kk' => array('Kazakh', 'Қазақ'),
|
||||
'km' => array('Khmer', 'ភាសាខ្មែរ'),
|
||||
'kn' => array('Kannada', 'ಕನ್ನಡ'),
|
||||
'ko' => array('Korean', '한국어'),
|
||||
'ku' => array('Kurdish', 'Kurdî'),
|
||||
'ky' => array('Kyrgyz', 'Кыргызча'),
|
||||
'lo' => array('Lao', 'ພາສາລາວ'),
|
||||
'lt' => array('Lithuanian', 'Lietuvių'),
|
||||
'lv' => array('Latvian', 'Latviešu'),
|
||||
'mg' => array('Malagasy', 'Malagasy'),
|
||||
'mk' => array('Macedonian', 'Македонски'),
|
||||
'ml' => array('Malayalam', 'മലയാളം'),
|
||||
'mn' => array('Mongolian', 'монгол'),
|
||||
'mr' => array('Marathi', 'मराठी'),
|
||||
'ms' => array('Bahasa Malaysia', 'بهاس ملايو'),
|
||||
'my' => array('Burmese', 'ဗမာစကား'),
|
||||
'ne' => array('Nepali', 'नेपाली'),
|
||||
'nl' => array('Dutch', 'Nederlands'),
|
||||
'nb' => array('Norwegian Bokmål', 'Norsk, bokmål'),
|
||||
'nn' => array('Norwegian Nynorsk', 'Norsk, nynorsk'),
|
||||
'oc' => array('Occitan', 'Occitan'),
|
||||
'pa' => array('Punjabi', 'ਪੰਜਾਬੀ'),
|
||||
'pl' => array('Polish', 'Polski'),
|
||||
'pt-pt' => array('Portuguese, Portugal', 'Português, Portugal'),
|
||||
'pt-br' => array('Portuguese, Brazil', 'Português, Brasil'),
|
||||
'ro' => array('Romanian', 'Română'),
|
||||
'ru' => array('Russian', 'Русский'),
|
||||
'sco' => array('Scots', 'Scots'),
|
||||
'se' => array('Northern Sami', 'Sámi'),
|
||||
'si' => array('Sinhala', 'සිංහල'),
|
||||
'sk' => array('Slovak', 'Slovenčina'),
|
||||
'sl' => array('Slovenian', 'Slovenščina'),
|
||||
'sq' => array('Albanian', 'Shqip'),
|
||||
'sr' => array('Serbian', 'Српски'),
|
||||
'sv' => array('Swedish', 'Svenska'),
|
||||
'sw' => array('Swahili', 'Kiswahili'),
|
||||
'ta' => array('Tamil', 'தமிழ்'),
|
||||
'ta-lk' => array('Tamil, Sri Lanka', 'தமிழ், இலங்கை'),
|
||||
'te' => array('Telugu', 'తెలుగు'),
|
||||
'th' => array('Thai', 'ภาษาไทย'),
|
||||
'tr' => array('Turkish', 'Türkçe'),
|
||||
'tyv' => array('Tuvan', 'Тыва дыл'),
|
||||
'ug' => array('Uyghur', 'Уйғур'),
|
||||
'uk' => array('Ukrainian', 'Українська'),
|
||||
'ur' => array('Urdu', /* Left-to-right marker "" */ 'اردو', LanguageInterface::DIRECTION_RTL),
|
||||
'vi' => array('Vietnamese', 'Tiếng Việt'),
|
||||
'xx-lolspeak' => array('Lolspeak', 'Lolspeak'),
|
||||
'zh-hans' => array('Chinese, Simplified', '简体中文'),
|
||||
'zh-hant' => array('Chinese, Traditional', '繁體中文'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* This function is a noop since the configuration cannot be overridden by
|
||||
* language unless the Language module is enabled. That replaces the default
|
||||
* language manager with a configurable language manager.
|
||||
*
|
||||
* @see \Drupal\language\ConfigurableLanguageManager::setConfigOverrideLanguage()
|
||||
*/
|
||||
public function setConfigOverrideLanguage(LanguageInterface $language = NULL) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConfigOverrideLanguage() {
|
||||
return $this->getCurrentLanguage();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Filters the full list of languages based on the value of the flag.
|
||||
*
|
||||
* The locked languages are removed by default.
|
||||
*
|
||||
* @param \Drupal\Core\Language\LanguageInterface[] $languages
|
||||
* Array with languages to be filtered.
|
||||
* @param int $flags
|
||||
* (optional) Specifies the state of the languages that have to be returned.
|
||||
* It can be: LanguageInterface::STATE_CONFIGURABLE,
|
||||
* LanguageInterface::STATE_LOCKED, or LanguageInterface::STATE_ALL.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageInterface[]
|
||||
* An associative array of languages, keyed by the language code.
|
||||
*/
|
||||
protected function filterLanguages(array $languages, $flags = LanguageInterface::STATE_CONFIGURABLE) {
|
||||
// STATE_ALL means we don't actually filter, so skip the rest of the method.
|
||||
if ($flags == LanguageInterface::STATE_ALL) {
|
||||
return $languages;
|
||||
}
|
||||
|
||||
$filtered_languages = array();
|
||||
// Add the site's default language if requested.
|
||||
if ($flags & LanguageInterface::STATE_SITE_DEFAULT) {
|
||||
|
||||
// Setup a language to have the defaults with data appropriate of the
|
||||
// default language only for runtime.
|
||||
$defaultLanguage = $this->getDefaultLanguage();
|
||||
$default = new Language(
|
||||
array(
|
||||
'id' => $defaultLanguage->getId(),
|
||||
'name' => $this->t("Site's default language (@lang_name)",
|
||||
array('@lang_name' => $defaultLanguage->getName())),
|
||||
'direction' => $defaultLanguage->getDirection(),
|
||||
'weight' => $defaultLanguage->getWeight(),
|
||||
)
|
||||
);
|
||||
$filtered_languages[LanguageInterface::LANGCODE_SITE_DEFAULT] = $default;
|
||||
}
|
||||
|
||||
foreach ($languages as $id => $language) {
|
||||
if (($language->isLocked() && ($flags & LanguageInterface::STATE_LOCKED)) || (!$language->isLocked() && ($flags & LanguageInterface::STATE_CONFIGURABLE))) {
|
||||
$filtered_languages[$id] = $language;
|
||||
}
|
||||
}
|
||||
|
||||
return $filtered_languages;
|
||||
}
|
||||
|
||||
}
|
226
core/lib/Drupal/Core/Language/LanguageManagerInterface.php
Normal file
226
core/lib/Drupal/Core/Language/LanguageManagerInterface.php
Normal file
|
@ -0,0 +1,226 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Language\LanguageManagerInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Language;
|
||||
|
||||
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Common interface for the language manager service.
|
||||
*/
|
||||
interface LanguageManagerInterface {
|
||||
|
||||
/**
|
||||
* Injects the string translation service.
|
||||
*
|
||||
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
|
||||
* The string translation service.
|
||||
*/
|
||||
public function setTranslation(TranslationInterface $translation);
|
||||
|
||||
/**
|
||||
* Returns whether or not the site has more than one language added.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if more than one language is added, FALSE otherwise.
|
||||
*/
|
||||
public function isMultilingual();
|
||||
|
||||
/**
|
||||
* Returns an array of the available language types.
|
||||
*
|
||||
* @return array
|
||||
* An array of language type machine names.
|
||||
*/
|
||||
public function getLanguageTypes();
|
||||
|
||||
/**
|
||||
* Returns information about all defined language types.
|
||||
*
|
||||
* @return array
|
||||
* An associative array of language type information arrays keyed by
|
||||
* language type machine name, in the format of
|
||||
* hook_language_types_info(). In some implementing classes, this is based
|
||||
* on information from hook_language_types_info() and
|
||||
* hook_language_types_info_alter().
|
||||
*/
|
||||
public function getDefinedLanguageTypesInfo();
|
||||
|
||||
/**
|
||||
* Returns the current language for the given type.
|
||||
*
|
||||
* @param string $type
|
||||
* (optional) The language type; e.g., the interface or the content
|
||||
* language. Defaults to
|
||||
* \Drupal\Core\Language\LanguageInterface::TYPE_INTERFACE.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageInterface
|
||||
* The current language object for the given type of language.
|
||||
*/
|
||||
public function getCurrentLanguage($type = LanguageInterface::TYPE_INTERFACE);
|
||||
|
||||
/**
|
||||
* Resets the given language type or all types if none specified.
|
||||
*
|
||||
* @param string|null $type
|
||||
* (optional) The language type to reset as a string, e.g.,
|
||||
* LanguageInterface::TYPE_INTERFACE, or NULL to reset all language types.
|
||||
* Defaults to NULL.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageManagerInterface
|
||||
* The language manager that has been reset.
|
||||
*/
|
||||
public function reset($type = NULL);
|
||||
|
||||
/**
|
||||
* Returns a language object representing the site's default language.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageInterface
|
||||
* A language object.
|
||||
*/
|
||||
public function getDefaultLanguage();
|
||||
|
||||
/**
|
||||
* Returns a list of languages set up on the site.
|
||||
*
|
||||
* @param int $flags
|
||||
* (optional) Specifies the state of the languages that have to be returned.
|
||||
* It can be: LanguageInterface::STATE_CONFIGURABLE,
|
||||
* LanguageInterface::STATE_LOCKED, or LanguageInterface::STATE_ALL.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageInterface[]
|
||||
* An associative array of languages, keyed by the language code.
|
||||
*/
|
||||
public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE);
|
||||
|
||||
/**
|
||||
* Returns a list of languages set up on the site in their native form.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageInterface[]
|
||||
* An associative array of languages, keyed by the language code, ordered
|
||||
* by weight ascending and name ascending.
|
||||
*/
|
||||
public function getNativeLanguages();
|
||||
|
||||
/**
|
||||
* Returns a language object from the given language code.
|
||||
*
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*
|
||||
* @return \Drupal\core\Language\LanguageInterface|null
|
||||
* A fully-populated language object or NULL.
|
||||
*/
|
||||
public function getLanguage($langcode);
|
||||
|
||||
/**
|
||||
* Produced the printed name for a language for display.
|
||||
*
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*
|
||||
* @return string
|
||||
* The printed name of the language.
|
||||
*/
|
||||
public function getLanguageName($langcode);
|
||||
|
||||
/**
|
||||
* Returns a list of the default locked languages.
|
||||
*
|
||||
* @param int $weight
|
||||
* (optional) An integer value that is used as the start value for the
|
||||
* weights of the locked languages.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageInterface[]
|
||||
* An array of language objects.
|
||||
*/
|
||||
public function getDefaultLockedLanguages($weight = 0);
|
||||
|
||||
/**
|
||||
* Checks whether a language is locked.
|
||||
*
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*
|
||||
* @return bool
|
||||
* Returns whether the language is locked.
|
||||
*/
|
||||
public function isLanguageLocked($langcode);
|
||||
|
||||
/**
|
||||
* Returns the language fallback candidates for a given context.
|
||||
*
|
||||
* @param array $context
|
||||
* (optional) An associative array of data that can be useful to determine
|
||||
* the fallback sequence. The following keys are used in core:
|
||||
* - langcode: Language code of the desired language.
|
||||
* - operation: The name of the operation indicating the context where
|
||||
* language fallback is being applied. The following operations are
|
||||
* defined in core, but more may be defined in contributed modules:
|
||||
* - entity_view: Invoked when an entity is about to be displayed.
|
||||
* The data key contains the loaded entity.
|
||||
* - views_query: Invoked when a field based views query is performed.
|
||||
* The data key contains a reference to the field object.
|
||||
* - locale_lookup: Invoked when a string translation was not found.
|
||||
* The data key contains the source string.
|
||||
* - data: A data structure that makes sense in the provided
|
||||
* context, see above.
|
||||
*
|
||||
* @return array
|
||||
* An array of language codes sorted by priority: first values should be
|
||||
* tried first.
|
||||
*/
|
||||
public function getFallbackCandidates(array $context = array());
|
||||
|
||||
/**
|
||||
* Returns the language switch links for the given language type.
|
||||
*
|
||||
* @param string $type
|
||||
* The language type.
|
||||
* @param \Drupal\Core\Url $url
|
||||
* The URL the switch links will be relative to.
|
||||
*
|
||||
* @return array
|
||||
* A keyed array of links ready to be themed.
|
||||
*/
|
||||
public function getLanguageSwitchLinks($type, Url $url);
|
||||
|
||||
/**
|
||||
* Sets the configuration override language.
|
||||
*
|
||||
* @param \Drupal\Core\Language\LanguageInterface $language
|
||||
* The language to override configuration with.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setConfigOverrideLanguage(LanguageInterface $language = NULL);
|
||||
|
||||
/**
|
||||
* Gets the current configuration override language.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageInterface
|
||||
* The current configuration override language.
|
||||
*/
|
||||
public function getConfigOverrideLanguage();
|
||||
|
||||
/**
|
||||
* Some common languages with their English and native names.
|
||||
*
|
||||
* Language codes are defined by the W3C language tags document for
|
||||
* interoperability. Language codes typically have a language and, optionally,
|
||||
* a script or regional variant name. See:
|
||||
* http://www.w3.org/International/articles/language-tags/ for more
|
||||
* information.
|
||||
*
|
||||
* @return array
|
||||
* An array of language code to language name information. Language name
|
||||
* information itself is an array of English and native names.
|
||||
*/
|
||||
public static function getStandardLanguageList();
|
||||
|
||||
}
|
260
core/lib/Drupal/Core/Language/language.api.php
Normal file
260
core/lib/Drupal/Core/Language/language.api.php
Normal file
|
@ -0,0 +1,260 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Hooks provided by the base system for language support.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
|
||||
/**
|
||||
* @defgroup i18n Internationalization
|
||||
* @{
|
||||
* Internationalization and translation
|
||||
*
|
||||
* The principle of internationalization is that it should be possible to make a
|
||||
* Drupal site in any language (or a multi-lingual site), where only content in
|
||||
* the desired language is displayed for any particular page request. In order
|
||||
* to make this happen, developers of modules, themes, and installation profiles
|
||||
* need to make sure that all of the displayable content and user interface (UI)
|
||||
* text that their project deals with is internationalized properly, so that it
|
||||
* can be translated using the standard Drupal translation mechanisms.
|
||||
*
|
||||
* @section internationalization Internationalization
|
||||
* Different @link info_types types of information in Drupal @endlink have
|
||||
* different methods for internationalization, and different portions of the
|
||||
* UI also have different methods for internationalization. Here is a list of
|
||||
* the different mechanisms for internationalization, and some notes:
|
||||
* - UI text is always put into code and related files in English.
|
||||
* - Any time UI text is displayed using PHP code, it should be passed through
|
||||
* either the global t() function or a t() method on the class. If it
|
||||
* involves plurals, it should be passed through either the global
|
||||
* formatPlural() function or a formatPlural() method on the class. Use
|
||||
* \Drupal\Core\StringTranslation\StringTranslationTrait to get these methods
|
||||
* into a class.
|
||||
* - Dates displayed in the UI should be passed through the 'date' service
|
||||
* class's format() method. Again see the Services topic; the method to
|
||||
* call is \Drupal\Core\Datetime\Date::format().
|
||||
* - Some YML files contain UI text that is automatically translatable:
|
||||
* - *.routing.yml files: route titles. This also applies to
|
||||
* *.links.task.yml, *.links.action.yml, and *.links.contextual.yml files.
|
||||
* - *.info.yml files: module names and descriptions.
|
||||
* - For configuration, make sure any configuration that is displayable to
|
||||
* users is marked as translatable in the configuration schema. Configuration
|
||||
* types label, text, and date_format are translatable; string is
|
||||
* non-translatable text. See the @link config_api Config API topic @endlink
|
||||
* for more information.
|
||||
* - For annotation, make sure that any text that is displayable in the UI
|
||||
* is wrapped in \@Translation(). See the
|
||||
* @link plugin_translatable Plugin translatables topic @endlink for more
|
||||
* information.
|
||||
* - Content entities are translatable if they have
|
||||
* @code
|
||||
* translatable = TRUE,
|
||||
* @endcode
|
||||
* in their annotation. The use of entities to store user-editable content to
|
||||
* be displayed in the site is highly recommended over creating your own
|
||||
* method for storing, retrieving, displaying, and internationalizing content.
|
||||
* - For Twig templates, use 't' or 'trans' filters to indicate translatable
|
||||
* text. See https://www.drupal.org/node/2133321 for more information.
|
||||
* - In JavaScript code, use the Drupal.t() and Drupal.formatPlural() functions
|
||||
* (defined in core/misc/drupal.js) to translate UI text.
|
||||
* - If you are using a custom module, theme, etc. that is not hosted on
|
||||
* Drupal.org, see
|
||||
* @link interface_translation_properties Interface translation properties topic @endlink
|
||||
* for information on how to make sure your UI text is translatable.
|
||||
*
|
||||
* @section translation Translation
|
||||
* Once your data and user interface are internationalized, the following Core
|
||||
* modules are used to translate it into different languages (machine names of
|
||||
* modules in parentheses):
|
||||
* - Language (language): Define which languages are active on the site.
|
||||
* - Interface Translation (locale): Translate UI text.
|
||||
* - Content Translation (content_translation): Translate content entities.
|
||||
* - Configuration Translation (config_translation): Translate configuration.
|
||||
*
|
||||
* The Interface Translation module deserves special mention, because besides
|
||||
* providing a UI for translating UI text, it also imports community
|
||||
* translations from the
|
||||
* @link https://localize.drupal.org Drupal translation server. @endlink If
|
||||
* UI text and provided configuration in Drupal Core and contributed modules,
|
||||
* themes, and installation profiles is properly internationalized (as described
|
||||
* above), the text is automatically added to the translation server for
|
||||
* community members to translate, via *.po files that are generated by
|
||||
* scanning the project files.
|
||||
*
|
||||
* @section context Translation string sharing and context
|
||||
* By default, translated strings are only translated once, no matter where
|
||||
* they are being used. For instance, there are many forms with Save
|
||||
* buttons on them, and they all would have t('Save') in their code. The
|
||||
* translation system will only store this string once in the translation
|
||||
* database, so that if the translation is updated, all forms using that text
|
||||
* will get the updated translation.
|
||||
*
|
||||
* Because the source of translation strings is English, and some words in
|
||||
* English have multiple meanings or uses, this centralized, shared translation
|
||||
* string storage can sometimes lead to ambiguous translations that are not
|
||||
* correct for every place the string is used. As an example, the English word
|
||||
* "May", in a string by itself, could be part of a list of full month names or
|
||||
* part of a list of 3-letter abbreviated month names. So, in languages where
|
||||
* the month name for May is longer than 3 letters, you'd need to translate May
|
||||
* differently depending on how it's being used. To address this problem, the
|
||||
* translation system includes the concept of the "context" of a translated
|
||||
* string, which can be used to disambiguate text for translators, and obtain
|
||||
* the correct translation for each usage of the string.
|
||||
*
|
||||
* Here are some examples of how to provide translation context with strings, so
|
||||
* that this information can be included in *.po files, displayed on the
|
||||
* localization server for translators, and used to obtain the correct
|
||||
* translation in the user interface:
|
||||
* @code
|
||||
* // PHP code
|
||||
* t('May', array(), array('context' => 'Long month name');
|
||||
* format_plural($count, '1 something', '@count somethings',
|
||||
* array(), array('context' => 'My context'));
|
||||
*
|
||||
* // JavaScript code
|
||||
* Drupal.t('May', {}, {'context': 'Long month name'});
|
||||
* Drupal.formatPlural(count, '1 something', '@count somethings', {},
|
||||
* {'context': 'My context'});
|
||||
*
|
||||
* // *.links.yml file
|
||||
* title: 'May'
|
||||
* title_context: 'Long month name'
|
||||
*
|
||||
* // *.routing.yml file
|
||||
* my.route.name:
|
||||
* pattern: '/something'
|
||||
* defaults:
|
||||
* _title: 'May'
|
||||
* _title_context: 'Long month name'
|
||||
*
|
||||
* // Config schema to say that a certain piece of configuration should be
|
||||
* // translatable using the Config Translation API. Note that the schema label
|
||||
* // is also translatable, but it cannot have context.
|
||||
* date_format:
|
||||
* type: string
|
||||
* label: 'PHP date format'
|
||||
* translatable: true
|
||||
* translation context: 'PHP date format'
|
||||
*
|
||||
* // Twig template
|
||||
* {% trans with {'context': 'Long month name'} %}
|
||||
* May
|
||||
* {% endtrans %}
|
||||
* @endcode
|
||||
*
|
||||
* @see transliteration
|
||||
* @see t()
|
||||
* @see format_plural()
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup hooks
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Perform alterations on language switcher links.
|
||||
*
|
||||
* A language switcher link may need to point to a different path or use a
|
||||
* translated link text before going through _l(), which will just handle the
|
||||
* path aliases.
|
||||
*
|
||||
* @param $links
|
||||
* Nested array of links keyed by language code.
|
||||
* @param $type
|
||||
* The language type the links will switch.
|
||||
* @param $path
|
||||
* The current path.
|
||||
*/
|
||||
function hook_language_switch_links_alter(array &$links, $type, $path) {
|
||||
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
|
||||
|
||||
if ($type == LanguageInterface::TYPE_CONTENT && isset($links[$language_interface->getId()])) {
|
||||
foreach ($links[$language_interface->getId()] as $link) {
|
||||
$link['attributes']['class'][] = 'active-language';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "addtogroup hooks".
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup transliteration Transliteration
|
||||
* @{
|
||||
* Transliterate from Unicode to US-ASCII
|
||||
*
|
||||
* Transliteration is the process of translating individual non-US-ASCII
|
||||
* characters into ASCII characters, which specifically does not transform
|
||||
* non-printable and punctuation characters in any way. This process will always
|
||||
* be both inexact and language-dependent. For instance, the character Ö (O with
|
||||
* an umlaut) is commonly transliterated as O, but in German text, the
|
||||
* convention would be to transliterate it as Oe or OE, depending on the context
|
||||
* (beginning of a capitalized word, or in an all-capital letter context).
|
||||
*
|
||||
* The Drupal default transliteration process transliterates text character by
|
||||
* character using a database of generic character transliterations and
|
||||
* language-specific overrides. Character context (such as all-capitals
|
||||
* vs. initial capital letter only) is not taken into account, and in
|
||||
* transliterations of capital letters that result in two or more letters, by
|
||||
* convention only the first is capitalized in the Drupal transliteration
|
||||
* result. Also, only Unicode characters of 4 bytes or less can be
|
||||
* transliterated in the base system; language-specific overrides can be made
|
||||
* for longer Unicode characters. So, the process has limitations; however,
|
||||
* since the reason for transliteration is typically to create machine names or
|
||||
* file names, this should not really be a problem. After transliteration,
|
||||
* other transformation or validation may be necessary, such as converting
|
||||
* spaces to another character, removing non-printable characters,
|
||||
* lower-casing, etc.
|
||||
*
|
||||
* Here is a code snippet to transliterate some text:
|
||||
* @code
|
||||
* // Use the current default interface language.
|
||||
* $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||
* // Instantiate the transliteration class.
|
||||
* $trans = \Drupal::transliteration();
|
||||
* // Use this to transliterate some text.
|
||||
* $transformed = $trans->transliterate($string, $langcode);
|
||||
* @endcode
|
||||
*
|
||||
* Drupal Core provides the generic transliteration character tables and
|
||||
* overrides for a few common languages; modules can implement
|
||||
* hook_transliteration_overrides_alter() to provide further language-specific
|
||||
* overrides (including providing transliteration for Unicode characters that
|
||||
* are longer than 4 bytes). Modules can also completely override the
|
||||
* transliteration classes in \Drupal\Core\CoreServiceProvider.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Provide language-specific overrides for transliteration.
|
||||
*
|
||||
* If the overrides you want to provide are standard for your language, consider
|
||||
* providing a patch for the Drupal Core transliteration system instead of using
|
||||
* this hook. This hook can be used temporarily until Drupal Core's
|
||||
* transliteration tables are fixed, or for sites that want to use a
|
||||
* non-standard transliteration system.
|
||||
*
|
||||
* @param array $overrides
|
||||
* Associative array of language-specific overrides whose keys are integer
|
||||
* Unicode character codes, and whose values are the transliterations of those
|
||||
* characters in the given language, to override default transliterations.
|
||||
* @param string $langcode
|
||||
* The code for the language that is being transliterated.
|
||||
*
|
||||
* @ingroup hooks
|
||||
*/
|
||||
function hook_transliteration_overrides_alter(&$overrides, $langcode) {
|
||||
// Provide special overrides for German for a custom site.
|
||||
if ($langcode == 'de') {
|
||||
// The core-provided transliteration of Ä is Ae, but we want just A.
|
||||
$overrides[0xC4] = 'A';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "defgroup transliteration".
|
||||
*/
|
Reference in a new issue