Localization

Introduction

Localization features 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:

app.I18n.Load("./locales/*/*")

Then set the default language using:

Load embedded locales

You may want to embed locales with a go-bindata tool within your application executable.

  1. install a go-bindata tool, e.g.

    $ go get -u github.com/go-bindata/go-bindata/...

  2. embed local files to your application

    $ go-bindata -o locales.go ./locales/...

  3. use the LoadAssets method to initialize and load the languages

    ^ The AssetNames and Asset functions are generated by go-bindata

Defining Translations

Each file should contain keys with translated text or template values.

Fmt Style

YAML, TOML, JSON, INI files.

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.

INI Sections

INI Sections are separated by dot ".". The second optional value can be a map or a struct as the template value like the rest file formats.

Inside Views

Sitemap

Sitemap translations are automatically set to each route by path prefix if app.I18n.PathRedirect is true or by subdomain if app.I18n.Subdomain is true or by URL query parameter if app.I18n.URLParameter is not empty.

Read more at: https://support.google.com/webmasters/answer/189077?hl=en

Last updated

Was this helpful?