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 (
|
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"))
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
|
Loading…
Reference in New Issue
Block a user