May 15, 2021 Revel
Revel uses text files to provide international translation support. Revel supports language translation documentation, automatic area queries, cookie rewrites, nested messages and parameters.
en-US
en
Refer to
ISO 639-1 codes for language identifiers.
US
For zone identifiers, refer
to ISO 3166-1 alpha-2 codes.
Revel handles message files and internationalization in a similar way to other frameworks.
If you want to get started, you can refer
revel/samples/i18n
which contains all the basics.
The internationalization message is defined in the message file. T hese messages are used when rendering view templates (or elsewhere in the program). When you create a new message file, there are several rules to follow:
messages
directory at the root of the application.
There are no restrictions on the file name of the message file; A
s long as the extension is legitimate. T
here is no limit to the number of files per language. W
hen the application starts, Revel
messages
in the messages language directory and merges them according to the language.
This means you are free to organize your message files.
For example, you might want to take the traditional approach of defining all of them in one file:
/app
/messages
messages.en
messages.fr
...
Or create multiple files for each language and organize them by category:
/app
/messages
labels.en
warnings.en
labels.fr
warnings.fr
...
Important: In multiple files in the same language, although the same message key can technically be defined, this can lead to unpredictable behavior. When more than one file is defined per language, be careful to keep your key unique so that when the files are merged, the key will not be overwritten!
The message file is a goconfig file. This means that the message should be defined in key-value format:
key=value
Take a chestnut:
greeting=Hello
greeting.name=Rob
greeting.suffix=, welcome to Revel!
Goconfig files are divided into multiple sections . ways present and contains messages defined when the segment is not specified. For example:
key=value
[SECTION]
key2=value2
key=value
message message is implicitly placed in the default segment because it is not defined in a specified segment.
All messages in the message file should be in the default segment defined, unless they are used for a specific region (see Regions for more messages).
Note: Segment is goconfig feature.
Messages for a specific region should be defined in the same paragraph. R
egion-specific messages should be defined in sections with the same name. F
or example, suppose we want to display
"Hello"
"Hey"
for UK users,
"Howdy"
To do this, we can define the following message
greeting.en
:
greeting=Hello
[GB]
greeting=Hey
[US]
greeting=Howdy
Revel parses greeting to Hello when English
en
is
greeting
as their preferred
Hello
Revel resolves the greeting with the message in the corresponding segment only
en-US
region is clearly defined, such as en-GB or
en-GB
greeting
Important: If an illegal area is defined, it is technically allowed, but they are never resolved.
Messages in a file can also refer to other messages. T
his allows the user to form a single message from one or more other messages. T
he syntax of the
%(key)s
.
For example:
greeting=Hello
greeting.name=Rob
greeting.suffix=, welcome to Revel!
greeting.full=%(greeting)s %(greeting.name)s%(greeting.suffix)s
Attention:
The message supports one or more parameters. T
he parameters in the message are
fmt
rules as the fmt package.
For example:
greeting.name_arg=Hello %s!
The parameters are parsed in the given order, and the reference resolves the message .
To find out which language environment your users like, Revel looks for an available language environment in the following places:
Language cookies
For each request, the framework looks for the application configuration
i18n.cookie
I
f found, its value is assumed to be the current locale.
When a cookie has been discovered, all other resolution methods will be skipped.
Accept-Language
HTTP header
For each request, Revel automatically parses
Accept-Language
E
ach of these language environments is
acquired and stored
in
Request
instance according to the HTTP specification.
This information is used to determine the current language environment for a later message resolution function.
For more information, please refer to Parsed Accept-Language HTTP Header .
The default language
When none of these methods find an available client language environment, the framework uses the default language defined in the
i18n.default_language
If a message resolution fails, a specially formatted string containing the original message name is returned.
Note:
Accept-Language
information
is
always parsed and saved to the
Request
even if it already exists.
In this case, the values in the header information can be used when they are needed, even if they have never been used by message resolution.
Applications can
Request
the current
Request.Locale
property.
For example:
func (c App) Index() revel.Result {
currentLocale := c.Request.Locale
c.Render(currentLocale)
}
In the template, you can access the current language environment
currentLocale
variable in
renderArgs
For example:
<p>Current preferred locale: {{.currentLocale}}</p>
If the program needs access
Accept-Language
HTTP header information, it can
Controller
from
Request
the Controller instance.
AcceptLanguages
AcceptLanguage
contains all the values that are parsed from the respective header information, and the most appropriate value is the first in the slice.
For example:
func (c App) Index() revel.Result {
// 获取所有解析到的AcceptLanguage的字符串表示形式
c.RenderArgs["acceptLanguageHeaderParsed"] = c.Request.AcceptLanguages.String()
// 返回最合适AcceptLanguage实例
c.RenderArgs["acceptLanguageHeaderMostQualified"] = c.Request.AcceptLanguages[0]
c.Render()
}
For more information, please refer to the HTTP specification .
Messages can be resolved in the controller or template.
Message
Message(message string, args ...interface{})
the message using the current locale:
func (c App) Index() revel.Result {
c.RenderArgs["controllerGreeting"] = c.Message("greeting")
c.Render()
}
Use the template function
msg in the
msg
messages for the current locale.
For example:
<p>Greetings without arguments: {{msg . "greeting"}}</p>
<p>Greetings: {{msg . "greeting.full.name" "Tommy Lee Jones"}}</p>
Note: The
msg
the msg function is
msg . "message name" "argument" "argument"
.
If there are no arguments, you can ignore them.
File | Options | Describe |
---|---|---|
app.conf
|
i18n.cookie
|
The name of the language cookie. The prefix of the Revel cookie should be used to avoid cookie name conflicts. |
app.conf
|
i18n.default_language
|
The default locale, which is used without a preferred region. |