Soluling home   Document home

Go Localization and Internationalization

Go Localization and Internationalization

Go (Wikipedia) is a popular programming language. Soluling localization tool and service support go resource files.

The end of the document contains links to get the full source code of the samples. After reading this document, we recommend reading a tutorial about how to use Soluling.

Process

Use go-i18n (homepage) package to internationalize your code. Start by installing go-i18n.

go get -u github.com/nicksnyder/go-i18n/v2/goi18n

Import go-i18n into your code.

import "github.com/nicksnyder/go-i18n/v2/i18n"

Create a localization bundle.

bundle := i18n.NewBundle(language.English)

Load translations into your bundle.

bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
// Add your language(s) here. This loads English, German and Finnish. 
bundle.MustLoadMessageFile("active.en.json")
bundle.MustLoadMessageFile("active.de.json")
bundle.MustLoadMessageFile("active.fi.json")

Set the active language.

localizer := i18n.NewLocalizer(bundle, "fi")  // Select Finnish

Use the localizer to translate strings.

fmt.Println("Hello World")

Change the above line.

fmt.Println(
  localizer.MustLocalize(&i18n.LocalizeConfig{
    DefaultMessage: &i18n.Message{
      ID: "HelloWorld",
      Other: "Hello World"},
  })) 

The syntax go-i18n uses, is a bit verbose, but it is also very flexible.

You can use interpolated strings.

fmt.Println(
  localizer.MustLocalize(&i18n.LocalizeConfig{
    DefaultMessage: &i18n.Message{
      ID: "HelloName",
      Other: "Hello {{.name}}!",
  },
  TemplateData: map[string]interface{}{
    "name": "John",
  },
})) 

You can use plural enabled messages.

fmt.Println(
  localizer.Localize(&i18n.LocalizeConfig{
    DefaultMessage: &i18n.Message{
      ID: "Skis",
      One: "I have {{.ski}} ski.",
      Other: "I have {{.ski}} skis.",
    },
    TemplateData: map[string]interface{}{
      "ski": 2,
    },
    PluralCount: 2,
}))

Once you have internationalized your code run the extractor tool. go-i18n supports three resource file formats: JSON, YAML, and TOML. Soluling supports JSON.

goi18n extract -format json

The extractor creates a resource file called active.en.json.

Create a Soluling project

Now it is time to create a Soluling project for our resource file. Start Soluling. Drag and drop active.en.json file into Soluling or click New from File or Files and browse the active.en.json file. The project wizard shows the Select File Type dialog.

Localized options

Select go-i18n file and click OK. Select Languages page appears. Add the languages you want to support and also select the original language.

Select languages

We added Finnish and German as target languages and English as the original language. Complete the wizard by clicking Finish. A new project appears.

New project

Next, translate the strings.

Translated project

Finally, click Build All to build the localized resource files (active.de.json and active.fi.json) in the same directory as the original active.en.json.

Samples

GitHub and <data-dir>\Samples\Go contains following Go samples:

Directory Description
Simple A simple sample that uses go-i18n package and shows how to localize plain, interpolated, and plural messages.

Configuring Go Localization

You create a resource file localization project by adding an Go resource file (e.g. D:\Sample\active.en.json) into your Soluling project. You can configure how to localize your Go resource file by selecting the item in the project tree, right-clicking, and choosing the Options menu. A source dialog appears that lets you edit the options. This source uses the following option sheets.