Django gettext windows




















We have enough to do in the next month for 1. Yeah, I'd seen that. I was going to introduce something similar into Django's translation utilities also in pure Python. I think this is achievable. Thanks for the answer here. It helps flesh out my cunning plan a bit more. Let's put this on the list for 1. Sure it does. Thanks Marc! Just in case I also put there the GNU. Written by Ulrich Drepper.

And the same for msgmerge, msguniq and msgfmt. The code for django-taggit is open source and maintained by the collaborative developer community group Jazzband.

The project aims to provide necessary core features and then have an easy plugin format for additional features, rather than having every exhaustive feature built into the core system. The most accurate speech-to-text API. Built for Python developers. Full Stack Python. Example 1 from django-allauth django-allauth project website is a Django library for easily adding local and social authentication flows to Django projects. POST or None if request. In this case, the lazy translations in result will only be converted to strings when result itself is used in a string usually at template rendering time.

Django offers many utility functions particularly in django. These functions are used by template filters as well as directly in other code. For cases like this, use the django. Using this decorator means you can write your function and assume that the input is a proper string, then add support for lazy translation objects at the end.

Django does not support localizing your application into a locale for which Django itself has not been translated. In this case, it will ignore your translation files.

If you were to try this and Django supported it, you would inevitably see a mixture of translated strings from your application and English strings from Django itself. The first step is to create a message file for a new language. A message file is a plain-text file, representing a single language, that contains all available translation strings and how they should be represented in the given language.

Message files have a. Django comes with a tool, django-admin. To create or update a message file, run this command:. The language code, in this case, is in locale format. This script runs over your project source tree or your application source tree and pulls out all strings marked for translation. By default django-admin. In case you want to override that default, use the --extension or -e option to specify the file extensions to examine:.

The format of. For example, if your Django app contained a translation string for the text "Welcome to my site. Long messages are a special case. There, the first string directly after the msgstr or msgid is an empty string. Then the content itself will be written over the next few lines as one string per line. Those strings are directly concatenated. To reexamine all source code and templates for new translation strings and update all message files for all languages, run this:.

Do this with the django-admin. This tool runs over all available. In the same directory from which you ran django-admin. Behind the scenes, Django has a very flexible model of deciding which language should be used — installation-wide, for a particular user, or both. Django uses this language as the default translation — the final attempt if no other translator finds a translation. If you want to let each individual user specify which language he or she prefers, use LocaleMiddleware.

LocaleMiddleware enables language selection based on data from the request. It customizes content for each user. To use LocaleMiddleware , add 'django. Because middleware order matters, you should follow these guidelines:.

In each of these places, the language preference is expected to be in the standard language format, as a string. For example, Brazilian Portuguese is pt-br. If a base language is available but the sublanguage specified is not, Django uses the base language. For example, if a user specifies de-at Austrian German but Django only has de available, Django uses de.

If you want to select a language within a template, you can use the language template tag:. Example: en-us. See How Django discovers language preference. Hebrew, Arabic. English, French, German, etc. If you enable the django. You can also retrieve information about any of the available languages using provided template tags and filters.

If you do this in your view:. Django provides an integrated solution for these problems: It passes the translations into JavaScript, so you can call gettext , etc. The main solution to these problems is the following JavaScriptCatalog view, which generates a JavaScript code library with functions that mimic the gettext interface, plus an array of translation strings. A view that produces a JavaScript code library with functions that mimic the gettext interface, plus an array of translation strings.

Translation domain containing strings to add in the view output. Defaults to 'djangojs'. A list of application names among installed applications. Those apps should contain a locale directory.

The precedence of translations is such that the packages appearing later in the packages argument have higher precedence than the ones appearing at the beginning. This is important in the case of clashing translations for the same literal. If you use more than one JavaScriptCatalog view on a site and some of them define the same strings, the strings in the catalog that was loaded last take precedence.

When the catalog is loaded, your JavaScript code can use the following methods:. The gettext function behaves similarly to the standard gettext interface within your Python code:.

The ngettext function provides an interface to pluralize words and phrases:. The interpolate function supports dynamically populating a format string.

The interpolation syntax is borrowed from Python, so the interpolate function supports both positional and named interpolation:.

Positional interpolation: obj contains a JavaScript Array object whose elements values are then sequentially interpolated in their corresponding fmt placeholders in the same order they appear. Named interpolation: This mode is selected by passing the optional boolean named parameter as true. This emulates the gettext function but does nothing, returning whatever is passed to it:. The pgettext function behaves like the Python variant pgettext , providing a contextually translated word:.

The npgettext function also behaves like the Python variant npgettext , providing a pluralized contextually translated word:. The pluralidx function works in a similar way to the pluralize template filter, determining if a given count should use a plural form of a word or not:.

In the simplest case, if no custom pluralization is needed, this returns false for the integer 1 and true for all other numbers. However, pluralization is not this simple in all languages. If the language does not support pluralization, an empty value is provided.

Additionally, if there are complex rules around pluralization, the catalog view will render a conditional expression. This will evaluate to either a true should pluralize or false should not pluralize value. In order to use another client-side library to handle translations, you may want to take advantage of the JSONCatalog view.

See the documentation for JavaScriptCatalog to learn about possible values and use of the domain and packages attributes. Server-side caching will reduce CPU load. To trigger cache invalidation when your translations change, provide a version-dependent key prefix, as shown in the example below, or map the view at a version-dependent URL:. Client-side caching will save bandwidth and make your site load faster. Otherwise, you can apply conditional decorators.

In the following example, the cache is invalidated whenever you restart your application server:. You can even pre-generate the JavaScript catalog as part of your deployment procedure and serve it as a static file. This radical technique is implemented in django-statici18n. Using either one of these features requires that an active language be set for each request; in other words, you need to have django.

If localized URLs get reversed in templates they always use the current language. To link to a URL in another language use the language template tag. It enables the given language in the enclosed template section:. The language tag expects the language code as the only argument. Once the string literals of an application have been tagged for later translation, the translation themselves need to be written or obtained.

The first step is to create a message file for a new language. A message file is a plain-text file, representing a single language, that contains all available translation strings and how they should be represented in the given language. Message files have a. Django comes with a tool, django-admin makemessages , that automates the creation and upkeep of these files. The makemessages command and compilemessages discussed later use commands from the GNU gettext toolset: xgettext , msgfmt , msgmerge and msguniq.

The minimum version of the gettext utilities supported is 0. When you run makemessages from the root directory of your project, the extracted strings will be automatically distributed to the proper message files. That is, a string extracted from a file of an app containing a locale directory will go in a message file under that directory.

By default django-admin makemessages examines every file that has the. If you want to override that default, use the --extension or -e option to specify the file extensions to examine:. When creating message files from JavaScript source code you need to use the special djangojs domain, not -e js. To extract strings from a project containing Jinja2 templates, use Message Extracting from Babel instead.

For more information, read its documentation about working with message catalogs.



0コメント

  • 1000 / 1000