Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go Internationalization and Localization

Go Internationalization and Localization

An introduction to Go's text package, which allows developers to build localized applications and services.

Terin Stock

May 02, 2018
Tweet

More Decks by Terin Stock

Other Decks in Programming

Transcript

  1. Strings are extracted from the program's source into a Portable

    Object Template. xgettext -c main.c -o main.pot This results in main.pot. #: main.c:1 msgid "%s has one car.\n" msgid_plural "%s has %d cars.\n" msgstr[0] "" msgstr[1] ""
  2. A translator can generate a Portable Object file. msginit --locale=da

    --input=main.pot Then translate da.po. #: main.c:1 msgid "%s has one car.\n" msgid_plural "%s has %d cars.\n" msgstr[0] "%s har en bil.\n" msgstr[1] "%s har %d biler.\n"
  3. During the build step, Portable Object files are compiled into

    Machine Object files, and distributed with the software package. At runtime, the appropriate locale is displayed based on the setting of environment variables.
  4. Formatted Output import "golang.org/x/text/message" p := message.NewPrinter(language.Danish) p.Printf( "Hello, my

    name is %s. I have %d cars.\n", name, num_cars, ) "Hello, my name is Terin. I have 1.024 cars.\n"
  5. Formatted Numerics p.Printf( "The mass of the sun is %v

    kilograms.\n", number.Scientific(1.9891e30)) Formats this via the printer's locale: The mass of the sun is 2 × 10³⁰ kilograms.
  6. Formatted Numerics p.Printf( "The maximum power output is %v watts.\n",

    number.Engineering(2e7)) Formats this via the printer's locale: The maximum power output is 20 × 10⁶ watts.
  7. Locales • Composed of multiple parts, to define language, script,

    region, variants, and extensions. • Go uses BCP 47 language tags ◦ en ◦ en-DK ◦ da ◦ da-GL ◦ zh-Hans ◦ he-IL-u-ca-hebrew-tz-jeruslm
  8. Locales import "golang.org/x/text/language" var l language.Tag l = language.English l

    = language.MustParse("he-IL-u-ca-hebrew-tz-jeruslm") p := message.NewPrinter(l)
  9. HTTP Handler handler(w http.ResponseWriter, r *http.Request) { lang, _ :=

    r.Cookie("lang") accept := r.Header.Get("Accept-Language") tag, _ := language.MatchStrings( matcher, lang.String(), accept) p := message.NewPrinter(tag) p.Fprintln(w, "Hello World") }
  10. CLI Application tag, err := language.Parse(os.Getenv("LANG")) if err != nil

    { tag = language.English } p := message.NewPrinter(tag) p.Printf(w, "Hello World")
  11. Defining Translations Go defines translations in "catalogs" that are transparently

    used when formatting strings using the message printer.
  12. Building a Catalog import "golang.org/x/text/message/catalog" l := language.Danish cat :=

    catalog.NewBuilder() cat.SetString(l, "Hello, my name is %s.\n", "Hej, mit navn er %s.\n", ) p := message.NewPrinter(l, message.Catalog(cat))
  13. Plurals import "golang.org/x/text/feature/plural" cat.Set( language.Danish, "I have %d cars.\n", plural.Selectf(1,

    "%d", "=0", "Jeg har ingen biler.\n", "one", "Jeg har en bil.\n", "other", "Jeg har %d biler.\n", ), )
  14. gotext go get golang.org/x/text/cmd/gotext/... • Go replacement for "gettext" ◦

    Extract text from Go programs ◦ Update catalogs ◦ Code generation for loading catalogs