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 (
english = "English"
spanish = "Spanish"
englishHelloPrefix = "Hello "
spanishHelloPrefix = "Hola "
englishDefaultWorld = "world"
spanishDefaultWorld = "Mundo"
)
func getDefaultWorldByLanguage(language string) string {
switch language {
case english:
return englishDefaultWorld
case spanish:
return spanishDefaultWorld
var (
helloPrefixMap map[string]string
defaultWorldMap map[string]string
)
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 {
switch language {
case english:
return englishHelloPrefix
case spanish:
return spanishHelloPrefix
}
return englishHelloPrefix
return helloPrefixMap[language]
}
func Hello(name, language string) string {
@ -46,5 +49,6 @@ func Hello(name, language string) string {
}
func main() {
initDefaults()
fmt.Println(Hello("Mary", "English"))
}

View File

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