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

Comments

HasinaNjaratin created an issue. See original summary.

cilefen’s picture

Category: Bug report » Support request
Priority: Major » Normal
Issue tags: -translation

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

vasiliyrepin’s picture

It helps to me:

drush php:cli
_locale_rebuild_js('YOUR_TWO_CHAR_LANG_CODE_HERE');
exit;
drush cr

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

quietone’s picture

Status: Active » Fixed

Two suggestions have been offered here on this support request. I hope one of those worked!

I am closing this per the guidance in Handle or refer a support request in an issue.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.