0

I'm developing a multilingual Django website. It has two languages, English and Hebrew. I want the default language for every first-time visitor to be Hebrew, regardless of what his browser's Accept-Language is.

Of course, if he changes language to English (and thus gets the language cookie or the key in the session), it should remain at English.

I think I can program this algorithm myself, but where do I "plug it in"? How do I make my project use it?

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

2 Answers2

0

Start by reading this: http://docs.djangoproject.com/en/1.1/topics/i18n/#topics-i18n

Then read this: http://docs.djangoproject.com/en/1.1/topics/i18n/internationalization/#topics-i18n-internationalization

Each RequestContext has access to three translation-specific variables:

LANGUAGES is a list of tuples in which the first element is the language code and the second is the language name (translated into the currently active locale).

LANGUAGE_CODE is the current user's preferred language, as a string. Example: en-us. (See How Django discovers language preference.)

LANGUAGE_BIDI is the current locale's direction. If True, it's a right-to-left language, e.g.: Hebrew, Arabic. If False it's a left-to-right language, e.g.: English, French, German etc.

If you don't use the RequestContext extension, you can get those values with three tags:

Is this what you're asking about?

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • I read your link before, and also this one: http://docs.djangoproject.com/en/1.1/topics/i18n/deployment/ But I didn't see anything about how to customize the algorithm. – Ram Rachum Feb 25 '10 at 14:22
  • @cool-RR: "customize the algorithm"? Do you mean override the settings found in the Request? Are you asking about how to use the "Each RequestContext has access to three translation-specific variables"? – S.Lott Feb 25 '10 at 14:27
  • No. I'm talking about the algorithm by which Django determines the user's language. Check out the function `django.utils.translation.get_language_from_request`. I want to override it somehow. – Ram Rachum Feb 25 '10 at 14:31
  • @cool-RR: Actually, I don't think you do. I think you want to customize the language in which you reply, based on the "get_language_from_request" plus your application-specific settings. – S.Lott Feb 25 '10 at 14:39
  • I'm not seeing how your idea would work. HOW am I supposed to do this? – Ram Rachum Feb 25 '10 at 14:48
  • @cool-RR: Your view function provides the language settings to the template for rendering. Your view function looks at session state, user profile and get_language_from_request to decide which language setting to provide to the template. – S.Lott Feb 25 '10 at 14:54
0

Maybe you don't have to override anything. You can just check on the first page, (or maybe every page) if the user already has a language cookie and otherwise redirect him to the set_language redirect view. In that way you could force the language to Hebrew.

If the user decides to change back to English, he can do it very easily.

HWM-Rocker
  • 587
  • 2
  • 8
  • 17
  • This inspired me to create a little middleware that did exactly what you said. (I did it as a middleware so it will automatically apply to all pages.) – Ram Rachum Feb 25 '10 at 22:01