Hello: multiple languages

This commit is contained in:
vinchent 2024-09-10 11:35:30 +02:00
parent f593fb28de
commit 576e29f1ba
2 changed files with 49 additions and 12 deletions

View File

@ -1,19 +1,50 @@
package main
import "fmt"
const (
englishHelloPrefix = "Hello "
englishDefaultHelloName = "world"
import (
"fmt"
)
func Hello(name string) string {
if name == "" {
return englishHelloPrefix + englishDefaultHelloName
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
}
return fmt.Sprintf("%s%s", englishHelloPrefix, name)
return englishDefaultWorld
}
func getHelloPrefixByLanguage(language string) string {
switch language {
case english:
return englishHelloPrefix
case spanish:
return spanishHelloPrefix
}
return englishHelloPrefix
}
func Hello(name, language string) string {
prefix := getHelloPrefixByLanguage(language)
if name == "" {
return prefix + getDefaultWorldByLanguage(language)
}
return prefix + name
}
func main() {
fmt.Println(Hello("Mary"))
fmt.Println(Hello("Mary", "English"))
}

View File

@ -4,16 +4,22 @@ import "testing"
func TestHello(t *testing.T) {
t.Run("Hello someone", func(t *testing.T) {
got := Hello("Jason")
got := Hello("Jason", "English")
exp := "Hello Jason"
assertCorrectMsg(t, got, exp)
})
t.Run("Hello default", func(t *testing.T) {
got := Hello("")
got := Hello("", "English")
exp := "Hello world"
assertCorrectMsg(t, got, exp)
})
t.Run("Hello default", func(t *testing.T) {
got := Hello("Elodie", "Spanish")
exp := "Hola Elodie"
assertCorrectMsg(t, got, exp)
})
}
func assertCorrectMsg(t testing.TB, got, exp string) {