Making your code multilingual using x/text

By Marcel van Lohuizen

Elevator Pitch

In this talk I’ll show how to use golang.org/x/text to localize and internationalize your applications. After discussing some of the principles and important aspects of the x/text repository, I will dive straight into a life demo localizing an English-only web server from scratch.

Description

This talk discusses an overview of the part of the golang.org/x/text repo that deals with localizing text. This includes an overview of the pipeline for extracting and injecting text, as well as how to format printed values such as numbers and currencies in locale-specific ways.

The talk will start with discussing some of the fundamental components, like language matching, and show by means of some fun facts why things are trickier than they may seem.

The life demo will take up at least 50% of the talk, and shows how a simple web demo can be localized. This part includes:

  • showing how gotext rewrites some code to be more suitable for localization
  • extracts strings
  • how to generate code from translations obtained externally
  • selecting the user language
  • all the stuff you get for free, like number formatting and selecting the right regional settings even though a local language may not be available.
  • selecting translations based on plural forms
  • A note on the various ways to add translations to your binary and how the community can help to support more formats.

The attendee will learn that it is better to specify the purpose and function of an element to be formatted than to provide one own’s formatting. Another main takeaway is that despite all the complexity it is relatively easy for a developer to support multiple languages. Finally, the attendee will learn how to contribute to the project.

Notes

I would argue I’m the best person to speak on this topic as I’ve developed the vast lion share of x/text.

The talk will be aimed at a general audience. The purpose of x/text is to make it as easy for developers to localize their programs, without having to know the peculiarities of languages. The talk contains example based on non-English languages providing both comic and to provide a use x/text and “don’t try this at home” message.