开发者

Getting the two letter language code of the client

This is probably a very common question but after a lot of Googling, I'm still yet to find a useful answer.

My site is only in English but I like to keep l开发者_如何学Pythonogs of the user's sessions and I need to get the client's language. I've read that I can get the the language information from the HTTP_ACCEPT_LANGUAGE header which is a comma-separated list of the client's languages in a descending order of priority i.e. the first one is the primary language.

I'd like to get the two-letter language code of the client's language in Django. e.g.

en-gb, en-us                       en
fr-fr, en-au                       fr
zh, en-us; q=0.8, en; q=0.6       zh

How can I do this? Do I have to parse the header myself or does Django have a inbuilt list of languages?

Is there detailed list of all the language codes?

Thanks.


Use translation.get_language_from_request :

from django.utils import translation

def view(request):

    client_lang = translation.get_language_from_request(request)

By the way why you want this ? did you check django support for i18n and l10n, because if you have the LocaleMiddleware enable you can also get the language using request.LANGUAGE_CODE.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜