开发者

Django: get_language in settings.py

I implemented the user login/registration using Django's authentication syst开发者_运维问答em but hit the wall and hopefully someone can help me.

This website is using django-localeurl and is presently running in 3 languages.

I'm having problems passing the login redirect to the right language. What I want to do is pass the current language to the LOGIN_REDIRECT_URL variable (in settings.py), so that instead of having:

LOGIN_REDIRECT_URL = '/accounts/my_account/'

I'd have something like:

LOGIN_REDIRECT_URL = '/%s/accounts/my_account/' % request.LANGUAGE_CODE

which of course doesn't work because I'm not passing requests to settings.py.

Is there a really really easy and smart way to do this? It always defaults to English and that's a big problem. If a user is viewing the site in Spanish, once they login they're redirected to English :/

I have languages setup like this:

gettext = lambda s: s
LANGUAGES = (
    ('pt', gettext('Portuguese')),
    ('es', gettext('Spanish')),
    ('en', gettext('English')),
)

and

LANGUAGE_CODE = 'en'

because the admin needs to be in English.

Can anyone help?

Thanks!


For my own projects of that kind, I use my own login_redirect decorator instead of the one provided by Django, which always sent me to the static url defined in settings.py. My i18n decorator looks like that:

def i18n_login_required(function):
    def wrap(request, *args, **kwargs):
        if not request.user.is_authenticated():
            return HttpResponseRedirect('/%s/accounts/login/?next=%s' % (get_language(), request.GET.get('next', urlquote(request.get_full_path()))))
        return function(request, *args, **kwargs)
    wrap.__doc__=function.__doc__
    wrap.__name__=function.__name__
    return wrap


I guess it wont work the way you are trying to solve it. I would set up view that is called on your the login redirect and inside this view determine the language settings etc and redirect again to the correct view + the correct locale settings!


for translation i use : http://code.google.com/p/django-transmeta/ and it works perfect for me with 3 languages and 14 location. check it out, might lead you somewhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜