0

This question already has an answer here:

I've recently started working on a custom D8 module. I've used $this->t() (or t()) wherever I had to output a text so that this text would then be translatable.

My module works correctly, but when I visit Drupal's "User interface translation" (admin/config/regional/translate) page, those strings are nowhere to be found.

Two examples:

_1. in the mymodule.module file, I have implemented the hook_help() function to display a help page in the admin:

<?php

/**
 * Implements hook_help().
 */
function mymodule_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.mymodule':
      return t('<p>Here is the (much longer) help text.</p>');
  }
}

?>

The help page is visible in the admin and shows the text; however this text cannot be found in the "User interface translation" page.

_2. the module has a configuration page in the admin, with several fields and a description for each field. This configuration form is defined in src/Form/mymoduleForm.php:

<?php

namespace Drupal\mymodule\Form;

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

class mymoduleForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    // Form constructor.
    $form = parent::buildForm($form, $form_state);
    $config = $this->config('mymodule.settings');
    $form['field1'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('The first field:'),
      '#default_value' => $config->get('mymodule.field1'),
      '#description' => $this->t('<p>Description for this field.</p>'),
      '#required' => TRUE,
    );
    // and so on
  }
  // other code
}
?>

Here again, the configuration page works, all the fields are visible, but none of the strings in the code above appear on the "User interface translation" page (neither the title of the string, nor the description).

The same goes for strings declared in the *.yml files: they're supposed to be picked up automatically by Drupal, but I don't seen any of them in the interface... (e.g. the module description in the *.info.yml file.)

What am I missing?

marked as duplicate by leymannx, Community Oct 23 '18 at 13:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Visit the page the source text is on, then run cron a few times, clear cache a few times, make a cup of tea - that usually works for me – Clive Oct 22 '18 at 10:07
  • 1
    I'm not sure if it's the same in D8 but in D7 you had to visit the page where the string is but while (interface) language in other than English/default. That triggers Drupal to become aware of the new strings and make them available for translation. – prkos Oct 22 '18 at 10:57
  • Thank you both. Running cron or clearing the cache didn't really help (I've done it numerous times before), but visiting the pages in the target language seems to do the trick. It seems a bit inconvenient when trying to localize a module (especially since the "hidden" texts such as the error messages are still unavailable, so I guess I will have to trigger each one of them before I can translate them), but at least I'm glad there's (apparently) nothing wrong with my code. – s427 Oct 22 '18 at 15:44
0

As suggested by @prkos in the comments, visiting the pages (both public and admin) in the target language caused the corresponding strings to be discovered by Drupal and therefore become available in the interface translation. For each string, you have to visit the corresponding page (or block).

If you have custom error or confirmation messages, you apparently have to trigger them before they appear too. (? not entirely sure)

(I thought the "updating translations" progress bar that you see during the module installation was precisely Drupal listing all translations available in the module, but apparently that's not the case.)

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