O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The package golang.org/x/text/message contains the function NewPrinter which accepts the language identification and creates the formatted I/O, the same as the fmt package does, but with the ability to translate messages based on gender and plural forms.

The Set function of the message package adds the translation and plurals selection. The plural form itself is selected based on rules set via the Selectf function. The Selectf function produces the catalog.Message type with rules based on the plural.Form or selector.

The preceding sample code uses plural.One and plural.Other forms, and =x, <x selectors. These are matched against the formatting verb %d (other verbs can also be used). The first matching case is chosen.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required