Boky
Boky

Reputation: 12084

How to translate .js files in Django and Wagtail

I'm using Wagtail and Django and I'm trying to translate the content in html templates and js files.

The content in html templates is added correctly to .po files but the strings from js files aren't added at all.

The structure of my project is as follows:

enter image description here

The urls.py file inside webDealers folder is as follows:

urlpatterns = [
    url(r'^django-admin/', admin.site.urls),

    url(r'^admin/', include(wagtailadmin_urls)),
    url(r'^documents/', include(wagtaildocs_urls)),

    url(r'^search/$', search_views.search, name='search'),
    url(r'^api/', include('API.urls')),

    url(r'^i18n/', include('django.conf.urls.i18n'), name='set_language'),
    url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),

    url(r'', include(wagtail_urls)),
]

The settings.py is as follows:

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)

LANGUAGE_CODE = 'en'    
TIME_ZONE = 'UTC'    
USE_I18N = True    
USE_L10N = True    
USE_TZ = True

LANGUAGES = [('en', 'English'), ('fr', 'French'), ('nl', 'Dutch')]

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

All .js files are in static folder inside webDealers folder.

I've added <script src="{% url 'javascript-catalog' %}"></script> to the <head> tag and I've added gettext("String to translate") in .js file where the string needs to be translated.

But when I run django-admin makemessages -l en inside locale folder I get only translations from html files and not from js files.

And I'm using Django==2.1.2

Any advice how to translate strings in js files?

UPDATE

When I run

django-admin makemessages -d django -l nl

it gets all strings from html files and put them inside locale folder in root folder (root/locale).

But when I do

django-admin makemessages -d djangojs -l nl

it gets all .js strings and put them inside locale folder in root/webDealers/locale.

I want to have all translations in one place

Upvotes: 2

Views: 432

Answers (1)

gasman
gasman

Reputation: 25292

When calling makemessages, you need to pass --domain js or --extension js to pick up translatable strings in .js files. See the Django documentation:

https://docs.djangoproject.com/en/2.1/ref/django-admin/#django-admin-makemessages https://docs.djangoproject.com/en/2.1/topics/i18n/translation/#message-files

Upvotes: 1

Related Questions