How to return to multiple languages ​​in Django at runtime?

I am creating a Django application that uses Django translation functions to provide localization in multiple languages. But I also use Django's translation features to translate certain terminology into different industries based on current user settings.

For example, for an English-speaking user working in the learning assessment industry, I want the following behavior:

For this request to the page:

  • Look at the user's natural language (e.g. German).
  • Look at the user industry (e.g. learning assessment)
  • Activate German language / learning assessment course (eg translation.activate ("learning-evaluation-de")

The "learning-evaluation-de" .po file will only convert a subset of all the lines in the project, because it is only there to translate certain industry terminology.

This is the question:

When the line is missing, I want Django to return to German (defined in step # 1 above), and not English (the default language in my settings.py).

My default files for English / German .po are industry specific.

Is it possible?

+6
source share
1 answer

I think this is possible, and one of the fastest ways to do this (even if you check to see if it works) would be for the Django translation module for the monkey patch to add support for auxiliary languages ​​(not verified):

from django.utils.translation import trans_real ... # Import other things # Added `fallback_language` parameter def do_translate(message, translation_function, fallback_language=None): """ Translates 'message' using the given 'translation_function' name -- which will be either gettext or ugettext. It uses the current thread to find the translation object to use. If no current translation is activated, the message will be run through the default translation object. """ global _default # str() is allowing a bytestring message to remain bytestring on Python 2 eol_message = message.replace(str('\r\n'), str('\n')).replace(str('\r'), str('\n')) t = getattr(_active, "value", None) if t is not None: result = getattr(t, translation_function)(eol_message) else: # Use other language as fallback. if fallback_language is not None: fallback = translation(fallback_language) result = getattr(_default, translation_function)(eol_message) else: if _default is None: from django.conf import settings _default = translation(settings.LANGUAGE_CODE) result = getattr(_default, translation_function)(eol_message) if isinstance(message, SafeData): return mark_safe(result) return result # Added `fallback_language` parameter def do_ntranslate(singular, plural, number, translation_function, fallback_language=None): global _default t = getattr(_active, "value", None) if t is not None: return getattr(t, translation_function)(singular, plural, number) # Use other language as fallback. if fallback_language is not None: fallback = translation(fallback_language) return getattr(fallback, translation_function)(singular, plural, number) if _default is None: from django.conf import settings _default = translation(settings.LANGUAGE_CODE) return getattr(_default, translation_function)(singular, plural, number) # Override Django functions with custom ones. trans_real.do_translate = do_translate trans_real.do_ntranslate = do_ntranslate 

The two above functions are taken from the django.utils.translation.trans_real module. Just added a few lines to these functions. You will also need to change other functions (e.g. gettext , ngettext , pgettext ) to accept the fallback_language parameter and pass it to the two above. Try it if this code works.

Please note that neutralization of monkeys is common for the entire project - it affects your entire application and third-party applications. Alternatively, you can take the source code of the django.utils.translation.trans_real module as a base for creating custom translation functions that will be used only in a few places of your application.

+1
source

Source: https://habr.com/ru/post/952996/


All Articles