Drupal translation javascript function Drupal.t does not work

Created on 10 January 2024, 5 months ago

I have a custom trad file *.po

- mybestmodule.info.yml

interface translation project: mybestmodule
interface translation server pattern: modules/mybestmodule/translations/mybestmodule.%language.po


- translations/mybestmodule.fr.po

msgid ""
msgstr ""
"Project-Id-Version: mybestmodule(1.0.0)\n"
"POT-Creation-Date: 2024-01-03 09:50+0200\n"
"PO-Revision-Date: 2024-01-03 09:50+0200\n"
"Language-Team: French\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n!=1);\n"

msgid "mybestmodule.greeting"
msgstr "salut"


When I do the translation on the controller side via the

$this->t('mybestmodule.greeting')

or

t('mybestmodule.greeting')

function.

=> it works correctly.

the same, at the branch level, if I do

{{ 'mybestmodule.greeting'|t }}

or

{{ 'mybestmodule.greeting'|trans }}

=> it works very well.

on the other hand, when I use it at the javascript level in library with dependencies : core/drupal, core/drupalSettings, core/once, core/jquery

(function ($, Drupal, once) {
  Drupal.behaviors.myBestModule = {
    attach: function (context, settings) {
      once('myBestModule', 'body', context).forEach(function () {
        console.log(Drupal.t('mybestmodule.greeting'));
      });
    }
  };
})(jQuery, Drupal, once);

=> it does not work. it does not translate the word (returns mybestmodule.greeting). and when I check in the global js variable of window.drupalTranslations , it is not there.

I can see the word clearly from the translation BO /admin/config/regional/translate

I did several times drush locale-check && drush locale-update && drush cr

but yet it doesn't work; and that only on the JS part.

Any ideas?

THANKS

๐Ÿ’ฌ Support request
Status

Active

Version

10.1 โœจ

Component
Localeย  โ†’

Last updated 4 days ago

Created by

๐Ÿ‡ซ๐Ÿ‡ทFrance HasinaNjaratin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024