3

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:

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

Can we add multilingual support to such simple modules?

7
  • Hello. This looks like a feature request to numerous blogs and manuals (and none of them have an issue queue at drupal.org). This is not the best place for feature request to 3rd party websites. If you have practical, answerable questions based on actual problem, please edit. – Mołot Oct 7 '13 at 7:29
  • @Mołot, I don't mind about someone's blogs and don't create feature requests. I just read it or not. – kalabro Oct 7 '13 at 9:06
  • @Mołot, question is practical, because it's about important Drupal problem. Many Drupal 7 contribs just haven't multilingual support or use multilingual API incorrectly. I think we should talk and ask about i18n in Drupal 8 as early as possible. I also created very practical answer with code which can be used as an example when you upgrade your modules from 7 to 8. If you have ideas how to edit question/answer you are welcome. – kalabro Oct 7 '13 at 9:11
  • 1
    Adding multilingual to one given piece of code would be a practical and answerable problem. Adding it to a blog post made by some guy who probably will not give us his password is another ;) I'm not saying what you want is wrong, just the way you stated it. And there are 3 close votes now, so I guess I'm not the only one to think that. – Mołot Oct 7 '13 at 9:19
  • @Mołot, “Adding multilingual to one given piece of code” — Hello World isn't one given piece of code? If you check links above, you can see that Hello World modules are all the same. – kalabro Oct 7 '13 at 9:35
9

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.

enter image description here

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: enter image description here
  • 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' => 'textfield',
        '#title' => t('Your "Hello World!" message'),
        '#default_value' => $config->get('hello_message'),
        '#required' => TRUE,
      );
      
    • Update HelloController to take hello message from setting instead of hardcoding it:

      $hello_message = $config->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.

enter image description here

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.).

enter image description here

enter image description here

Only translatable settings are appeared.

enter image description here

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

1
  • Almost all works fine, but I had use "config_object" instead of "mapping" – Eugene Mar 31 '16 at 18:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.