init
This commit is contained in:
commit
a00ea001fb
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.vscode
|
||||
4
example_translations/de.json
Normal file
4
example_translations/de.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"KEY": "WERT",
|
||||
"WITH_PARAM": "WERT %[1]s_%[2]s"
|
||||
}
|
||||
4
example_translations/en.json
Normal file
4
example_translations/en.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"KEY": "VALUE",
|
||||
"WITH_PARAM": "VALUE %[1]s_%[2]s"
|
||||
}
|
||||
6
example_translations/files.go
Normal file
6
example_translations/files.go
Normal file
@ -0,0 +1,6 @@
|
||||
package exampletranslations
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed *.json
|
||||
var Files embed.FS
|
||||
4
example_translations/fr.json
Normal file
4
example_translations/fr.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"KEY": "VALEUR",
|
||||
"WITH_PARAM": "VALEUR %[1]s_%[2]s"
|
||||
}
|
||||
59
translation.go
Normal file
59
translation.go
Normal file
@ -0,0 +1,59 @@
|
||||
package translation
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var defaultCulture = "en"
|
||||
var sources = make(map[string]map[string]string)
|
||||
var translationFiles embed.FS
|
||||
|
||||
func SetDefaultCulture(culture string) {
|
||||
defaultCulture = strings.ToLower(culture)
|
||||
}
|
||||
|
||||
func Init(files embed.FS) {
|
||||
translationFiles = files
|
||||
}
|
||||
|
||||
func Get(key string, culture string, args ...string) string {
|
||||
source, err := loadSource(culture)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("unknown error: %s", err.Error())
|
||||
}
|
||||
value, ok := source[key]
|
||||
if !ok {
|
||||
return fmt.Sprintf("no value for key %s found in source %s", key, culture)
|
||||
}
|
||||
if len(args) > 0 {
|
||||
var tmpArgs = make([]any, len(args))
|
||||
for idx, arg := range args {
|
||||
tmpArgs[idx] = arg
|
||||
}
|
||||
return fmt.Sprintf(value, tmpArgs...)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func loadSource(culture string) (map[string]string, error) {
|
||||
source, ok := sources[culture]
|
||||
if ok {
|
||||
return source, nil
|
||||
}
|
||||
data, err := translationFiles.ReadFile(fmt.Sprintf("%s.json", strings.ToLower(culture)))
|
||||
if err != nil {
|
||||
data, err = translationFiles.ReadFile(fmt.Sprintf("%s.json", defaultCulture))
|
||||
if err != nil {
|
||||
return source, fmt.Errorf("can not load translation source for culture %s DefaultCulture: %s", culture, defaultCulture)
|
||||
}
|
||||
}
|
||||
err = json.Unmarshal(data, &source)
|
||||
if err != nil {
|
||||
return source, fmt.Errorf("can not parse translation source %s", culture)
|
||||
}
|
||||
sources[culture] = source
|
||||
return sources[culture], nil
|
||||
}
|
||||
72
translation_test.go
Normal file
72
translation_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
package translation_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.apihub24.de/admin/translation"
|
||||
exampletranslations "git.apihub24.de/admin/translation/example_translations"
|
||||
)
|
||||
|
||||
func Test_Translation_Init(t *testing.T) {
|
||||
translation.Init(exampletranslations.Files)
|
||||
if translation.Get("KEY", "de") != "WERT" {
|
||||
t.Errorf("translations not initialized!")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Translation_Get_de(t *testing.T) {
|
||||
translation.Init(exampletranslations.Files)
|
||||
value := translation.Get("KEY", "de")
|
||||
if value != "WERT" {
|
||||
t.Errorf("expect German KEY to have Value 'WERT' but was %s", value)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Translation_Get_en(t *testing.T) {
|
||||
translation.Init(exampletranslations.Files)
|
||||
value := translation.Get("KEY", "en")
|
||||
if value != "VALUE" {
|
||||
t.Errorf("expect English KEY to have Value 'VALUE' but was %s", value)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Translation_Get_fr(t *testing.T) {
|
||||
translation.Init(exampletranslations.Files)
|
||||
value := translation.Get("KEY", "fr")
|
||||
if value != "VALEUR" {
|
||||
t.Errorf("expect France KEY to have Value 'VALEUR' but was %s", value)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Translation_Get_Fallback_en(t *testing.T) {
|
||||
translation.Init(exampletranslations.Files)
|
||||
value := translation.Get("KEY", "notexists")
|
||||
if value != "VALUE" {
|
||||
t.Errorf("expect Fallback KEY to have Value 'VALUE' but was %s", value)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Translation_Can_Change_Fallback_Language(t *testing.T) {
|
||||
translation.Init(exampletranslations.Files)
|
||||
translation.SetDefaultCulture("de")
|
||||
value := translation.Get("KEY", "notexists")
|
||||
if value != "WERT" {
|
||||
t.Errorf("expect Fallback KEY to have Value 'WERT' but was %s", value)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Translation_Get_With_Parameter(t *testing.T) {
|
||||
translation.Init(exampletranslations.Files)
|
||||
valueDe := translation.Get("WITH_PARAM", "de", "a", "b")
|
||||
valueEn := translation.Get("WITH_PARAM", "en", "a", "b")
|
||||
valueFr := translation.Get("WITH_PARAM", "fr", "a", "b")
|
||||
if valueDe != "WERT a_b" {
|
||||
t.Errorf("expect German KEY to have Value 'WERT a_b' but was %s", valueDe)
|
||||
}
|
||||
if valueEn != "VALUE a_b" {
|
||||
t.Errorf("expect English KEY to have Value 'VALUE a_b' but was %s", valueEn)
|
||||
}
|
||||
if valueFr != "VALEUR a_b" {
|
||||
t.Errorf("expect France KEY to have Value 'VALEUR a_b' but was %s", valueFr)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user