Hello: multi language v2

This commit is contained in:
vinchent 2024-09-10 11:44:21 +02:00
parent 576e29f1ba
commit b50e202a97
2 changed files with 26 additions and 21 deletions

View File

@ -7,33 +7,36 @@ import (
const ( const (
english = "English" english = "English"
spanish = "Spanish" spanish = "Spanish"
englishHelloPrefix = "Hello "
spanishHelloPrefix = "Hola "
englishDefaultWorld = "world"
spanishDefaultWorld = "Mundo"
) )
func getDefaultWorldByLanguage(language string) string { var (
switch language { helloPrefixMap map[string]string
case english: defaultWorldMap map[string]string
return englishDefaultWorld )
case spanish:
return spanishDefaultWorld func initDefaults() {
initPrefixMap()
initWorldMap()
} }
return englishDefaultWorld func initPrefixMap() {
helloPrefixMap = make(map[string]string)
helloPrefixMap[english] = "Hello "
helloPrefixMap[spanish] = "Hola "
}
func initWorldMap() {
defaultWorldMap = make(map[string]string)
defaultWorldMap[english] = "world"
defaultWorldMap[spanish] = "Mundo"
}
func getDefaultWorldByLanguage(language string) string {
return defaultWorldMap[language]
} }
func getHelloPrefixByLanguage(language string) string { func getHelloPrefixByLanguage(language string) string {
switch language { return helloPrefixMap[language]
case english:
return englishHelloPrefix
case spanish:
return spanishHelloPrefix
}
return englishHelloPrefix
} }
func Hello(name, language string) string { func Hello(name, language string) string {
@ -46,5 +49,6 @@ func Hello(name, language string) string {
} }
func main() { func main() {
initDefaults()
fmt.Println(Hello("Mary", "English")) fmt.Println(Hello("Mary", "English"))
} }

View File

@ -3,6 +3,7 @@ package main
import "testing" import "testing"
func TestHello(t *testing.T) { func TestHello(t *testing.T) {
initDefaults()
t.Run("Hello someone", func(t *testing.T) { t.Run("Hello someone", func(t *testing.T) {
got := Hello("Jason", "English") got := Hello("Jason", "English")
exp := "Hello Jason" exp := "Hello Jason"