Enabling translation for module configurationfor Drupal 8 , 9 , 10 , and 11

Last updated :  

Introduction

Building on our previous lessons where we made strings translatable using t() and created .po files, it's time to expand our module's multilingual capabilities. In this tutorial, we'll focus on enabling translation for module configurations, ensuring a thorough localization process.

Why Translate Module Configuration?

While translating static strings is important, Drupal's modules often contain configuration settings that site administrators will need to adjust. Translations help ensure these configurations remain accessible in all supported languages, allowing site administrators and users to comprehend and update settings without linguistic barriers.

Prerequisites

Before we begin, ensure that you've enabled necessary Drupal multilingual modules like Configuration Translation, Interface Translation, and Language. These modules form the foundation for managing multilingual content and configurations.

Making Configuration Translatable

To make your module's configuration translatable, you need to modify the hook_schema implementation or the YAML file associated with your configuration. Here’s how you can do it:

Step 1: Define Your Configuration Schema

First, determine the configurations that you want to be translatable. Let's assume we're working with a configuration that manages a welcome message:


my_module.settings:
  type: config_object
  label: 'My Module settings'
  mapping:
    welcome_message:
      type: string
      label: 'Welcome message'
      translatable: true

In this YAML file, the key translatable: true under the welcome_message indicates that this configuration item should be translatable.

Step 2: Implement the Schema

Ensure your module's schema is implemented correctly in a file located at my_module/config/schema/my_module.schema.yml.

Step 3: Add Configuration to Your Module

Within your module, you will manage the configurations using Config API. Here is an example:


use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class MyModuleSettingsForm extends ConfigFormBase {

  public function getFormId() {
    return 'my_module_settings_form';
  }

  protected function getEditableConfigNames() {
    return ['my_module.settings'];
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('my_module.settings');

    $form['welcome_message'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Welcome message'),
      '#default_value' => $config->get('welcome_message'),
    ];

    return parent::buildForm($form, $form_state);
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('my_module.settings')
      ->set('welcome_message', $form_state->getValue('welcome_message'))
      ->save();
    parent::submitForm($form, $form_state);
  }
}

This form makes use of the Configuration API to store and retrieve the configuration. Notice how we use $this->t() when creating the form elements to ensure the text is translated.

Testing Configuration Translation

  • Access the configuration page of your module under Configuration > My Module Settings.
  • Set a value for the welcome message.
  • Navigate to Configuration > Regional and language > Configuration translation.
  • Locate and translate the configuration items into the desired languages.

When complete, the configuration will display the translated message based on selected language.

Conclusion

Multilingual configurations ensure that your module can truly operate globally. By enabling translation on configuration settings, you broaden your module's accessibility and efficacy, making it a powerful addition to any multilingual website.

What's Next?

We will continue our journey through Drupal module development by exploring how to handle language-specific settings within your custom modules, such as language fallback strategies and specific language setups. This will further enhance your module's multilingual functionality!