Translating words in array in javascript file in django

191 views Asked by At

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

0

There are 0 answers