diff --git a/web/i18n/defaults/active.en.toml b/web/i18n/defaults/active.en.toml index 5f8927b..8a60bf1 100644 --- a/web/i18n/defaults/active.en.toml +++ b/web/i18n/defaults/active.en.toml @@ -1,4 +1,5 @@ # default localiztion file for english +MetaLanguage = "English" # generic terms GenericConfirm = "Yes" diff --git a/web/i18n/helper.go b/web/i18n/helper.go index 20fb32f..44bc1e4 100644 --- a/web/i18n/helper.go +++ b/web/i18n/helper.go @@ -22,11 +22,11 @@ import ( ) type Helper struct { - bundle *i18n.Bundle + bundle *i18n.Bundle + languages map[string]string } func New(r repo.Interface) (*Helper, error) { - bundle := i18n.NewBundle(language.English) bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) @@ -114,22 +114,36 @@ func New(r repo.Interface) (*Helper, error) { return nil, fmt.Errorf("i18n: failed to iterate localizations: %w", err) } - return &Helper{bundle: bundle}, nil + // create a mapping of language tags to the translated language names + langmap := listLanguages(bundle) + return &Helper{bundle: bundle, languages: langmap}, nil } -func (h Helper) GetRenderFuncs() []render.Option { - var opts = []render.Option{ - render.InjectTemplateFunc("i18npl", func(r *http.Request) interface{} { - loc := h.FromRequest(r) - return loc.LocalizePlurals - }), +func listLanguages(bundle *i18n.Bundle) map[string]string { + langmap := make(map[string]string) - render.InjectTemplateFunc("i18n", func(r *http.Request) interface{} { - loc := h.FromRequest(r) - return loc.LocalizeSimple - }), + for _, langTag := range bundle.LanguageTags() { + var l Localizer + l.loc = i18n.NewLocalizer(bundle, langTag.String()) + + msg, err := l.loc.Localize(&i18n.LocalizeConfig{ + MessageID: "MetaLanguage", + }) + if err != nil { + msg = langTag.String() + } + + langmap[langTag.String()] = msg } - return opts + + return langmap +} + +// ListLanguages returns a mapping between the room's translated languages. +// The keys are language tags (as strings) and the values are the name of the language tag, as translated in the original language. +// For example: en -> English, sv -> Svenska, de -> Deutsch +func (h Helper) ListLanguages() map[string]string { + return h.languages } type Localizer struct { @@ -153,6 +167,21 @@ func (h Helper) FromRequest(r *http.Request) *Localizer { return h.newLocalizer(lang, accept) } +func (h Helper) GetRenderFuncs() []render.Option { + var opts = []render.Option{ + render.InjectTemplateFunc("i18npl", func(r *http.Request) interface{} { + loc := h.FromRequest(r) + return loc.LocalizePlurals + }), + + render.InjectTemplateFunc("i18n", func(r *http.Request) interface{} { + loc := h.FromRequest(r) + return loc.LocalizeSimple + }), + } + return opts +} + func (l Localizer) LocalizeSimple(messageID string) string { msg, err := l.loc.Localize(&i18n.LocalizeConfig{ MessageID: messageID, diff --git a/web/i18n/i18ntesting/testing.go b/web/i18n/i18ntesting/i18n_test.go similarity index 85% rename from web/i18n/i18ntesting/testing.go rename to web/i18n/i18ntesting/i18n_test.go index 30a7819..cedecb2 100644 --- a/web/i18n/i18ntesting/testing.go +++ b/web/i18n/i18ntesting/i18n_test.go @@ -3,6 +3,7 @@ package i18ntesting import ( "bytes" "fmt" + "github.com/stretchr/testify/assert" "io/ioutil" "os" "path/filepath" @@ -66,7 +67,17 @@ func justTheKeys(t *testing.T) []byte { return buf.Bytes() } -func WriteReplacement(t *testing.T) { +func TestListAllLanguages(t *testing.T) { + r := repo.New(filepath.Join("testrun", t.Name())) + a := assert.New(t) + helper, err := i18n.New(r) + a.NoError(err) + t.Log(helper) + langmap := helper.ListLanguages() + a.Equal(langmap["en"], "English") +} + +func TestWriteReplacement(t *testing.T) { r := repo.New(filepath.Join("testrun", t.Name())) testOverride := filepath.Join(r.GetPath("i18n"), "active.en.toml")