Language
hook_language_types_info
/**
* Define language types.
*
* @return array
* An associative array of language type definitions. The keys are the
* identifiers, which are also used as names for global variables representing
* the types in the bootstrap phase. The values are associative arrays that
* may contain the following elements:
* - name: The human-readable language type identifier.
* - description: A description of the language type.
* - locked: A boolean indicating if the user can choose whether to configure
* the language type or not using the UI.
* - fixed: A fixed array of language negotiation method identifiers to use to
* initialize this language. If locked is set to TRUE and fixed is set, it
* will always use the specified methods in the given priority order. If not
* present and locked is TRUE then language-interface will be
* used.
*
* @todo Rename the 'fixed' key to something more meaningful, for instance
* 'negotiation settings'. See https://www.drupal.org/node/2166879.
*
* @see hook_language_types_info_alter()
*/
function hook_language_types_info(): array {
return [
'custom_language_type' => [
'name' => t('Custom language'),
'description' => t('A custom language type.'),
'locked' => FALSE,
],
'fixed_custom_language_type' => [
'locked' => TRUE,
'fixed' => ['custom_language_negotiation_method'],
],
];
}
hook_language_types_info_alter
/**
* Perform alterations on language types.
*
* @param array $language_types
* Array of language type definitions.
*
* @see hook_language_types_info()
*/
function hook_language_types_info_alter(array &$language_types) {
if (isset($language_types['custom_language_type'])) {
$language_types['custom_language_type_custom']['description'] = t('A far better description.');
}
}
hook_language_negotiation_info_alter
/**
* Perform alterations on language negotiation methods.
*
* @param array $negotiation_info
* Array of language negotiation method definitions.
*/
function hook_language_negotiation_info_alter(array &$negotiation_info) {
if (isset($negotiation_info['custom_language_method'])) {
$negotiation_info['custom_language_method']['config'] = 'admin/config/regional/language/detection/custom-language-method';
}
}
hook_language_fallback_candidates_alter
/**
* Allow modules to alter the language fallback candidates.
*
* @param array $candidates
* An array of language codes whose order will determine the language fallback
* order.
* @param array $context
* A language fallback context.
*
* @see \Drupal\Core\Language\LanguageManagerInterface::getFallbackCandidates()
*/
function hook_language_fallback_candidates_alter(array &$candidates, array $context) {
$candidates = array_reverse($candidates);
}
hook_language_fallback_candidates_OPERATION_alter
/**
* Allow modules to alter the fallback candidates for specific operations.
*
* @param array $candidates
* An array of language codes whose order will determine the language fallback
* order.
* @param array $context
* A language fallback context.
*
* @see \Drupal\Core\Language\LanguageManagerInterface::getFallbackCandidates()
*/
function hook_language_fallback_candidates_OPERATION_alter(array &$candidates, array $context) {
// We know that the current OPERATION deals with entities so no need to check
// here.
if ($context['data']->getEntityTypeId() == 'node') {
$candidates = array_reverse($candidates);
}
}
hook_language_switch_links_alter
/**
* 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 the link generator, which will
* just handle the path aliases.
*
* @param array $links
* Nested array of links keyed by language code.
* @param string $type
* The language type the links will switch.
* @param \Drupal\Core\Url $url
* The URL the switch links will be relative to.
*/
function hook_language_switch_links_alter(array &$links, $type, \Drupal\Core\Url $url) {
$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';
}
}
}
hook_transliteration_overrides_alter
/**
* 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.
*/
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';
}
}