Documentation

Localization feature provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application. Language strings are stored in files within the ./locales directory. Within this directory there should be a subdirectory for each language supported by the application:

   main.go
└───locales
    ├───el-GR
           home.yml
    ├───en-US
           home.yml
    └───zh-CN
            home.yml

The default language for your application is the first registered language.

app := iris.New()

// First parameter: Glob filpath patern,
// Second variadic parameter: Optional language tags,
// the first one is the default/fallback one.
app.I18n.Load("./locales/*/*", "en-US", "el-GR", "zh-CN")

Or if you load all languages by filename:

app.I18n.Load("./locales/*/*")
// Then set the default language using:
app.I18n.SetDefault("en-US")

Load embedded locales

You may want to embed locales with the new embed directive within your application executable.

  1. Import the embed package; if you don’t use any exported identifiers from this package, you can do a blank import with _ "embed".

  1. Embed directives accept paths relative to the directory containing the Go source file. We can embed multiple files or even folders with wildcards. This uses a variable of the embed.FS type, which implements a simple virtual file system.

  1. Instead of the Load method, we should use the LoadFS one.

Defining Translations

Locale files can be written at YAML(recommended), JSON, TOML or INI form.

Each file should contain keys. Keys can have sub-keys(we call them "sections") too.

Each key's value should be of form string or map containing by its translated text (or template) or/and its pluralized key-values.

Iris i18n module supports pluralization out-of-the-box, see below.

Fmt Style

Template

Pluralization

Iris i18n supports plural variables. To define a per-locale variable you must define a new section of Vars key.

The acceptable keys for variables are:

  • one

  • "=x" where x is a number

  • "<x"

  • other

  • format

Example:

Then, each message can use this variable, here's how:

You can select what message will be shown based on a given plural count.

Except variables, each message can also have its plural form too!

Acceptable keys:

  • zero

  • one

  • two

  • "=x"

  • "<x"

  • ">x"

  • other

Let's create a simple plural-featured message, it can use the Minutes variable we created above too.

Let's continue with a bit more advanced example, using template text + functions + plural + variables.

Sections

If the key is not a reserved one (e.g. one, two...) then it acts as a sub section. The sections are separated by dot characters (.).

Determining The Current Locale

You may use the context.GetLocale method to determine the current locale or check if the locale is a given value:

The Locale interface looks like this.

Retrieving Translation

Use of context.Tr method as a shortcut to get a translated text for this request.

Inside Views

Last updated

Was this helpful?