Golang

Localization in Go – Part 1

Recently while writing an API in Go, I faced issues in localizing my application. Currently, Go’s standard package does not provide localization support, so I decided to write a simple blog post on some steps we need to implement it.

What do we want ?

I simply want one method to which if I pass a key and a language as an argument. It should return a message in that language. Lets see one example for better understanding:

Lets define some data first:

en-US.yaml

es-AR.yaml

Time for some action:

translate(“welcome”, “en-US”) => “Hello”
translate(“welcome”, “es-AR”) => “Hola”

Whoa! that is exactly our requirement, lets see how can we do this.

Lets pickup a package

Lets not re-invent the wheel and look over for some package which we can use to implement localization. After some research, I decided to use nicksnyder/go-i18n package. Reason being that it is the most popular one and is actively maintained. So my post will be using examples very specific to the library which I have chosen.

Cheer up ! We are already one step towards success.

Now What ? Lets plan and code!

The package documentation seems quite full of knowledge and different implementations of how to use the library. Lets jump right on to the steps which are relevant to our use case.

Create YAML files containing data

Create some YAML files in root directory of your application:

en-US.yaml (English US)

es-AR.yaml (Spanish Argentina)

In our main function define a slice of strings containing path to all language translation files which our application will support.

Please note that I have given only file names because translation files are in same directory of my main.go file. If this is not the case with you, please give complete path to yaml files.

Register the above translations using go-i18n package

Whenever our application starts, we will load all translations in memory so that they can later be used for translating a key. Have a look at the code below:

Oh that is a lot of code. I added few comments to help you understand it better. In short the CreateLocalizerBundle function will read all our translation files and load them so that they can be used later. I am keeping the explanation shorter as everything in code is library method, if you are curious to know what is going inside please go through the source code of library.

Time for some magic

Now we have our bundle ready which is capable of translating a key to message of any defined language. Lets write some code to get this working.

So as you can see in the code above, whenever we need to translate a message, we can create a localizer for the current language and use the localizer for translation.

Now lets refactor our code and write a translate function which will accept a key and locale and return a message as per locale.

Now we can use our translate method to translate a key to a message of current language. The complete source code can be found here. In part-II of this post I will be writing how to use localisation in a Go API. So whenever a request will come to our application we will read the user’s current language from request header and return all response messages in that language only.

Conclusion

Thanks for reading. I hope this post helps you to implement localisation in your Go application. I have shown a very basic implementation of localisation, If you have any queries, feel free to comment here and I will love to help you out.