Django javascript traslation directory file not loading showing error - ReferenceError: gettext not defined

As mentioned in the documentation, I followed these 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 the root folder of the Django project

  • 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 can see the translation made by the trans tag, but when m tries to translate the variables into javascript using the gettext m method, getting this specific error

    ReferenceError: gettext is not defined

The FYI files - djangojs.po and djangojs.mo are in my locale directory, and I compiled the file after entering the translations.

I tried hard on google, but still the same error, please help me here.

+4
source share
1 answer

,

url urls.py,

urlpatterns = [
 url(r'^jsi18n/$', javascript_catalog, js_info_dict),
 url(r'^phone/', include('phone.urls')),
] 

, JS, js .

, .. ..

+2

Source: https://habr.com/ru/post/1625296/


All Articles