Add multilingual support to Drupal 8 Hello World module Today we already can upgrade contrib projects to Drupal 8 (#D8CX) and there are more and more “Hello World” code examples for Drupal 8. Some of them:


Drupal 8: Writing a Hello World Module
Drupal 8: Hello OOP, Hello world!
Drupal 8: Hello, Configuration Management
Drupal 8 module development #3 - adding a settings page - revision
Drupal 8 isn't Scary, Pt. 1: Introduction &amp; Creating a Hello World Module


Can we add multilingual support to such simple modules?
Drupal 8 has many improvements for interface, configuration and content translation. There are great series of articles from Gábor Hojtsy: Multilingual Drupal 8.

Let's make Hello World module settings from this article translatable. 
Full module code is avalible here: https://github.com/kalabro/drupal-examples/tree/master/hello
This code was tested with core 8.x dev branch (ed8dfae 2014-05-19).

1. Preparing Drupal 8 installation


Enable “Language”, “Interface Translation” and “Configuration Translation” modules.
Configure multiple languages.
Place “Language switcher” block.
Check how “Configuration Translation” with Site information form.




2. “Hello World” module


Use Drupal 8: Hello, Configuration Management article as base for “Hello World” module.
This module provides static /hello page with hardcoded “'Hello, World!'” message in it. The message can be outputted in normal or upper case. There is a setting for message case:

Play with the code until it becomes yours.
hello.info.yml:

name: Hello
description: '"Hello World" module with config translation support.'
core: 8.x 
type: module
configure: hello.settings_form



3. More complex settings


Let's make message on /hello page configurable (dynamic).
For this purpose we should update form and controller.


Add 'hello_message' textfield in 'buildForm' method:  

$form['hello_message'] = array(
  '#type' =&gt; 'textfield',
  '#title' =&gt; t('Your "Hello World!" message'),
  '#default_value' =&gt; $config-&gt;get('hello_message'),
  '#required' =&gt; TRUE,
);

Update HelloController to take hello message from setting instead of hardcoding it:  

$hello_message = $config-&gt;get('hello_message');


Check full code in repository: HelloConfigForm.php, HelloController.php and etc. Feel free to ask any questions.


4. Make settings translatable

Our setting 'Your "Hello World!" message' should be translatable.


First of all we have to add config schema. It is recommended to add schema for any modules with configuration.
hello.schema.yml:

hello.settings:
  type: mapping
  label: 'Hello World settings'
  mapping:
    case:
      type: integer
      label: 'Letter case of your "Hello World!" message'
      # translatable: true
    hello_message:
      type: text # translatable by default.
      label: 'Your "Hello World!" message'



Now Drupal knows what kind of data is stored in module config. Some of config data types are translatable by default (text, label, mail). You can also add translatable: true to settings manually (try to do it for “case” setting).


Add Configuration Translation support.
hello.config_translation.yml:

  hello:
    title: 'Hello World settings'
    base_route_name: hello.settings_form
    names:
      - 'hello.settings'



Just small YAML file and that's all.



To see “Translate settings” tab you need existing default local task (See hello.local_tasks.yml for an example. Note: because of a change in Drupal core, this file name is no longer correct. It should be hello.links.task.yml now.).





Only translatable settings are appeared.



P.S.: In Drupal 7 i18n_variable API is used for this task.