0

We're developing an application which will serve multiple industries. I'm hoping to use django's i18n functionality to accomplish two things:

  1. Translation to different languages (standard).
  2. Translation to different industries (much less standard).

Lets say the application serves vets and car mechanics, you would end up with a matrix of options:

              | English | French
----------------------------------
Vets          | horse   | cheval 
----------------------------------
Car Mechanics | car     | voiture

I guess I can setup the message files for different contexts pretty easily:

python manage.py makemessages -l fr_vet
etc...

Now how would I go about activating that translation?

I know the industry in middlewhere from the request, could I subclass django.middleware.locale.LocaleMiddleware and alter it, or do I need to subclass django.utils.translation and alter the activate function? Or something completely different?

Appologies if I've missed an existing explanation of how to do this - it was a classic case of "I'm sure the answer must exist but without knowing what it's called I can't google it".

SColvin
  • 11,584
  • 6
  • 57
  • 71

2 Answers2

0

I had the same situation once and I read almost all Django documentation about translations (it's really huge)... I would like to share with you some important links that can help:

Switch language in templates: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#switching-language-in-templates

This one, I think is the most important one for you, "How django discovers language..." https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-language-preference

One of the things that I think you are missing are the languages in your settings file, for example:

LANGUAGES = (
   ('de', _('German')),
   ('en', _('English')),
)
Lara
  • 2,170
  • 6
  • 22
  • 43
  • Thanks a lot, I'll have a look at that and see how far I get. Can I misuse the second part of language names (eg. "VET" from "en_VET") to identify different translations? – SColvin Aug 07 '14 at 21:59
  • Thanks, I'll have a check and confirm the answer once I've got it working. – SColvin Aug 07 '14 at 22:04
0

Lara's answer was useful, but didn't explain it fully, I thought it worthwhile to add a full explanation.

All that's required is to add middleware which sets the translation code for the industry:

    from django.middleware.locale import LocaleMiddleware
    from django.utils import translation

    class CustomLocaleMiddleware(LocaleMiddleware):
        def process_request(self, request):
            check_path = self.is_language_prefix_patterns_used()
            language = translation.get_language_from_request(
                request, check_path=check_path)
            if hasattr(request, 'user_info'):
                language = language[:2] + '-' + request.user_info['trans_code']
            translation.activate(language)
            request.LANGUAGE_CODE = translation.get_language()

(Note: user_info is added to request in middleware which is called before CustomLocaleMiddleware)

request.user_info['trans_code'] will be set to "ve" for vets and "cm" for car mechanics.

Then in settings:

    MIDDLEWARE_CLASSES = (
        ...
        'path.to.middleware.CustomAuthMiddleware', # this adds request.user_info
        'path.to.middleware.CustomLocaleMiddleware',
        ...
    )

    LANGUAGE_CODE = 'en'
    USE_I18N = True
    USE_L10N = True

    LANGUAGES = (
        ('en-ve', 'English Vets'),
        ('en-cm', 'English Car Mechanics'),
        ('fr-ve', 'French Vets'),
        ('fr-cm', 'French Car Mechanics'),
    )

    LOCALE_PATHS = (os.path.join(BASE_DIR,'locale'),)

You can create your .po files with

python manage.py makemessages -l en_VE -l en_CM -l fr_VE -l fr_CM

This has the advantage that it would default to the standard "de" translation if you had it but hadn't yet build "de-ve" or "de-cm".

SColvin
  • 11,584
  • 6
  • 57
  • 71