Golang : Translate language with language package example
Just a simple example on how to use the language
package to do language translation. Not a google translate type of translation engine.... still need to provide exact match for each translation to work.
Putting this here for my own future reference.
Here you go!
package main
import (
//"fmt" -- replace with message package in this example
"golang.org/x/text/language"
"golang.org/x/text/language/display"
"golang.org/x/text/message"
)
func main() {
// define a direct translation from English to Dutch
message.SetString(language.Dutch, "In %v people speak %v.", "In %v spreekt men %v.")
fr := language.French
region, _ := fr.Region()
for _, tag := range []string{"en", "nl"} {
p := message.NewPrinter(language.Make(tag))
p.Printf("In %v people speak %v.", display.Region(region), display.Language(fr))
p.Println()
}
// define a direct translation from English to Chinese
message.SetString(language.Chinese, "In %v people speak %v.", "在%v说%v.") // * Must match for translation to work
zh := language.Chinese
region, _ = zh.Region()
for _, tag := range []string{"en", "zh"} {
p := message.NewPrinter(language.Make(tag))
p.Printf("In %v people speak %v.", display.Region(region), display.Language(zh)) // * Must match for translation to work
p.Println()
}
// define a direct translation from English to Malay
message.SetString(language.Malay, "In %v people speak %v.", "Orang %v berbahasa %v.")
msmy := language.Malay
region, _ = msmy.Region()
for _, tag := range []string{"en", "ms"} {
p := message.NewPrinter(language.Make(tag))
p.Printf("In %v people speak %v.", display.Region(region), display.Language(msmy))
p.Println()
}
}
Output:
In France people speak French.
In Frankrijk spreekt men Frans.
In China people speak Chinese.
在中国说中文.
In Malaysia people speak Malay.
Orang Malaysia berbahasa Melayu.
References:
https://godoc.org/golang.org/x/text/message
https://godoc.org/golang.org/x/text/language/display#Dictionary.Languages
See also : Golang : Gargish-English language translator
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.2k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+7.6k Javascript : How to check a browser's Do Not Track status?
+26.3k Golang : Encrypt and decrypt data with AES crypto
+4.5k MariaDB/MySQL : Form select statement or search query with Chinese characters
+30.5k Golang : Download file example
+7.9k Golang : Auto-generate reply email with text/template package
+24.2k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+5k Javascript : Change page title to get viewer attention
+35.8k Golang : Converting a negative number to positive number
+19.7k Golang : Compare floating-point numbers
+7.1k Golang : How to convert strange string to JSON with json.MarshalIndent