1

I have a custom admin form validation js code in separate js file.

i want to render the appropriate js file, meaning if LANGUAGE_CODE is 'es' , the js file should end with myjsfile_es.js

I did:

class BlogForm(forms.ModelForm):
   class Media:
       js = ('js/custom_%s.js' % get_language(),)

the actual page's url is:

http://127.0.0.1:8000/es/admin/blog/blog/add/

but get_language is giving 'en' so I end up having wrong js file custom_en.js for es page,

en is default language in settings.py.

how can I get the active language in admin.py?

doniyor
  • 36,596
  • 57
  • 175
  • 260

2 Answers2

1

Maybe this is because django get's your language from the browser settings. I had same issue in one of my projects, and solve it by using that middleware:

# -*- coding: utf-8 -*-


class IgnoreAcceptLanguageMiddleware(object):
   """
   Ignore Accept-Language HTTP headers

   """

    def process_request(self, request):
        if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
            del request.META['HTTP_ACCEPT_LANGUAGE']
kpacn
  • 257
  • 2
  • 8
  • why are you deleting ``HTTP_ACCEPT_LANGUAGE``, I need it in other places, I just need an access to request object in admin.py – doniyor Oct 30 '14 at 14:35
  • 1
    Sorry this is another issue, misunderstand you question. There is a lot of answers for your problem:http://stackoverflow.com/questions/2683689/django-access-request-object-from-admins-form-clean – kpacn Oct 30 '14 at 14:56
-1

you can get the current language from django.utils.translation.get_language()

Check documentation:

https://docs.djangoproject.com/en/1.6/topics/i18n/translation/#using-translations-outside-views-and-templates

Naveed
  • 19
  • 5
  • please read my question, this is exactly what is not working for me – doniyor Oct 30 '14 at 13:38
  • That's weird behavior of django. There are two additional methods regarding get_language. May be they help 'get_language_from_path', 'get_language_from_request', – Naveed Oct 30 '14 at 13:43
  • yeah, but the other two methods need a path or request object which i dont know how to get in admin.py – doniyor Oct 30 '14 at 13:45