Hey there, fellow Go developers! Today, I want to share some insights on a topic that's close to my heart: localization in Golang. If you're developing a web application that serves users from different parts of the world, it's essential to cater to their language preferences and regional settings. This is where localization comes in handy, making your app more user-friendly and accessible.
In this blog post, we'll dive into the world of localization with Golang, exploring how to leverage the powerful go-i18n package to manage translations, detect user language preferences, handle pluralization and contextual rules, and format dates, times, currency, and numbers. By the end of this post, you'll be equipped with the knowledge and practical examples needed to create a Golang web application that speaks your users' language. So, let's get started!
Now, let's take a look at an example of setting up a simple "Hello, World!" Golang application that displays a localized message based on the user's language preference.
1. First, install Go on your system by following the official installation guide: https://golang.org/doc/install
2. Create a new directory for your project and navigate to it:
mkdir go-localization-example
cd go-localization-example
3. Initialize a Go module:
go mod init go-localization-example
4. Create a main.go file in the project directory with the following content:
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello,World!")
}
5. Now, let's add localization support to our application. Create a new directory named locales within your project directory to store translation files:
mkdir locales
6. Inside the locales directory, create two JSON files named en-US.json and tr-TR.json for English (United States) and Turkish (Turkey) translations respectively. These files will contain the translation strings for our application:
en-US.json:
{
"hello_world": "Hello, World!"
}
tr-TR.json:
{
"hello_world": "Merhaba, Dünya!"
}
In the next part, we will discuss Golang localization concepts in more detail, and you will learn how to use the Go i18n package to load and use these translation files in your application.
Golang Localization Concepts
Before diving deeper into localization with Golang, let's understand some key concepts:
- Internationalization (i18n): It is the process of designing and developing a software application so that it can be adapted to various languages and regions without any changes in the source code.
- Localization (l10n): It is the actual adaptation of the application for a specific language or region, which includes translating text, formatting dates and currencies, and other region-specific adjustments.
Now, let's proceed with the Go i18n package, which provides powerful tools for handling localization in Golang applications.
1. Install the go-i18n package by running the following command:
go get -u github.com/nicksnyder/go-i18n/v2/i18n
In your main.go file, import the required packages and initialize the i18n Bundle with the translation files you created earlier:
package main
import (
"fmt"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)
func main() {
// Initialize the i18n Bundle
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("json", i18n.UnmarshalJSON)
// Load the translation files
bundle.MustLoadMessageFile("locales/en-US.json")
bundle.MustLoadMessageFile("locales/tr-TR.json")
// Rest of the code...
}
3. Next, create a function that takes a language tag as input and returns the localized "Hello, World!" message for that language:
func getLocalizedMessage(langTag string, bundle *i18n.Bundle) string {
// Create a Localizer for the specified language
localizer := i18n.NewLocalizer(bundle, langTag)
// Localize the "hello_world" message
localizedMessage, _ := localizer.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "hello_world",
Other: "Hello, World!",
},
})
return localizedMessage
}
4. Update the main function to use the getLocalizedMessage function and display the localized message based on the user's language preference. For this example, let's assume the user prefers Turkish:
func main() {
// Initialize the i18n Bundle
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("json", i18n.UnmarshalJSON)
// Load the translation files
bundle.MustLoadMessageFile("locales/en-US.json")
bundle.MustLoadMessageFile("locales/tr-TR.json")
// Get the localized message
userPreferredLanguage := "tr-TR"
localizedMessage := getLocalizedMessage(userPreferredLanguage, bundle)
// Print the localized message
fmt.Println(localizedMessage)
}
When you run this updated application, it will display "Merhaba, Dünya!" instead of "Hello, World!" since we set the user's preferred language to Turkish.
In the next part, we will explore more about handling datetime and money localization in Golang and how to use JSON for these purposes.
Handling Datetime and Money Localization in Golang
In this section, we'll learn how to handle datetime and money localization using JSON in your Golang applications. We'll use the golang.org/x/text package to format dates, times, and currencies according to the user's locale.
1. Install the golang.org/x/text package by running the following command:
go get -u golang.org/x/text
2. In your main.go file, import the required packages:
import (
"fmt"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/currency"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
3. Create a function that formats a date according to the user's language preference:
func formatDate(langTag string, date time.Time) string {
// Parse the language tag
lang, _ := language.Parse(langTag)
// Create a new message printer with the specified language
p := message.NewPrinter(lang)
// Format the date
formattedDate := p.Sprintf("%s, %s %d, %d", date.Weekday(), date.Month(), date.Day(), date.Year())
return formattedDate
}
Similarly, create a function that formats a currency value according to the user's language preference:
func formatCurrency(langTag string, value float64, cur currency.Unit) string {
// Parse the language tag
lang, _ := language.Parse(langTag)
// Create a new message printer with the specified language
p := message.NewPrinter(lang)
// Format the currency value
formattedCurrency := p.Sprintf("%s %.2f", currency.Symbol(cur), value)
return formattedCurrency
}
5. Update the translation files to include date and currency formatting examples. Add the following keys to both en-US.json and tr-TR.json:
en-US.json:
{
"hello_world": "Hello, World!",
"today_is": "Today is",
"balance": "Your balance is"
}
tr-TR.json:
{
"hello_world": "Merhaba, Dünya!",
"today_is": "Bugün",
"balance": "Bakiyeniz"
}
6. Update the main function to print a localized date and currency example:
func main() {
// Initialize the i18n Bundle
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("json", i18n.UnmarshalJSON)
// Load the translation files
bundle.MustLoadMessageFile("locales/en-US.json")
bundle.MustLoadMessageFile("locales/tr-TR.json")
// Get the localized message
userPreferredLanguage := "tr-TR"
localizedMessage := getLocalizedMessage(userPreferredLanguage, bundle)
// Print the localized message
fmt.Println(localizedMessage)
// Format and print the date and currency examples
// Example date and currency values
exampleDate := time.Date(2023, time.March, 29, 0, 0, 0, 0, time.UTC)
exampleCurrencyValue := 12345.67
// Format the date and currency values
formattedDate := formatDate(userPreferredLanguage, exampleDate)
formattedCurrency := formatCurrency(userPreferredLanguage, exampleCurrencyValue, currency.TRY)
// Get the localized messages for date and currency
localizedDateMessage := getLocalizedMessage(userPreferredLanguage, bundle, "today_is")
localizedCurrencyMessage := getLocalizedMessage(userPreferredLanguage, bundle, "balance")
// Print the localized date and currency messages
fmt.Printf("%s %s\n", localizedDateMessage, formattedDate)
fmt.Printf("%s %s\n", localizedCurrencyMessage, formattedCurrency)
}
When you run this updated application, it will display a localized date and currency message, formatted according to the user's preferred language (Turkish, in this case):
Merhaba, Dünya!
Bugün ÇarÅamba, Mart 29, 2023
Bakiyeniz ⺠12.345,67
In the next part, we will discuss how to implement localization in popular Golang web frameworks such as Go Fiber, Gorilla Mux, and net/http.
Implementing Localization in Popular Golang Web Frameworks
In this section, we'll explore how to implement localization in three popular Golang web frameworks: Go Fiber, Gorilla Mux, and net/http.
Go Fiber
1. Install Go Fiber by running the following command:
go get -u github.com/gofiber/fiber/v2
2. In your main.go file, import the Go Fiber package and create a new Fiber app:
import (
"github.com/gofiber/fiber/v2"
// Other imports...
)
func main() {
// Create a new Fiber app
app := fiber.New()
// Rest of the code...
}
3. Define a middleware function that extracts the user's language preference from the request's "Accept-Language" header and stores it in the request's Locals:
func detectLanguageMiddleware(c *fiber.Ctx) error {
// Get the user's language preference from the "Accept-Language" header
lang := c.Get("Accept-Language", "en-US")
// Store the language preference in the request's Locals
c.Locals("lang", lang)
// Proceed to the next middleware or route handler
return c.Next()
}
4. Register the middleware function and create a route handler that responds with a localized message:
func main() {
// Create a new Fiber app
app := fiber New()
// Register the middleware function
New app.Use(detectLanguageMiddleware)
// Create a route handler that responds with a localized message
app.Get("/", func(c *fiber.Ctx) error {
// Get the user's language preference from the request's Locals
lang := c.Locals("lang").(string)
// Get the localized message
localizedMessage := getLocalizedMessage(lang, bundle)
// Send the localized message as the response
return c.SendString(localizedMessage)
})
// Start the Fiber app on port 3000
app.Listen(":3000")
}
Gorilla Mux
1. Install Gorilla Mux by running the following command:
go get -u github.com/gorilla/mux
In your main.go file, import the Gorilla Mux package and create a new Gorilla Mux router:
import (
"github.com/gorilla/mux"
"net/http"
// Other imports...
)
func main() {
// Create a new Gorilla Mux router
router := mux.NewRouter()
// Rest of the code...
}
3. Define a middleware function that extracts the user's language preference from the request's "Accept-Language" header and stores it in the request's context:
func detectLanguageMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the user's language preference from the "Accept-Language" header
lang := r.Header.Get("Accept-Language")
if lang == "" {
lang = "en-US"
}
// Store the language preference in the request's context
ctx := context.WithValue(r.Context(), "lang", lang)
// Proceed to the next middleware or route handler
next.ServeHTTP(w, r.WithContext(ctx))
})
}
4. Register the middleware function and create a route handler that responds with a localized message:
func main() {
// Create a new Gorilla Mux router
router := mux.NewRouter()
// Register the middleware function
router.Use(detectLanguageMiddleware)
// Create a route handler that responds with a localized message
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Get the user's language preference from the request's context
lang := r.Context().Value("lang").(string)
// Get the localized message
localizedMessage := getLocalizedMessage(lang, bundle)
// Send the localized message as the response
w.Write([]byte(localizedMessage))
})
// Start the Gorilla Mux router on port 3000
http.ListenAndServe(":3000", router)
}
When you run the updated application and send a request to http://localhost:3000, the server will respond with a localized "Hello, World!" message based on the "Accept-Language" header sent by the client.
net/http
1. In your main.go file, remove the Gorilla Mux import and create a new net/http router:
import (
"net/http"
// Other imports...
)
func main() {
// Rest of the code...
}
2. Register the middleware function and create a route handler that responds with a localized message:
func main() {
// Create a new net/http router
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Apply the detectLanguageMiddleware
detectLanguageMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the user's language preference from the request's context
lang := r.Context().Value("lang").(string)
// Get the localized message
localizedMessage := getLocalizedMessage(lang, bundle)
// Send the localized message as the response
w.Write([]byte(localizedMessage))
})).ServeHTTP(w, r)
})
// Start the net/http router on port 3000
http.ListenAndServe(":3000", nil)
}
When you run the updated application and send a request to http://localhost:3000, the server will respond with a localized "Hello, World!" message based on the "Accept-Language" header sent by the client.
In this section, we covered how to implement localization in three popular Golang web frameworks: Go Fiber, Gorilla Mux, and net/http. By applying these techniques, you can easily create web applications that support multiple languages and cater to a global audience.
Implementing Language Switching
In this part, we'll discuss how to implement language switching in your Golang web applications, allowing users to select their preferred language manually. This can be particularly useful when the user's preferred language is different from the one detected by their browser or device.
1. Modify the detectLanguageMiddleware (for Fiber, Gorilla Mux, or net/http, depending on the framework you're using) to check if a "lang" query parameter is present in the request. If it is, use that as the user's language preference. Otherwise, fallback to the "Accept-Language" header or the default language:
func detectLanguageMiddleware(c *fiber.Ctx) error {
// Check if a "lang" query parameter is present in the request
lang := c.Query("lang")
// If not, get the user's language preference from the "Accept-Language" header
if lang == "" {
lang = c.Get("Accept-Language", "en-US")
}
// Store the language preference in the request's Locals
c.Locals("lang", lang)
// Proceed to the next middleware or route handler
return c.Next()
}
2. Update your web application to include a user interface for selecting the preferred language. This can be a simple drop-down list, a set of buttons, or any other control that allows the user to choose their language.
3. When the user selects a language from the interface, send a request to the server with the chosen language as a "lang" query parameter. This can be done using JavaScript or by submitting a form.
For example, using JavaScript and the Fetch API, you can send a request to the server when the user selects a language from a drop-down list:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Language Switch Example</title>
</head>
<body>
<h1 id="localized-message"></h1>
<select id="language-selector">
<option value="en-US">English</option>
<option value="tr-TR">Türkçe</option>
</select>
<script>
const languageSelector = document.getElementById("language-selector");
const localizedMessageElement = document.getElementById("localized-message");
async function fetchLocalizedMessage(lang) {
const response = await fetch(`/?lang=${lang}`);
const localizedMessage = await response.text();
localizedMessageElement.textContent = localizedMessage;
}
languageSelector.addEventListener("change", () => {
fetchLocalizedMessage(languageSelector.value);
});
// Fetch the initial localized message
fetchLocalizedMessage(languageSelector.value);
</script>
</body>
</html>
This will update the displayed message on the page according to the user's selected language, without reloading the entire page.
In this part, we've discussed how to implement language switching in Golang web applications, allowing users to select their preferred language manually. By adding this feature to your application, you can provide a more personalized and user-friendly experience to your users, regardless of their browser or device settings.
Handling Pluralization and Contextual Rules
In some languages, the rules for pluralization and contextual translation can be quite complex. In this part, we'll discuss how to handle pluralization and contextual rules in your Golang localization process using the go-i18n package.
1. Update your translation files to include plural forms for a key. For example, let's add a "messages_count" key with plural forms to both en-US.json and tr-TR.json:
en-US.json:
{
"hello_world": "Hello, World!",
"messages_count": {
"one": "You have {{.Count}} new message.",
"other": "You have {{.Count}} new messages."
}
}
tr-TR.json:
{
"hello_world": "Merhaba, Dünya!",
"messages_count": {
"one": "{{.Count}} yeni mesajınız var.",
"other": "{{.Count}} yeni mesajınız var."
}
}
2. Create a function that returns a localized message with the appropriate plural form based on the provided count:
func getLocalizedPluralMessage(langTag string, bundle *i18n.Bundle, messageID string, count int) string {
// Parse the language tag
lang, _ := language.Parse(langTag)
// Create a new message printer with the specified language
p := message.NewPrinter(lang)
// Get the localized message template
localizedMessageTemplate := p.Sprintf(bundle.Lookup(messageID))
// Create a template with the localized message
tmpl, err := template.New("localizedMessage").Parse(localizedMessageTemplate)
if err != nil {
log.Fatal(err)
}
// Create a buffer to store the rendered template
var renderedMessage bytes.Buffer
// Render the template with the provided data
err = tmpl.Execute(&renderedMessage, map[string]interface{}{
"Count": count,
})
if err != nil {
log.Fatal(err)
}
return renderedMessage.String()
}
3. Use the getLocalizedPluralMessage function to get a localized message with the appropriate plural form for a given count:
func main() {
// Initialize the i18n Bundle
// ... (previous code)
// Get the localized message
// ... (previous code)
// Get the localized message with pluralization
newMessagesCount := 3
localizedPluralMessage := getLocalizedPluralMessage(userPreferredLanguage, bundle, "messages_count", newMessagesCount)
// Print the localized message with pluralization
fmt.Println(localizedPluralMessage)
}
When you run the updated application, it will display a localized message with the appropriate plural form, based on the newMessagesCount value:
Merhaba, Dünya!
3 yeni mesajınız var.
By using the go-i18n package and following the steps outlined in this part, you can handle pluralization and contextual rules in your Golang localization process effectively, ensuring that your translations are accurate and grammatically correct across different languages.
Formatting Dates and Times
When localizing your web application, it's important to consider the various ways that dates and times are represented in different languages and regions. In this section, we'll discuss how to format dates and times in Golang using the golang.org/x/text/message package.
1. Install the golang.org/x/text package by running the following command:
go get -u golang.org/x/text
2. In your main.go file, import the golang.org/x/text/message and golang.org/x/text/language packages:
import (
"fmt"
"golang.org/x/text/language"
"golang.org/x/text/message"
// Other imports...
)
3. Create a function that formats a given date and time according to the user's preferred language:
func formatDateTime(langTag string, dt time.Time) string {
// Parse the language tag
lang, _ := language.Parse(langTag)
// Create a new message printer with the specified language
p := message.NewPrinter(lang)
// Format the date and time according to the user's preferred language
formattedDateTime := p.Sprintf("%v", dt)
return formattedDateTime
}
4. Use the formatDateTime function to format a date and time according to the user's preferred language:
func main() {
// Set the user's preferred language
userPreferredLanguage := "tr-TR"
// Get the current date and time
now := time.Now()
// Format the date and time according to the user's preferred language
formattedDateTime := formatDateTime(userPreferredLanguage, now)
// Print the formatted date and time
fmt.Println(formattedDateTime)
}
When you run the updated application, it will display the current date and time formatted according to the user's preferred language:
29 Mart 2023 15:26:45
By following these steps, you can format dates and times in your Golang web application according to the user's preferred language, ensuring a consistent and user-friendly experience across different languages and regions.
Formatting Currency and Numbers
In addition to dates and times, it's important to consider the various ways that currency and numbers are represented in different languages and regions when localizing your web application. In this section, we'll discuss how to format currency and numbers in Golang using the golang.org/x/text/message package.
1. As we have already installed the golang.org/x/text package and imported the required packages in the previous section, we can move on to the next step.
2. Create a function that formats a given number as currency according to the user's preferred language:
func formatCurrency(langTag string, amount float64) string {
// Parse the language tag
lang, _ := language.Parse(langTag)
// Create a new message printer with the specified language
p := message.NewPrinter(lang)
// Format the amount as currency according to the user's preferred language
formattedCurrency := p.Sprintf("%.2f", amount)
return formattedCurrency
}
3. Use the formatCurrency function to format an amount as currency according to the user's preferred language:
func main() {
// Set the user's preferred language
userPreferredLanguage := "tr-TR"
// Set an amount
amount := 1234.56
// Format the amount as currency according to the user's preferred language
formattedCurrency := formatCurrency(userPreferredLanguage, amount)
// Print the formatted currency
fmt.Println(formattedCurrency)
}
When you run the updated application, it will display the amount formatted as currency according to the user's preferred language: 1.234,56
4. Create a function that formats a given number with the appropriate grouping separators according to the user's preferred language:
func formatNumber(langTag string, number int64) string {
// Parse the language tag
lang, _ := language.Parse(langTag)
// Create a new message printer with the specified language
p := message.NewPrinter(lang)
// Format the number with grouping separators according to the user's preferred language
formattedNumber := p.Sprintf("%d", number)
return formattedNumber
}
5. Use the formatNumber function to format a number with grouping separators according to the user's preferred language:
func main() {
// Set the user's preferred language
userPreferredLanguage := "tr-TR"
// Set a number
number := int64(1234567)
// Format the number with grouping separators according to the user's preferred language
formattedNumber := formatNumber(userPreferredLanguage, number)
// Print the formatted number
fmt.Println(formattedNumber)
}
When you run the updated application, it will display the number formatted with grouping separators according to the user's preferred language: 1.234.567
By following these steps, you can format currency and numbers in your Golang web application according to the user's preferred language, ensuring a consistent and user-friendly experience across different languages and regions.
In conclusion, throughout this tutorial, we have covered how to implement localization in Golang web applications, including handling pluralization and contextual rules, formatting dates, times, currency, and numbers, and loading translation files from JSON. By applying these techniques, you can create web applications that support multiple languages and provide a user-friendly experience to a global audience.