10.1 Setting the default region

Finding out the locale

A locale is a set of descriptors for a particular geographical region, and can include specific language habits, text formatting, cultural idioms and a multitude of other settings. A locale’s name is usually composed of three parts. First (and mandatory) is the locale’s language abbreviation, such as “en” for English or “zh” for Chinese. The second part is an optional country specifier, and follows the first with an minus sign. This specifier allows web applications to distinguish between different countries which speak the same language, such as “en-US” for U.S. English, and “en-GB” for British English. The last part is another optional specifier, and is added to the locale with a period. It specifies which character set to use, for instance “zh-CN.gb2312” specifies the gb2312 character set for Chinese.

Go defaults to the “UTF-8” encoding set, so i18n in Go applications do not need to consider the last parameter. Thus, in our examples, we’ll only use the first two parts of locale descriptions as our standard i18n locale names.

On Linux and Solaris systems, you can use the locale -a command to get a list of all supported regional names. You can use this list as examples of some common locales. For BSD and other systems, there is no locale command, but the regional information is stored in /usr/share/locale.

Setting the locale

Now that we’ve defined what a locale is, we need to be able to set it according to visiting users’ information (either from their personal settings, the visited domain name, etc.). Here are some methods we can use to set the user’s locale:

From the domain name

We can set a user’s locale via the domain name itself when the application uses different domains for different regions. For example, we can use www.asta.com as our default English website, and the domain name www.asta.cn as its Chinese counterpart. By setting up separate domains for separate regions, you can detect and serve the requested locale. This type of setup has several advantages:

  • Identifying the locale via URL is distinctive and unambiguous
  • Users intuitively know which domain names to visit for their specific region or language
  • Implementing this scheme in a Go application is very simple and convenient, and can be achieved through a map
  • Conducive to search engine crawlers which can improve the site’s SEO

We can use the following code to implement a corresponding domain name locale:

  1. if r.Host == "www.asta.com" {
  2. i18n.SetLocale("en")
  3. } else if r.Host == "www.asta.cn" {
  4. i18n.SetLocale("zh-CN")
  5. } else if r.Host == "www.asta.tw" {
  6. i18n.SetLocale("zh-TW")
  7. }

Alternatively, we could have also set locales through the use of sub-domain such as “en.asta.com” for English sites and “cn.asta.com” for Chinese site. This scheme can be realized in code as follows:

  1. prefix:= strings.Split(r.Host,".")
  2. if prefix[0] == "en" {
  3. i18n.SetLocale("en")
  4. } else if prefix[0] == "cn" {
  5. i18n.SetLocale("zh-CN")
  6. } else if prefix[0] == "tw" {
  7. i18n.SetLocale("zh-TW")
  8. }

Setting locales from the domain name as we’ve done above has its advantages, however l10n is generally not implemented in this way. First of all, the cost of domain names (although usually quite affordable individually) can quickly add up given that each locale will need its own domain name, and often the name of the domain will not necessarily fit in with the local context. Secondly, we don’t want to have to individually configure each website for each locale. Rather, we should be able to do this programmatically, for instance by using URL parameters. Let’s have a look at the following description.

From URL parameters

The most common way of implementing l10n is to set the desired locale directly in the URL parameters, such www.asta.com/hello?locale=zh or www.asta.com/zh/hello. This way, we can set the region like so: i18n.SetLocale(params["locale"]).

This setup has almost all the advantages of prepending the locale in front of the domain and it’s RESTful, so we don’t need to add additional methods to implement it. The downside to this approach is that it requires a corresponding locale parameter inside each link, which can be quite cumbersome and may increase complexity. However, we can write a generic function that produces these locale-specific URLs so that all links are generated through it. This function should automatically add a locale parameter to each link so when users click them, we are able to parse their requests with ease: locale = params [" locale "].

Perhaps we want our URLs to look even more RESTful. For example, we could map each of our resources under a specific locale like www.asta.com/en/books for our English site and www.asta.com/zh/books for the Chinese one. This approach is not only more conducive to URL SEO, but is also more friendly for users. Anybody visiting the site should be able to access locale-specific website resources directly from the URL. Such URL addresses can then be passed through the application router in order to obtain the proper locale (refer to the REST section, which describes the router plug-in implementation):

  1. mux.Get("/:locale/books", listbook)

From the client settings area

In some special cases, we require explicit client information in order to set the locale rather than obtaining it from the URL or URL parameters. This information may come directly from the client’s browser settings, the user’s IP address, or the location settings filled out by the user at the time of registration. This approach is more suitable for web-based applications.

  • Accept-Language

When a client requests information using an HTTP header set with the Accept-Language field, we can use the following Go code to parse the header and set the appropriate region code:

  1. AL := r.Header.Get("Accept-Language")
  2. if AL == "en" {
  3. i18n.SetLocale("en")
  4. } else if AL == "zh-CN" {
  5. i18n.SetLocale("zh-CN")
  6. } else if AL == "zh-TW" {
  7. i18n.SetLocale("zh-TW")
  8. }

Of course, in real world applications, we may require more rigorous processes and rules for setting user regions

  • IP Address

Another way of setting a client’s region is to look at the user’s IP address. We can use the popular GeoIP GeoLite Country or City libraries to help us relate user IP addresses to their corresponding regional areas. Implementing this mechanism is very simple: we only need to look up the user’s IP address inside our database and then return locale-specific content according to which region was returned.

  • User profile

You can also let users provide you with their locale information through an input element such as a drop-down menu (or something similar). When we receive this information, we can save it to the account associated with the user’s profile. When the user logs in again, we will be able to check and set their locale settings -this guarantees that every time the user accesses the website, the returned content will be based on their previously set locale.

Summary

In this section, we’ve demonstrated a variety of ways with which user specific locales can be detected and set. These methods included setting the user locale via domain name, subdomain name, URL parameters and directly from client settings. By catering to the specific needs of specific regions, we can provide a comfortable, familiar and intuitive environment for users to access the services that we provide.