As mention in documentation, i have followed below steps -
In my settings.py
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGE_CODE = 'en-us'
USE_I18N = True
USE_L10N = True
ugettext = lambda s: s
LANGUAGES = (
('en', ugettext('English')),
('mar', ugettext('Marathi')),
)
My locale directory is in my Django project's root folder
In urls.py
from django.views.i18n import javascript_catalog
js_info_dict = {
'packages': ('phone',),
}
urlpatterns = [
url(r'^phone/', include('phone.urls')),
url(r'^jsi18n/$', javascript_catalog, js_info_dict),
]
In base.html
<script type="text/javascript" src="/jsi18n/"></script>
I am able to see translation done by trans tag, but when m trying to translate variables in javascript using gettext method m getting this particular error
ReferenceError: gettext is not defined
FYI - djangojs.po and djangojs.mo files are there in my locale directory and i have compiled the file after putting translations on.
I tried hard on google but still same error.
After trying hard over a week i got the solution for my problem, which is bit strange for me
i had to re-arrange my url in urls.py as shown below
urlpatterns = [
url(r'^jsi18n/$', javascript_catalog, js_info_dict),
url(r'^phone/', include('phone.urls')),
]
as soon as i made above change my JS catalog file got loaded and translation in js file started working.
i did not understand the cause exactly, but would like to share.. might be useful..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With