Hello: multi language v2
This commit is contained in:
parent
576e29f1ba
commit
b50e202a97
44
hello.go
44
hello.go
@ -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"))
|
||||
}
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user