' . t('About') . ''; $output .= '
' . t('The Language module allows you to configure the languages used on your site, and provides information for the Content Translation, Interface Translation, and Configuration Translation modules, if they are enabled. For more information, see the online documentation for the Language module.', [':doc_url' => 'https://www.drupal.org/documentation/modules/language', ':content' => (\Drupal::moduleHandler()->moduleExists('content_translation')) ? Url::fromRoute('help.page', ['name' => 'content_translation'])->toString() : '#', ':interface' => (\Drupal::moduleHandler()->moduleExists('locale')) ? Url::fromRoute('help.page', ['name' => 'locale'])->toString() : '#', ':configuration' => (\Drupal::moduleHandler()->moduleExists('config_translation')) ? Url::fromRoute('help.page', ['name' => 'config_translation'])->toString() : '#']) . '
'; $output .= '' . t('Reorder the configured languages to set their order in the language switcher block and, when editing content, in the list of selectable languages. This ordering does not impact detection and selection.', [':detection' => Url::fromRoute('language.negotiation')->toString()]) . '
'; $output .= '' . t('The site default language can also be set. It is not recommended to change the default language on a working site. Configure the Selected language setting on the detection and selection page to change the fallback language for language selection.', [':language-detection' => Url::fromRoute('language.negotiation')->toString()]) . '
'; return $output; case 'language.add': return '' . t('Add a language to be supported by your site. If your desired language is not available, pick Custom language... at the end and provide a language code and other details manually.') . '
'; case 'language.negotiation': $output = '' . t('Define how to decide which language is used to display page elements (primarily text provided by modules, such as field labels and help text). This decision is made by evaluating a series of detection methods for languages; the first detection method that gets a result will determine which language is used for that type of text. Be aware that some language detection methods are unreliable under certain conditions, such as browser detection when page-caching is enabled and a user is not currently logged in. Define the order of evaluation of language detection methods on this page. The default language can be changed in the list of languages.', [':admin-change-language' => Url::fromRoute('entity.configurable_language.collection')->toString()]) . '
'; return $output; case 'language.negotiation_session': $output = '' . t('Determine the language from a request/session parameter. Example: "http://example.com?language=de" sets language to German based on the use of "de" within the "language" parameter.') . '
'; return $output; case 'language.negotiation_browser': $output = '' . t('Browsers use different language codes to refer to the same languages. Internally, a best effort is made to determine the correct language based on the code that the browser sends. You can add and edit additional mappings from browser language codes to site languages.', [':configure-languages' => Url::fromRoute('entity.configurable_language.collection')->toString()]) . '
'; return $output; case 'language.negotiation_selected': $output = '' . t('Changing the selected language here (and leaving this option as the last among the detection and selection options) is the easiest way to change the fallback language for the website, if you need to change how your site works by default (e.g., when using an empty path prefix or using the default domain). Changing the site\'s default language itself might have other undesired side effects.', [':admin-change-language' => Url::fromRoute('entity.configurable_language.collection')->toString()]) . '
'; return $output; case 'entity.block.edit_form': if (($block = $route_match->getParameter('block')) && $block->getPluginId() == 'language_block:language_interface') { return '' . t('With multiple languages configured, registered users can select their preferred language and authors can assign a specific language to content.') . '
'; } break; case 'block.admin_add': if ($route_match->getParameter('plugin_id') == 'language_block:language_interface') { return '' . t('With multiple languages configured, registered users can select their preferred language and authors can assign a specific language to content.') . '
'; } break; case 'language.content_settings_page': return '' . t("Change language settings for content types, taxonomy vocabularies, user profiles, or any other supported element on your site. By default, language settings hide the language selector and the language is the site's default language.") . '
'; } } /** * Implements hook_theme(). */ function language_theme() { return [ 'language_negotiation_configure_form' => [ 'render element' => 'form', 'file' => 'language.admin.inc', ], 'language_content_settings_table' => [ 'render element' => 'element', 'file' => 'language.admin.inc', ], ]; } /** * Implements hook_element_info_alter(). * * @see \Drupal\Core\Render\Element\LanguageSelect * @see \Drupal\Core\Render\Element\Select */ function language_element_info_alter(&$type) { // Alter the language_select element so that it will be rendered like a select // field. if (isset($type['language_select'])) { if (!isset($type['language_select']['#process'])) { $type['language_select']['#process'] = []; } if (!isset($type['language_select']['#theme_wrappers'])) { $type['language_select']['#theme_wrappers'] = []; } $type['language_select']['#process'] = array_merge($type['language_select']['#process'], [ 'language_process_language_select', ['Drupal\Core\Render\Element\Select', 'processSelect'], ['Drupal\Core\Render\Element\RenderElement', 'processAjaxForm'], ]); $type['language_select']['#theme'] = 'select'; $type['language_select']['#theme_wrappers'] = array_merge($type['language_select']['#theme_wrappers'], ['form_element']); $type['language_select']['#languages'] = LanguageInterface::STATE_CONFIGURABLE; $type['language_select']['#multiple'] = FALSE; } } /** * Processes a language select list form element. * * @param array $element * The form element to process. * * @return array * The processed form element. */ function language_process_language_select($element) { // Don't set the options if another module (translation for example) already // set the options. if (!isset($element['#options'])) { $element['#options'] = []; foreach (\Drupal::languageManager()->getLanguages($element['#languages']) as $langcode => $language) { $element['#options'][$langcode] = $language->isLocked() ? t('- @name -', ['@name' => $language->getName()]) : $language->getName(); } } return $element; } /** * Implements hook_entity_base_field_info_alter(). */ function language_entity_base_field_info_alter(&$fields) { foreach ($fields as $definition) { // Set configurable form display for language fields with display options. if ($definition->getType() == 'language') { foreach (['form', 'view'] as $type) { if ($definition->getDisplayOptions($type)) { // The related configurations will be purged manually on Language // module uninstallation. @see language_modules_uninstalled(). $definition->setDisplayConfigurable($type, TRUE); } } } } } /** * Submit handler for the forms that have a language_configuration element. */ function language_configuration_element_submit(&$form, FormStateInterface $form_state) { // Iterate through all the language_configuration elements and save their // values. // In case we are editing a bundle, we must check the new bundle name, // because e.g. hook_ENTITY_update fired before. if ($language = $form_state->get('language')) { foreach ($language as $element_name => $values) { $entity_type_id = $values['entity_type']; $bundle = $values['bundle']; $form_object = $form_state->getFormObject(); if ($form_object instanceof EntityFormInterface) { /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */ $entity = $form_object->getEntity(); if ($entity->getEntityType()->getBundleOf()) { $bundle = $entity->id(); $language[$element_name]['bundle'] = $bundle; } } $config = ContentLanguageSettings::loadByEntityTypeBundle($entity_type_id, $bundle); $config->setDefaultLangcode($form_state->getValue([$element_name, 'langcode'])); $config->setLanguageAlterable($form_state->getValue([$element_name, 'language_alterable'])); $config->save(); // Set the form_state language with the updated bundle. $form_state->set('language', $language); } } } /** * Implements hook_entity_bundle_delete(). */ function language_entity_bundle_delete($entity_type_id, $bundle) { // Remove the content language settings associated with the bundle. $settings = ContentLanguageSettings::loadByEntityTypeBundle($entity_type_id, $bundle); if (!$settings->isNew()) { $settings->delete(); } } /** * Returns the default language code assigned to an entity type and a bundle. * * @param string $entity_type * The entity type. * @param string $bundle * The bundle name. * * @return string * The language code. */ function language_get_default_langcode($entity_type, $bundle) { $configuration = ContentLanguageSettings::loadByEntityTypeBundle($entity_type, $bundle); $default_value = NULL; $language_interface = \Drupal::languageManager()->getCurrentLanguage(); switch ($configuration->getDefaultLangcode()) { case LanguageInterface::LANGCODE_SITE_DEFAULT: $default_value = \Drupal::languageManager()->getDefaultLanguage()->getId(); break; case 'current_interface': $default_value = $language_interface->getId(); break; case 'authors_default': $user = \Drupal::currentUser(); $language_code = $user->getPreferredLangcode(); if (!empty($language_code)) { $default_value = $language_code; } else { $default_value = $language_interface->getId(); } break; } if ($default_value) { return $default_value; } // If we still do not have a default value, just return the value stored in // the configuration; it has to be an actual language code. return $configuration->getDefaultLangcode(); } /** * Reads language prefixes and uses the langcode if no prefix is set. * * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use * \Drupal::config('language.negotiation')->get('url.prefixes') instead. * * @see https://www.drupal.org/node/2912748 */ function language_negotiation_url_prefixes() { @trigger_error("language_negotiation_url_prefixes() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal::config('language.negotiation')->get('url.prefixes') instead. See https://www.drupal.org/node/2912748", E_USER_DEPRECATED); return \Drupal::config('language.negotiation')->get('url.prefixes'); } /** * Update the list of prefixes from the installed languages. */ function language_negotiation_url_prefixes_update() { $config = \Drupal::configFactory()->getEditable('language.negotiation'); $prefixes = $config->get('url.prefixes'); foreach (\Drupal::languageManager()->getLanguages() as $language) { // The prefix for this language should be updated if it's not assigned yet // or the prefix is set to the empty string. if (empty($prefixes[$language->getId()])) { // For the default language, set the prefix to the empty string, // otherwise use the langcode. $prefixes[$language->getId()] = $language->isDefault() ? '' : $language->getId(); } // Otherwise we keep the configured prefix. } $config->set('url.prefixes', $prefixes)->save(TRUE); } /** * Reads language domains. * * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use * \Drupal::config('language.negotiation')->get('url.domains') instead. * * @see https://www.drupal.org/node/2912748 */ function language_negotiation_url_domains() { @trigger_error("language_negotiation_url_domains() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal::config('language.negotiation')->get('url.domains') instead. See https://www.drupal.org/node/2912748", E_USER_DEPRECATED); return \Drupal::config('language.negotiation')->get('url.domains'); } /** * Implements hook_modules_installed(). */ function language_modules_installed($modules) { if (!in_array('language', $modules)) { // Since newly (un)installed modules may change the default settings for // non-locked language types (e.g. content language), we need to resave the // language type configuration. /** @var \Drupal\language\LanguageNegotiatorInterface $negotiator */ $negotiator = \Drupal::service('language_negotiator'); $configurable = \Drupal::config('language.types')->get('configurable'); $negotiator->updateConfiguration($configurable); $negotiator->purgeConfiguration(); } else { // In language_entity_base_field_info_alter() we are altering view/form // display definitions to make language fields display configurable. Since // this is not a hard dependency, and thus is not detected by the config // system, we have to clean up the related values manually. foreach (['entity_view_display', 'entity_form_display'] as $key) { $displays = \Drupal::entityTypeManager()->getStorage($key)->loadMultiple(); /** @var \Drupal\Core\Entity\Display\EntityDisplayInterface $display */ foreach ($displays as $display) { $display->save(); } } } } /** * Implements hook_modules_uninstalled(). */ function language_modules_uninstalled($modules) { language_modules_installed($modules); } /** * Implements hook_preprocess_HOOK() for block templates. */ function language_preprocess_block(&$variables) { if ($variables['configuration']['provider'] == 'language') { $variables['attributes']['role'] = 'navigation'; } } /** * Returns language mappings between browser and Drupal language codes. * * @return array * An array containing browser language codes as keys with corresponding * Drupal language codes as values. */ function language_get_browser_drupal_langcode_mappings() { $config = \Drupal::config('language.mappings'); if ($config->isNew()) { return []; } return $config->get('map'); } /** * Implements hook_form_alter(). */ function language_form_alter(&$form, FormStateInterface $form_state) { // Content entity forms may have added a langcode field. But content language // configuration should decide if it should be exposed or not in the forms. $form_object = $form_state->getFormObject(); if ($form_object instanceof ContentEntityFormInterface && $form_object->getEntity()->getEntityType()->hasKey('langcode')) { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $form_object->getEntity(); $entity_type = $entity->getEntityType(); $langcode_key = $entity_type->getKey('langcode'); if (isset($form[$langcode_key]) && $form[$langcode_key]['#access'] !== FALSE) { $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle($entity->getEntityTypeId(), $entity->bundle()); $form[$langcode_key]['#access'] = $language_configuration->isLanguageAlterable(); } } } /** * Implements hook_field_info_alter(). */ function language_field_info_alter(&$info) { // Change the default behavior of language field. $info['language']['class'] = '\Drupal\language\DefaultLanguageItem'; } /** * Implements hook_entity_field_access() */ function language_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) { // Only allow edit access on a langcode field if the entity it is attached to // is configured to have an alterable language. Also without items we can not // decide whether or not to allow access. if ($items && $operation == 'edit') { // Check if we are dealing with a langcode field. $langcode_key = $items->getEntity()->getEntityType()->getKey('langcode'); if ($field_definition->getName() == $langcode_key) { // Grant access depending on whether the entity language can be altered. $entity = $items->getEntity(); $config = ContentLanguageSettings::loadByEntityTypeBundle($entity->getEntityTypeId(), $entity->bundle()); return AccessResult::forbiddenIf(!$config->isLanguageAlterable()); } } return AccessResult::neutral(); } /** * Implements hook_tour_tips_alter(). */ function language_tour_tips_alter(array &$tour_tips, EntityInterface $entity) { foreach ($tour_tips as $tour_tip) { if ($tour_tip->get('id') == 'language-overview') { $additional_overview = ''; if (Drupal::service('module_handler')->moduleExists('locale')) { $additional_overview = t("This page also provides an overview of how much of the site's interface has been translated for each configured language."); } else { $additional_overview = t("If the Interface Translation module is enabled, this page will provide an overview of how much of the site's interface has been translated for each configured language."); } $tour_tip->set('body', $tour_tip->get('body') . '' . $additional_overview . '
'); } elseif ($tour_tip->get('id') == 'language-continue') { $additional_continue = ''; $additional_modules = []; if (!Drupal::service('module_handler')->moduleExists('locale')) { $additional_modules[] = Drupal::service('module_handler')->getName('locale'); } if (!Drupal::service('module_handler')->moduleExists('content_translation')) { $additional_modules[] = Drupal::service('module_handler')->getName('content_translation'); } if (!empty($additional_modules)) { $additional_continue = t('Depending on your site features, additional modules that you might want to enable are:') . '' . $additional_continue . '
'); } } } } /** * Implements hook_language_types_info_alter(). * * We can't set the fixed properties in \Drupal\Core\Language\LanguageManager, * where the rest of the properties for the default language types are defined. * The LanguageNegation classes are only loaded when the language module is * enabled and we can't be sure of that in the LanguageManager. */ function language_language_types_info_alter(array &$language_types) { $language_types[LanguageInterface::TYPE_CONTENT]['fixed'] = [LanguageNegotiationUI::METHOD_ID]; $language_types[LanguageInterface::TYPE_URL]['fixed'] = [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationUrlFallback::METHOD_ID]; }