What is the standard/ idiomatic way to handle multilingual Go web apps?

Multi tool use
Multi tool use


What is the standard/ idiomatic way to handle multilingual Go web apps?



I'm working on a website which needs to deliver web pages in different languages as selected by the user. e.g. if the user selects Spanish as his preferred language, the server should send text elements of web pages in Spanish.



What is the standard way of doing it in Go? I'd also love to know what methods you guys are using.



Thanks.





Well, that is a very broad question. Some frameworks provide some functionality here. You might want to check golang.org/x/text which is still under development.
– Volker
Jul 3 at 5:42





That's a web framework and a site design feature, not a language feature. In fact, it's just one way of providing multilingual content. A multinational company would need different sites per language/country, not just translated content.
– Panagiotis Kanavos
Jul 3 at 9:03




2 Answers
2



I allways use a map and define a function on it, which returns the text for a given key:


type Texts map[string]string

func (t *Texts) Get(key string) string{
return (*t)[key]
}

var texts = map[string]Texts{
"de":Texts{
"title":"Deutscher Titel",
},
"en":Texts{
"title":"English title",
},
}

func executeTemplate(lang string){
tmpl, _ := template.New("example").Parse(`Title: {{.Texts.Get "title" }} `)
tmpl.Execute(os.Stdout,struct{
Texts Texts
}{
Texts: texts[lang],
})
}





This looks clean. Thanks!
– Kshitij Kumar
Jul 3 at 13:58





@KshitijKumar If the answer solved your issue, you should accept it.
– Everton
Jul 3 at 14:05





Answer accepted. Thanks a lot!
– Kshitij Kumar
Jul 3 at 14:40





I cleaned it a little bit more to segregate pages as well: play.golang.org/p/dMnvs0K3Hag
– Kshitij Kumar
Jul 6 at 11:03



If a user's preferred language has a possibility of being unavailable, you can use Golang's text/language package to match requested languages to supported languages.



This type of language matching is a non-trivial problem as outlined in this excellent post in The Go Blog.



To use the language package, create a matcher with a slice of supported languages:


var serverLangs = language.Tag{
language.AmericanEnglish, // en-US fallback
language.German, // de
}

var matcher = language.NewMatcher(serverLangs)



Then match against one or more preferred languages. (The preferred language may be based on the user's IP address or the Accept-Language header.)


Accept-Language


var userPrefs = language.Tag{
language.Make("gsw"), // Swiss German
language.Make("fr"), // French
}

tag, index, confidence := matcher.Match(sortLanguageTags(&langs, DescendingQuality)...)



To retrieve the corresponding text for the language, you can use the tag.String() method:


tag.String()


type Translation map[string]string
type Translations map[string]Translation

translations := Translations{
"knee": {
language.German.String(): "knie",
language.AmericanEnglish.String(): "knee",
},
}

fmt.Println(translations["knee"][tag.String()]) // prints "knie"






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

wVZ,e,EX1,xoU,VK6473Mm,CPDU9mG UVqja1TY6,MmuJBLxX,8F1fZY6pjJTyz2PnU4mT966OAJMMfBH
lsUTzkDIIrtHpWj6RF0z06f7dWgfA00,rD 2 mm3A6rAIovs0hp5VcQB3N,Rn7J

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications