5

I'm using Umami profile and choose Spanish is default language. Almost of the interface has been translated into Spanish. But some places did not translate like user page.

enter image description here

I'm going to admin/config/regional/translate to check. But This text already translated, but UI still show English text.

enter image description here I try debug to find the reason. I was break point at line 340 of line drupal/core/modules/views/src/Plugin/views/field/BulkForm.php and found that $action->label() return English string.

enter image description here

Because at line 116 $this->actionStorage->loadMultiple() return all actions. In this some actions return English string, some actions return Spanish string (example install module pathauto, this action return Spanish). All actions when install profile will using English text and module install after will be translate.

enter image description here

I don't know why. And How to can translate label of actions?

2 Answers 2

5

To translate action labels install the core module Actions and go to /admin/config/regional/config-translation/action.

Actions are config entities, which you can translate in an exported config as well, if you don't want to install the UI option.

4
  • Thank you for support. Do you why all core config entities did not translate? But config entities from other modules (install after default) will be translated.
    – Jonh
    Mar 25, 2019 at 16:12
  • When the config entities are created on installation they are parsed through user interface translation. There might be a timing issue downloading translations and creating those entities. But I think installing the Actions module is the way to go if you want to correct this after installation.
    – 4uk4
    Mar 25, 2019 at 17:32
  • It appears that you can uninstall the Actions module after doing the translations. At least, I've not run into any bugs so far, and the translations are still there. I'll come back if I discover otherwise.
    – Jaypan
    Jun 30, 2021 at 21:16
  • Yes, Action configuration is part of the system module and is not affected by uninstalling the Actions UI module.
    – 4uk4
    Jul 1, 2021 at 6:39
1

You can translate them easily in hook_form_alter().

function YOURMODULENAME_form_alter(&$form, $form_state, $form_id) {
  if ($form_id ='views_exposed_form' && isset($form['header']['node_bulk_form']['action']['#options'])) {
    foreach ($form['header']['node_bulk_form']['action']['#options'] as $key => $option) {
      $form['header']['node_bulk_form']['action']['#options'][$key] = t($option);
    }
  }
}
15
  • 2
    The "You should never use t() to translate variables" doesn't count anymore for D8? api.drupal.org/api/drupal/includes%21bootstrap.inc/function/t/…
    – leymannx
    Mar 22, 2019 at 21:02
  • @leymannx I've tested it before and works.
    – Yuseferi
    Mar 22, 2019 at 21:14
  • 1
    Variables should not be passed through the t() function. It may work, but it can open up security vulnerabilities.
    – Jaypan
    Mar 29, 2019 at 2:54
  • 1
    From the documentation: It is especially important never to call new TranslatableMarkup($user_text) or t($user_text) where $user_text is some text that a user entered -- doing that can lead to cross-site scripting and other security problems. api.drupal.org/api/drupal/…
    – Jaypan
    Jul 9, 2019 at 22:41
  • 1
    Well, the security reason is only one reason not to use variables in the t() function. See the documentation page I linked to above. If you want to pass variables through t(), that's your choice, but it's considered a bad practice.
    – Jaypan
    Jul 11, 2019 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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