Django template comment multiline




















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.

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.

This means that everybody will be using the same encoding, which is important when Django processes the PO files. By default, fuzzy entries are not processed by compilemessages. 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 compilemessages utility. This tool runs over all available. In the same directory from which you ran django-admin makemessages , run django-admin compilemessages like this:.

Django only supports. In some cases, such as strings with a percent sign followed by a space and a string conversion type e. Or you can use no-python-format so that all percent signs are treated as literals:. You create and update the message files the same way as the other Django message files — with the django-admin makemessages tool. The only difference is you need to explicitly specify what in gettext parlance is known as a domain in this case the djangojs domain, by providing a -d djangojs parameter, like this:.

This would create or update the message file for JavaScript for German. After updating message files, run django-admin compilemessages the same way as you do with normal Django message files.

This is only needed for people who either want to extract message IDs or compile message files. Translation work itself involves editing existing files of this type, but if you want to create your own message files, or want to test or compile a changed message file, download a precompiled binary installer. You may also use gettext binaries you have obtained elsewhere, so long as the xgettext --version command works properly. If you need more flexibility, you could also add a new argument to your custom makemessages command:.

As a convenience, Django comes with a view, django. The view expects to be called via the POST method, with a language parameter set in request. If that is found and Django considers it to be a safe URL i. You may want to set the active language for the current session explicitly. That applies to the current thread only. You would typically want to use both: django. 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 translation mechanisms can be used to translate arbitrary texts to any language that is supported by Django as long as an appropriate translation catalog exists, of course.

You can load a translation catalog, activate it and translate text to language of your choice, but remember to switch back to original language, as activating a translation catalog is done on per-thread basis and such change will affect code running in the same thread.

Functions of particular interest are django. To help write more concise code, there is also a context manager django. With it, the above example becomes:. If you know gettext , you might note these specialties in the way Django does translation:.

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 better matching translation is found through one of the methods employed by the locale middleware see below. If you want to let each individual user specify which language they prefer, then you also need to use the 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, follow these guidelines:. For more on middleware, see the middleware documentation. First, it looks for the language prefix in the requested URL. This header is sent by your browser and tells the server which language s you prefer, in order by priority.

Django tries each language in the header until it finds one with available translations. In each of these places, the language preference is expected to be in the standard language format , as a string. Copy link. If you need an immediate workaround I think your example would work as The newline requirement to handle the native tags would be perfectly fine for us :. Node parsing should always treat native django tags as plain text Sign up for free to join this conversation on GitHub.

Already have an account? Sign in to comment. Linked pull requests. You signed in with another tab or window. Reload to refresh your session. You can also create your own custom template filters; see How to create custom template tags and filters. See The Django admin documentation generator. Tags are more complex than variables: Some create text in the output, some control flow by performing loops or logic, and some load external information into the template to be used by later variables.

Some tags require beginning and ending tags i. Django ships with about two dozen built-in template tags. You can read all about them in the built-in tag reference.

Loop over each item in an array. You can also use filters and various operators in the if tag:. While the above example works, be aware that most template filters return strings, so mathematical comparisons using filters will generally not work as you expect. Again, the above is only a selection of the whole list; see the built-in tag reference for the complete list.

You can also create your own custom template tags; see How to create custom template tags and filters. For example, this template would render as 'hello' :. If you need to comment out a multiline portion of the template, see the comment tag. In this example, the block tag defines three blocks that child templates can fill in. All the block tag does is to tell the template engine that a child template may override those portions of the template.

The extends tag is the key here. At that point, the template engine will notice the three block tags in base. You can use as many levels of inheritance as needed. One common way of using inheritance is the following three-level approach:. This approach maximizes code reuse and helps to add items to shared content areas, such as section-wide navigation. This is useful if you want to add to the contents of a parent block instead of completely overriding it.

See Extending an overridden template in the Overriding templates How-to for a full example. For example, consider this template fragment:. By default in Django, every template automatically escapes the output of every variable tag. Specifically, these five characters are escaped:. Again, we stress that this behavior is on by default. Why would you want to turn it off? For example, you might store a blob of HTML in your database and want to embed that directly into your template.

To disable auto-escaping for an individual variable, use the safe filter:. Think of safe as shorthand for safe from further escaping or can be safely interpreted as HTML. To control auto-escaping for a template, wrap the template or a particular section of the template in the autoescape tag, like so:.



0コメント

  • 1000 / 1000