How to Use the golang.org/x/text Package for i18n

Use golang.org/x/text/language and message packages to detect locales and format localized strings in Go applications.

Use golang.org/x/text/language to detect locales and golang.org/x/text/message to format strings with the NewPrinter function.

package main

import (
	"golang.org/x/text/language"
	"golang.org/x/text/message"
	"fmt"
)

func main() {
	tag := language.MustParse("es")
	p := message.NewPrinter(tag)
	p.Printf("Hello, %s!\n", "World")
}
  1. Import the language and message packages from golang.org/x/text.
  2. Parse your target locale tag using language.MustParse("en") or language.MustParse("es").
  3. Create a new printer instance with message.NewPrinter(tag).
  4. Call p.Printf() or p.Sprintf() to output localized strings.
  5. Install the module by running go get golang.org/x/text@latest.