2

I installed the JavaScript Catalog for translating text in javascript files as explained in the docs: https://docs.djangoproject.com/en/3.2/topics/i18n/translation/#module-django.views.i18n

That works well. I can create the PO translation files and als make and compile the translations. For example:

gettext("my-translation")

will work just fine.

Now I want get an array from an API call which looks like this:

let arr = ["red", "green", "yellow", "blue"].

I don't know how I can use gettext to create the translations for each word in the array. I tried to map it with arr.map(i => gettext( + i + ")"));. I also tried to add it to the whole array with gettext(arr), but no success.

My websearches did not help me. I cannot change the representation of the array, so I need to do this in the frontend.

Is there any trick for this? Thanks for any help and hints.

Edit:

  • I tried arr.map(i => gettext( + i + ")"));. This adds the last ")" into the string so it does not work

  • I tried arr.map(i => gettext(i)); This returns the same array without adding the gettext

  • I tried arr.map(i => gettext() + i); This adds me an "undefined" to the string

I tried other combinations which I can't remember

shadow
  • 151
  • 2
  • 12
  • I am getting the array with an ajax request. There are no translations in the DB. The colors are choice fields in the model, wich I tried to translate, but whenever I always only get the unstranslated versions back – shadow Jun 11 '21 at 18:25
  • Is there a typo here? `arr.map(i => gettext( + i + ")"));` Why do you add ")" to the string passed to the `gettext`? – Marc Compte Jun 11 '21 at 19:21
  • Well I tried a couple of ways. I edited my question to describe what the outcome was – shadow Jun 11 '21 at 19:38
  • 1
    The second one `arr.map(i => gettext(i));` should be the good one. If you get the same input string then make sure you translated the colour names, compiled the .po/.mo files and reload the page where you load the javascript catalog. – Marc Compte Jun 11 '21 at 22:08
  • @Marc You are right. `arr.map(i => gettext(i));` indeed works if I add the color translations manually. What does not work is `compilemessages`, that's why I thought I am doing it wrong. But adding it manaually to the `PO` file and then compiling does the trick. Thank you very much for the valuable hint. That solved it :) – shadow Jun 14 '21 at 16:14

0 Answers0