Djangothen provides utilities to extract the translation strings into amessage file. This file is a convenient way for translators to providethe equivalent of the translation strings in the target language. Once thetranslators have filled in the message file, it must be compiled. This processrelies on the GNU gettext toolset.
Since string extraction is done by the xgettext command, only syntaxessupported by gettext are supported by Django. In particular, Pythonf-strings are not yet supported by xgettext, andJavaScript template strings need gettext 0.21+.
When using ngettext(), make sure you use a single name for everyextrapolated variable included in the literal. In the examples above, notehow we used the name Python variable in both translation strings. Thisexample, besides being incorrect in some languages as noted above, wouldfail:
Sometimes words have several meanings, such as "May" in English, whichrefers to a month name and to a verb. To enable translators to translatethese words correctly in different contexts, you can use thedjango.utils.translation.pgettext() function, or thedjango.utils.translation.npgettext() function if the string needspluralization. Both take a context string as the first variable.
In the resulting .po file, the string will then appear as often as there aredifferent contextual markers for the same string (the context will appear on themsgctxt line), allowing the translator to give a different translation foreach of them.
This is something that can easily happen when defining models, forms andmodel forms, because Django implements these such that their fields areactually class-level attributes. For that reason, make sure to use lazytranslations in the following cases:
For any other case where you would like to delay the translation, but have topass the translatable string as argument to another function, you can wrapthis function inside a lazy call yourself. For example:
The name, name_local, and name_translated attributes of thedictionary contain the name of the language in English, in the languageitself, and in your current active language respectively. The bidiattribute is True only for bi-directional languages.
Translations in Django templates uses two templatetags and a slightly different syntax than in Python code. To give your templateaccess to these tags, put % load i18n % toward the top of your template.As with all template tags, this tag needs to be loaded in all templates whichuse translations, even those templates that extend from other templates whichhave already loaded the i18n tag.
When you use both the pluralization feature and bind values to local variablesin addition to the counter value, keep in mind that the blocktranslateconstruct is internally converted to an ngettext call. This means thesame notes regarding ngettext variablesapply.
Another feature % blocktranslate % supports is the trimmed option.This option will remove newline characters from the beginning and the end ofthe content of the % blocktranslate % tag, replace any whitespace at thebeginning and end of a line and merge all lines into one using a spacecharacter to separate them. This is quite useful for indenting the content of a% blocktranslate % tag without having the indentation characters end upin the corresponding entry in the .po file, which makes the translationprocess easier.
You can also use the % get_language_info_list % template tag to retrieveinformation for a list of languages (e.g. active languages as specified inLANGUAGES). See the section about the set_language redirectview for an example of how to display a languageselector using % get_language_info_list %.
The main solution to these problems is the following JavaScriptCatalog view,which generates a JavaScript code library with functions that mimic thegettext interface, plus an array of translation strings.
A list of application names amonginstalled applications. Those apps should contain a localedirectory. All those catalogs plus all catalogs found inLOCALE_PATHS (which are always included) are merged into onecatalog. Defaults to None, which means that all availabletranslations from all INSTALLED_APPS are provided in theJavaScript output.
The precedence of translations is such that the packages appearing later in thepackages argument have higher precedence than the ones appearing at thebeginning. This is important in the case of clashing translations for the sameliteral.
Additionally, if there are complex rules around pluralization, the catalog viewwill render a conditional expression. This will evaluate to either a true(should pluralize) or false (should not pluralize) value.
If localized URLs get reversed in templates they always use the currentlanguage. To link to a URL in another language use the languagetemplate tag. It enables the given language in the enclosed template section:
The first step is to create a message file for a new language. A messagefile is a plain-text file, representing a single language, that contains allavailable translation strings and how they should be represented in the givenlanguage. Message files have a .po file extension.
The script runs over your project source tree or your application source treeand pulls out all strings marked for translation (seeHow Django discovers translations and be sure LOCALE_PATHSis configured correctly). It creates (or updates) a message file in thedirectory locale/LANG/LC_MESSAGES. In the de example, the file will belocale/de/LC_MESSAGES/django.po.
When you run makemessages from the root directory of your project, theextracted strings will be automatically distributed to the proper message files.That is, a string extracted from a file of an app containing a localedirectory will go in a message file under that directory. A string extractedfrom a file of an app without any locale directory will either go in amessage file under the directory listed first in LOCALE_PATHS orwill generate an error if LOCALE_PATHS is empty.
By default django-admin makemessages examines everyfile that has the .html, .txt or .py file extension. If you want tooverride that default, use the --extensionor -e option to specify the file extensions to examine:
This tool runs over all available .po files and creates .mo files, whichare binary files optimized for use by gettext. In the same directory fromwhich you ran django-admin makemessages, rundjango-admin compilemessages like this:
This is only needed for people who either want to extract message IDs or compilemessage files (.po). Translation work itself involves editing existingfiles of this type, but if you want to create your own message files, or wantto test or compile a changed message file, download a precompiled binaryinstaller.
While Django provides a rich set of i18n tools for use in views and templates,it does not restrict the usage to Django-specific code. The Django translationmechanisms can be used to translate arbitrary texts to any language that issupported by Django (as long as an appropriate translation catalog exists, ofcourse). You can load a translation catalog, activate it and translate text tolanguage of your choice, but remember to switch back to original language, asactivating a translation catalog is done on per-thread basis and such changewill affect code running in the same thread.
Functions of particular interest aredjango.utils.translation.get_language() which returns the language usedin the current thread, django.utils.translation.activate() whichactivates a translation catalog for the current thread, anddjango.utils.translation.check_for_language()which checks if the given language is supported by Django.
To help write more concise code, there is also a context managerdjango.utils.translation.override() that stores the current language onenter and restores it on exit. With it, the above example becomes:
If you want to let each individual user specify which language theyprefer, then you also need to use the LocaleMiddleware.LocaleMiddleware enables language selection based on data from the request.It customizes content for each user.
At runtime, Django builds an in-memory unified catalog of literals-translations.To achieve this it looks for translations by following this algorithm regardingthe order in which it examines the different file paths to load the compiledmessage files (.mo) and the precedence of multipletranslations for the same literal:
In all cases the name of the directory containing the translation is expected tobe named using locale name notation. E.g. de, pt_BR, es_AR,etc. Untranslated strings for territorial language variants use the translationsof the generic language. For example, untranslated pt_BR strings use pttranslations.
This way, you can write applications that include their own translations, andyou can override base translations in your project. Or, you can build a bigproject out of several apps and put all translations into one big commonmessage file specific to the project you are composing. The choice is yours.
Translation is the communication of the meaning of a source-language text by means of an equivalent target-language text.[1] The English language draws a terminological distinction (which does not exist in every language) between translating (a written text) and interpreting (oral or signed communication between users of different languages); under this distinction, translation can begin only after the appearance of writing within a language community.
A translator always risks inadvertently introducing source-language words, grammar, or syntax into the target-language rendering. On the other hand, such "spill-overs" have sometimes imported useful source-language calques and loanwords that have enriched target languages. Translators, including early translators of sacred texts, have helped shape the very languages into which they have translated.[2]
Because of the laboriousness of the translation process, since the 1940s efforts have been made, with varying degrees of success, to automate translation or to mechanically aid the human translator.[3] More recently, the rise of the Internet has fostered a world-wide market for translation services and has facilitated "language localisation".[4]
3a8082e126