add GetSource
This commit is contained in:
parent
45b0355570
commit
11173f90b0
@ -2,4 +2,4 @@ module git.apihub24.de/admin/translation/v2
|
||||
|
||||
go 1.22.0
|
||||
|
||||
require git.apihub24.de/admin/generic-di v1.4.0
|
||||
require git.apihub24.de/admin/generic-di v1.4.0
|
||||
|
||||
@ -1 +1,4 @@
|
||||
git.apihub24.de/admin/generic-di v1.4.0 h1:0mQnpAcavMLBcnF5UO+tUI7abZ6zQPleqPsjEk3WIaU=
|
||||
git.apihub24.de/admin/generic-di v1.4.0/go.mod h1:VcHV8MOb1qhwabHdO09CpjEg2VaDesehul86g1iyOxY=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.apihub24.de/admin/generic-di"
|
||||
di "git.apihub24.de/admin/generic-di"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -17,6 +17,7 @@ type ITranslationService interface {
|
||||
Init(files embed.FS)
|
||||
SetDefaultCulture(culture string)
|
||||
Get(key string, culture string, args ...string) string
|
||||
GetSource(culture string) map[string]string
|
||||
}
|
||||
|
||||
type translationService struct {
|
||||
@ -60,6 +61,14 @@ func (translation *translationService) Get(key string, culture string, args ...s
|
||||
return value
|
||||
}
|
||||
|
||||
func (translation *translationService) GetSource(culture string) map[string]string {
|
||||
source, err := translation.loadSource(culture)
|
||||
if err != nil {
|
||||
return make(map[string]string)
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
func (translation *translationService) loadSource(culture string) (map[string]string, error) {
|
||||
source, ok := translation.sources[culture]
|
||||
if ok {
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
package translation_test
|
||||
|
||||
import (
|
||||
di "git.apihub24.de/admin/generic-di"
|
||||
"git.apihub24.de/admin/translation"
|
||||
exampletranslations "git.apihub24.de/admin/translation/example_translations"
|
||||
"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) {
|
||||
@ -87,3 +88,21 @@ func TestTranslationService_Get_Key_With_Parameter(t *testing.T) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user