Improve this question. Michael Michael 3, 3 3 gold badges 30 30 silver badges 56 56 bronze badges. It is not configured. I didn't think it to be necessary. Also: make sure the django. LocaleMiddleware middleware is present in your settings file — dentemm.
LocaleMiddleware is not necessary. I don't want to let them select the language. Strange situation. Then deleted both. And now it works. I don't know how it happened. But not it is translating. Could it have anything with caching in Django?
If yes, how to clear cache? Show 2 more comments. Active Oldest Votes. Check for the following: Settings. Django then provides utilities to extract the translation strings into a message file. This file is a convenient way for translators to provide the equivalent of the translation strings in the target language.
Once the translators have filled in the message file, it must be compiled. This process relies on the GNU gettext toolset. Then Django will make some optimizations so as not to load the internationalization machinery. See Format localization for more details. Specify a translation string by using the function ugettext. In Django, we have chosen not to follow this practice, for a couple of reasons:. In this example, the text "Welcome to my site. Obviously, you could code this without using the alias.
This example is identical to the previous one:. More on makemessages later. This technique lets language-specific translations reorder the placeholder text. For example, an English translation may be "Today is November For this reason, you should use named-string interpolation e.
If you would like to give translators hints about a translatable string, you can add a comment prefixed with the Translators keyword on the line preceding the string, e. This also works in templates with the comment tag:.
The comment will then appear in the resulting. Use the function django. The string is later translated from a variable. Use this if you have constant strings that should be stored in the source language because they are exchanged over systems or users — such as strings in a database — but should be translated at the last possible point in time, such as when the string is presented to the user. In this example the number of objects is passed to the translation languages as the count variable.
When using this technique, make sure you use a single name for every extrapolated variable included in the literal. In the example above note how we used the name Python variable in both translation strings.
This example would fail:. You would get an error when running django-admin. Sometimes words have several meanings, such as "May" in English, which refers to a month name and to a verb. To enable translators to translate these words correctly in different contexts, you can use the django. Both take a context string as the first variable.
In the resulting. Contextual markers are also supported by the trans and blocktrans template tags. Use the lazy versions of translation functions in django. These functions store a lazy reference to the string — not the actual translation. The translation itself will be done when the string is used in a string context, such as in template rendering. This is essential when calls to these functions are located in code paths that are executed at module load time.
This is something that can easily happen when defining models, forms and model forms, because Django implements these such that their fields are actually class-level attributes. For that reason, make sure to use lazy translations in the following cases:.
For example, to translate the help text of the name field in the following model, do the following:. For example:. This necessitates the use of the helper function described next. Standard Python string joins ''. Instead, you can use django. 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.
The bidi attribute is True only for bi-directional languages. The source of the language information is the django.
Similar access to this information is available for template code. See below. Translations in Django templates uses two template tags and a slightly different syntax than in Python code.
If the noop option is present, variable lookup still takes place but the translation is skipped. Internally, inline translations use an ugettext call. In case a template var myvar above is passed to the tag, the tag will first resolve such variable to a string at run-time and then look up that string in the message catalogs.
Contrarily to the trans tag, the blocktrans tag allows you to mark complex sentences consisting of literals and variable content for translation by making use of placeholders:. To translate a template expression — say, accessing object attributes or using template filters — you need to bind the expression to a local variable for use within the translation block.
You can use multiple expressions inside a single blocktrans tag:. When you use both the pluralization feature and bind values to local variables in addition to the counter value, keep in mind that the blocktrans construct is internally converted to an ungettext call. This means the same notes regarding ungettext variables apply. Reverse URL lookups cannot be carried out within the blocktrans and should be retrieved and stored beforehand:.
Each RequestContext has access to three translation-specific variables:. Translation hooks are also available within any template block tag that accepts constant strings. In this example, the translation infrastructure will be passed the string "yes,no" , not the individual strings "yes" and "no". The translated string will need to contain the comma so that the filter parsing code knows how to split up the arguments.
For example, a German translator might translate the string "yes,no" as "ja,nein" keeping the comma intact. 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 django. We explore several ways to determine user's language preference, translate URLs and add a language switcher. Edit a template file, load the i18n tag and use the trans tag to translate a string:. Create a file called urls. Create a directory called templates in the project root. Create a directory called pages in it and add a file called home. Add these lines in the home. Load the i18n tag in every template that uses translation tags even if the parent template already loads it.
Visit the homepage and you should see this:. The compilemessages command creates. The gettext function translates a message and returns it as a string. Add it in the settings. Let's try that. Here is an example on how to set up the cookie manually.
Edit the pages app views. Now the language for the home page will always be Finnish. Our custom code overrides all other behaviours. The Accept-Language header is sent by the browser.
0コメント