Translating configuration

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.

Getting ready

Your Drupal site needs to have two languages enabled in order to use Configuration Translation. Install Danish from the Languages interface.

How to do it…

  1. Go to the Extend and install the Configuration Translation module. It will prompt you to enable the Interface Translation, Language, File, and Field modules to be installed as well if they are not.
  2. After the module is installed, go to the Configuration. Go to the Configuration translation page under the Regional and Language section.
  3. Click on the list for the Date format option in the configuration entity option table:
    How to do it…
  4. We will translate the Default long date format to represent the Danish format. Click on the Translate for the Default long date format row.
  5. Click on Add to create a Danish translation:
    How to do it…
  6. For Danish, we will provide the following PHP date format: l j. F, Y – H.i. This will display the day of the week, day of the month, the month, full year, and 24 hour notation for time.
  7. Click on Save translation.
  8. Whenever a user is browsing your Drupal site with Danish as their language, the date format will now be localized for their experience.

How it works…

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:

How it works…

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.

There's more…

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.

Altering configuration translation info definitions

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.

Translating views

Views are configuration entities. When the Configuration translation module is enabled, it is possible to translate Views. This will allow you to translate display titles, exposed form labels, and other items. Refer to the Creating a multilingual view recipe in this chapter for more information.

See also

  • In recipe Creating a Multilingual View of Chapter 8, Multilingual and Internationalization
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset