Open labibramadhan opened 7 years ago
I can resolve by writing this:
I18nTranslations := make(map[string]map[string]string)
translations := Globals.I18n.LoadTranslations()
for lang, locales := range translations {
I18nTranslations[lang] = make(map[string]string)
for localeKey, translation := range locales {
I18nTranslations[lang][localeKey] = translation.Value
}
}
output := Globals.I18n.T(
"greeting",
"en",
I18nTranslations,
)
but it doesn't resolve for cases that the reusable translations has parameters, for example:
en:
greeting: Hello, good {{.Time}}
greetMySelf: {{.en.greeting}} for me!
that should output: Hello, good evening for me!
with arg: map[string]string{"Time": "evening"}
and it doesn't resolve for cases that the reusable translations is a nested variable, for example:
en:
greeting:
morning: Hello, good morning
evening: Hello, good evening
greetMySelf: {{.en.greeting.morning}} for me!
@labibramadhan when using i18n on an app, it's a good idea to use full strings as messages. Building up messages from other small messages has 2 main disadvantages:
That said, your idea of nesting translations is an interesting one, but as you've discovered, translations don't compose very well. A translation is not just the original message, it's the message plus substitutions (variables).
So to compose two messages into a new one, you need the new message and possible variables from any sub-messages:
en:
greeting: Hello, good {{.Time}} <= `greeting` id + `.Time` variable
greetMySelf: {{.en.greeting}} for me! <= `greetMySelf` + (`greeting` id + variables from `.en.greeting`: `.Time`)
Since the messages are translated at run-time you need to localise the messages multiple times to "get to the bottom" of the translation nesting:
en
translation for greetMySelf
.en.greeting
en
translation for greeting
<= recursion!greeting
doesn't reference other translationsAlso, since languages will reuse messages in different ways, you don't even really know which variables you will need, so you then either need to manually inspect the message nesting, or make all possible variables available.
If instead of reusing translations, you just want to include translated messages in other message, you can translate the message and provide it manually as a substitution:
en:
greeting:
morning: morning
text: Hello, good {{.Time}}
greetMySelf: {{.en.greeting}} for me!
time := t("greeting.morning")
g := t("greeting.text.", { "Time": time })
output := t("greetMySelf": { "greeting": g })
This is basically the same as my recursion example above, but you're doing it by hand instead of the library doing it for you.
Can i reuse existring translation? For example something like this: