Login

Tag "locale"

Snippet List

LocaleMiddleware without browser language discovery

This snippet holds your Django project from automatically changing language of the page to the best fitting one by discovering the client browser language. I personally needed to show the page to the user for the first time in the default language (English), although there were some translations. User can still change the language (via session cookies). Insert this middleware BEFORE the Django's `django.middleware.locale.LocaleMiddleware` in settings.

  • middleware
  • i18n
  • locale
Read More

Multilingual site based on domain - not accept header and django_session

On our site [Fornebuklinikken - A cosmetic surgeon in Norway](http://www.fornebuklinikken.no) we also have a domain [http://fornebuklinikken.com](http://www.fornebuklinikken.no) which should be using the 'en' language. We didn't wan't to use the standard locale lib, and wrote our own middleware which lookups the correct language corresponding to the domain (.no or .com) Any questions? Contact me on herman.schistad (at) gmail.com

  • internationalization
  • middleware
  • multilingual
  • locale
  • domain
  • localeurl
Read More

Modeli18n

This is a Model base class used to support internationalization (i18n) for your models. This code extends the Django's Model class so you can use all available options from Model safely. Basicly, it uses introspection to create a sub-model to your model to hold translation. **Features:** 1. Simplicity of use. You simply extend your model with this class and add all the fields that needs to be translated are placed under the `locale` sub-class; 2. The code uses the `django.utils.translation.get_language()` to select the current language; 3. You can use `python ./manage.py syncdb` command safely; 4. Force the user to enter a translation for each language even if the fields can be blank. This makes sure that all objects are returned safely. **Ordering by locale fields:** To sort on translated fields, use the form of "model_i18n__transfieldname" (see code for example). **Limitation:** Do not use localized fields in __unicode__, the admin will throw an exception when you'll add a new item and do "save and continue". Just drop a comment if you need more information. (last update: 06/15/2010)

  • internationalization
  • i18n
  • model
  • locale
  • translation
  • culture
Read More

Changing the locale temporary

This piece of code allows the quickly set the locale to a different language. This can be used for instance in cron jobs to send emails to users in their specific language. This is pretty rough code, it be better to create an object, having two functions: set_locale and reset_locale. set_locale would than contain steps 1 to 4 (and return the request object on succes), reset_locale would be step 6 Enjoy!

  • django
  • locale
Read More

Newforms field for decimals with a comma

This might be handy in countries where decimals are entered with a comma separating the decimal places from the integer part (for instance in Germany). It lets user enter and displays all decimals with a comma separator. I ran into this problem and couldn't find a clean internationalized way of doing it... but newforms makes it so easy to roll your own. Hope it helps someone.

  • newforms
  • locale
  • comma
  • decimal
Read More

BabelMiddleware

Originally posted on [skam.webfactional.com](http://skam.webfactional.com/blog/2007/07/16/babel-integration-django/) This is a very simple middleware that uses babel (http://babel.edgewall.org) for accessing locale data in request objects through request.LOCALE attribute. It also provides a function to get locale data outside views. settings.py: MIDDLEWARE_CLASSES = ( ... cut ... 'django.middleware.locale.LocaleMiddleware', 'middleware.locale.BabelMiddleware', ... cut ... )

  • middleware
  • i18n
  • l10n
  • locale
  • babel
Read More

Ignore HTTP Accept-Language headers

A little tiny middleware that, when used in multilingual sites, will make Django I18N ignore any `Accept-Language` headers in the request, thus ensuring that every first-time visitor (with no explicit language preference set via session or cookie) will see the site in the language specified by `settings.LANGUAGE_CODE`. (Please note that I think that overriding user preferences is generally a bad practice, but I had my reasons to use it :) )

  • middleware
  • i18n
  • l10n
  • locale
Read More

8 snippets posted so far.