Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django javascript translation catalog file is failing to load, showing error - ReferenceError: gettext is not defined

As mention in documentation, i have followed below steps -

  1. 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')),
    )
    
  2. My locale directory is in my Django project's root folder

  3. 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),
    ]   
    
  4. 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.

like image 352
Vee Avatar asked Sep 25 '22 15:09

Vee


1 Answers

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..

like image 92
Vee Avatar answered Sep 28 '22 06:09

Vee