i18n

The i18n middleware provides internationalization and localization for Flame instances.

You can read source code of this middleware on GitHubi18n - 图1open in new window and API documentation on pkg.go.devi18n - 图2open in new window.

Installation

The minimum requirement of Go is 1.16.

  1. go get github.com/flamego/i18n

Usage examples

The i18n.I18ni18n - 图3open in new window is used in combination with i18n.Optionsi18n - 图4open in new window to bootstrap the localization engine, which is built upon the shining go-i18n/i18ni18n - 图5open in new window.

By default, the locale files should resides in the “locales” directory and be named in the format of locale_%s.ini, where %s is the IETF BCP 47 language tagi18n - 图6open in new window, e.g. “en-US”, “zh-CN”.

Using local files

  • Directory
  • main.go
  • locale_en-US.ini
  • locale_zh-CN.ini
  1. $ tree .
  2. .
  3. ├── locales
  4. ├── locale_en-US.ini
  5. └── locale_zh-CN.ini
  6. ├── go.mod
  7. ├── go.sum
  8. └── main.go
  1. package main
  2. import (
  3. "github.com/flamego/flamego"
  4. "github.com/flamego/i18n"
  5. )
  6. func main() {
  7. f := flamego.Classic()
  8. f.Use(i18n.I18n(
  9. i18n.Options{
  10. Languages: []i18n.Language{
  11. {Name: "en-US", Description: "English"},
  12. {Name: "zh-CN", Description: "简体中文"},
  13. },
  14. },
  15. ))
  16. f.Get("/", func(l i18n.Locale) string {
  17. return l.Translate("greeting")
  18. })
  19. f.Run()
  20. }
  1. greeting = How are you?
  1. greeting = 你好吗?

Using the embed.FS

  • Directory
  • main.go
  • embed.go
  • locale_en-US.ini
  • locale_zh-CN.ini
  1. $ tree .
  2. .
  3. ├── locales
  4. ├── embed.go
  5. ├── locale_en-US.ini
  6. └── locale_zh-CN.ini
  7. ├── go.mod
  8. ├── go.sum
  9. └── main.go
  1. package main
  2. import (
  3. "net/http"
  4. "github.com/flamego/flamego"
  5. "github.com/flamego/i18n"
  6. "main/locales"
  7. )
  8. func main() {
  9. f := flamego.Classic()
  10. f.Use(i18n.I18n(
  11. i18n.Options{
  12. FileSystem: http.FS(locales.Locales),
  13. Languages: []i18n.Language{
  14. {Name: "en-US", Description: "English"},
  15. {Name: "zh-CN", Description: "简体中文"},
  16. },
  17. },
  18. ))
  19. f.Get("/", func(l i18n.Locale) string {
  20. return l.Translate("greeting")
  21. })
  22. f.Run()
  23. }
  1. package locales
  2. import "embed"
  3. //go:embed *.ini
  4. var Locales embed.FS
  1. greeting = How are you?
  1. greeting = 你好吗?

WARNING

Because the Go embed encodes the entire path (i.e. including parent directories), the embedding should happen within the same directory as locale files.

Determine the language

The i18n middleware uses following technique to determine which language to use for translation:

  1. Look at the URL query parameter, by default it is lang, e.g. lang=en-US would force translation in “English (Unitied Stats)”.
  2. Look at the cookie value, by default it’s the value of lang.
  3. Look at the Accept-Language request header.
  4. Finally, fall back to the default language, which by default is the first language specified as the i18n.Options.Languages.

Regardless of how the language was determined, the cookie value is updated for saving preference.