7

I followed the Django doc to internalize the js files, but it does not work. Here's my setup:

settings.py:

LOCALE_PATHS = (os.path.join(BASE_DIR, 'locale'),)

urls.py in root project:

from django.views.i18n import JavaScriptCatalog
from django.conf.urls.i18n import i18n_patterns

urlpatterns += i18n_patterns(
    path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
)

Running the following commands inside the folder and the .po and .mo files were generated:

django-admin makemessages -l pt_BR

django-admin makemessages -d djangojs -l pt_BR

django-admin makemessages -a

django-admin compilemessages

django.po file:

msgid "Customer"
msgstr "Cliente"

In the html template use as follows:

<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>

console.log( gettext('Customer') );

Nothing happens, the text continues in English. Does anyone know what can it be?

Dev Python
  • 175
  • 2
  • 11
  • Are you sure your current language is not english? – Jieter Sep 24 '18 at 07:13
  • Hello! I have `LANGUAGE_CODE = 'pt-br' ` in my settings.py. I deleted the .po and .mo files, recreated these files, included the translation and compiled. Even though the translation does not work, the output of the text continues in English. – Dev Python Sep 24 '18 at 16:48
  • Now I can show the translation in the html template, but the output of the javascript remains in English. I put the path this way. `LOCALE_PATHS = [os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'locale')] ` – Dev Python Sep 24 '18 at 21:06
  • Or using the default path and each app will need to have a locale folder. `LOCALE_PATHS = [os.path.join(os.path.dirname(__file__), "locale")] ` – Dev Python Sep 24 '18 at 21:15
  • Same problem there, and still investigating – VivienG Aug 09 '19 at 11:24
  • is this fixed in django 3? still having this problem in django 2.2.7 – Ebrahim Karimi Mar 24 '20 at 00:15
  • Still the same issue with Django 3.2.x. Have spent a lot of time with it, to no avail. – olaf Jul 01 '22 at 00:51

1 Answers1

0

OK, I think I have resolved this issue. For some reason (feature?), Django compiles the JS translations into [project-folder]/locale and not [your-app]/locale. So, to make it work, copy the djangojs.mo file over to [your-app]/locale and see if it works for you. Did the job for me.

I've tried various parameters in urls.py and elsewhere but I cannot make Django compile the trans file right where it belongs.

olaf
  • 151
  • 1
  • 1
  • 8