translation/v2/translation_test.go
2025-07-10 10:49:57 +02:00

109 lines
3.6 KiB
Go

package translation_test
import (
"testing"
di "git.apihub24.de/admin/generic-di"
"git.apihub24.de/admin/translation/v2"
exampletranslations "git.apihub24.de/admin/translation/v2/example_translations"
)
func TestTranslationService_Init_And_Get_de(t *testing.T) {
translationService := di.Inject[translation.ITranslationService]()
translationService.Init(exampletranslations.Files)
value := translationService.Get("KEY", "de")
if value != "WERT" {
t.Errorf("expect 'WERT' for key 'KEY' but was %s", value)
return
}
}
func TestTranslationService_Get_de_en_fr(t *testing.T) {
translationService := di.Inject[translation.ITranslationService]()
translationService.Init(exampletranslations.Files)
value := translationService.Get("KEY", "de")
if value != "WERT" {
t.Errorf("expect 'WERT' for key 'KEY' but was %s", value)
return
}
value = translationService.Get("KEY", "en")
if value != "VALUE" {
t.Errorf("expect 'VALUE' for key 'KEY' but was %s", value)
return
}
value = translationService.Get("KEY", "fr")
if value != "VALEUR" {
t.Errorf("expect 'VALEUR' for key 'KEY' but was %s", value)
return
}
}
func TestTranslationService_Fallback_Is_en(t *testing.T) {
translationService := di.Inject[translation.ITranslationService]("TestTranslationService_Fallback_Is_en")
translationService.Init(exampletranslations.Files)
value := translationService.Get("KEY", "notexists")
if value != "VALUE" {
t.Errorf("expect 'VALUE' for key 'KEY' as Fallback but was %s", value)
return
}
}
func TestTranslationService_SetDefaultCulture_de(t *testing.T) {
translationService := di.Inject[translation.ITranslationService]("TestTranslationService_SetDefaultCulture_de")
translationService.Init(exampletranslations.Files)
translationService.SetDefaultCulture("de")
value := translationService.Get("KEY", "notexists")
if value != "WERT" {
t.Errorf("expect 'WERT' for key 'KEY' as Fallback but was %s", value)
return
}
}
func TestTranslationService_Error_Message_On_Key_Not_Exists(t *testing.T) {
translationService := di.Inject[translation.ITranslationService]()
translationService.Init(exampletranslations.Files)
value := translationService.Get("notexists", "de")
if value != "no value for key notexists found in source de" {
t.Errorf("expect Error Message 'no value for key notexists found in source de' but was %s", value)
return
}
}
func TestTranslationService_Get_Key_With_Parameter(t *testing.T) {
translationService := di.Inject[translation.ITranslationService]()
translationService.Init(exampletranslations.Files)
value := translationService.Get("WITH_PARAM", "de", "a", "b")
if value != "WERT a_b" {
t.Errorf("expect 'WERT a_b' for key 'WITH_PARAM' but was %s", value)
return
}
value = translationService.Get("WITH_PARAM", "en", "a", "b")
if value != "VALUE a_b" {
t.Errorf("expect 'VALUE a_b' for key 'WITH_PARAM' but was %s", value)
return
}
value = translationService.Get("WITH_PARAM", "fr", "a", "b")
if value != "VALEUR a_b" {
t.Errorf("expect 'VALEUR a_b' for key 'WITH_PARAM' but was %s", value)
return
}
}
func TestTranslationService_GetSource(t *testing.T) {
translationService := di.Inject[translation.ITranslationService]()
translationService.Init(exampletranslations.Files)
data := translationService.GetSource("de")
if data == nil {
t.Errorf("no map get from source")
return
}
if v, ok := data["KEY"]; !ok || v != "WERT" {
t.Errorf("missing key 'KEY' with value 'WERT'")
return
}
if v, ok := data["WITH_PARAM"]; !ok || v != "WERT %[1]s_%[2]s" {
t.Errorf("missing key 'WITH_PARAM' with value 'WERT %%[1]s_%%[2]s'")
return
}
}