The Configuration translation module provides an interface for translating configurations with Interface translation and Language as dependencies. This module allows you to translate configuration entities. The ability to translate configuration entities adds an extra level of internationalization.
Interface translation allows you to translate strings provided in your Drupal site's code base. Configuration translation allows you to translate importable and exportable configuration items that you have created, such as your site title or date formats.
In this recipe, we will translate date format configuration entities. We will provide localized date formats for Danish to provide a more internationalized experience.
Your Drupal site needs to have two languages enabled in order to use Configuration Translation. Install Danish from the Languages interface.
The Configuration translation module requires Interface translation; however, it does not work in the same fashion. The module modifies all entity types that extend the DrupalCoreConfigEntityConfigEntityInterface
interface. It adds a new handler under the config_translation_list
key. This is used to build a list of available configuration entities and their bundles.
The module alters the configuration schema in Drupal and updates the default configuration element definitions to use a specified class under Drupalconfig_translationForm
. This allows Drupalconfig_translationFormConfigTranslationFormBase
and its child classes proper saved translated configuration data that can be modified through the configuration translation screens.
When the configuration is saved, it is identified as being part of a collection. The collection is identified as language.LANGCODE
and all translated configuration entities are saved and loaded by this identifier. Here is an example of how the configuration items are stored in the database:
When browsing the site in the es
language code, the appropriate block.block.bartik_account_menu
configuration entity will be loaded. If you are using the default site, or no language code, the configuration entity with an empty collection will be used.
Configuration entities and the ability to translate them are a big part of Drupal 8's multilingual capabilities. We'll explore them in detail in the next recipe.
Modules have the ability to invoke the hook_config_translation_info_alter
hook to alter discovered configuration mappers. For instance, the Node
module does this to modify the node_type
configuration entity:
/** * Implements hook_config_translation_info_alter(). */ function node_config_translation_info_alter(&$info) { $info['node_type']['class'] = 'Drupal odeConfigTranslationNodeTypeMapper'; }
This updates the node_type
definition to use the Drupal
odeConfigTranslationNodeTypeMapper
custom mapper class. This class adds the node type's title as a configurable translation item.