Slide 1

Slide 1 text

Hello, Verden! Go Cph 2 May 2018 Go internationalization and localization

Slide 2

Slide 2 text

Hej!Mit navn er Terin.

Slide 3

Slide 3 text

Localization is the act of adapting to meet local expectations

Slide 4

Slide 4 text

Internationalization is the design and implementation that enables localization

Slide 5

Slide 5 text

gettext(3)

Slide 6

Slide 6 text

printf(ngettext( "%s has one car.\n", "%s has %d cars.\n", num_cars ), name, num_cars );

Slide 7

Slide 7 text

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] ""

Slide 8

Slide 8 text

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"

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

golang.org/x/text

Slide 11

Slide 11 text

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"

Slide 12

Slide 12 text

Formatted Numerics import "golang.org/x/text/number" p.Printf("I have %v bytes.\n", number.Decimal(1024)) Formats this via the printer's locale: I have 1.024 bytes.

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

Formatted Currencies import "golang.org/x/text/currency" amount := currency.DKK.Amount(1234.56) p.Printf("Total: %v\n", currency.Symbol(amount)) Formats this via the printer's locale: Total: kr. 1234.56 ☹

Slide 16

Slide 16 text

Collations import "golang.org/x/text/collate" c := collate.New(language.Danish) strings := StringSlice{ []string{"åben", "adelig"}, } c.Sort(strings) // ["adelig", "åben"]

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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)

Slide 19

Slide 19 text

HTTP Handler import "golang.org/x/text/language" matcher := language.NewMatcher([]language.Tag{ language.English, language.MustParse("en-AU"), language.Danish, language.Chinese, })

Slide 20

Slide 20 text

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") }

Slide 21

Slide 21 text

CLI Application tag, err := language.Parse(os.Getenv("LANG")) if err != nil { tag = language.English } p := message.NewPrinter(tag) p.Printf(w, "Hello World")

Slide 22

Slide 22 text

Defining Translations Go defines translations in "catalogs" that are transparently used when formatting strings using the message printer.

Slide 23

Slide 23 text

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))

Slide 24

Slide 24 text

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", ), )

Slide 25

Slide 25 text

Catalog Interface type Catalog interface { Languages() []language.Tag Matcher() language.Matcher Context(tag language.Tag, r catmsg.Renderer) *Context }

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Future Work ● Gender support ● Date formatting ● Much more!