Skip to content

Commit

Permalink
add helper i18n.ListLanguages()
Browse files Browse the repository at this point in the history
i18n.ListLanguages() returns a map, mapping language tags ('en', 'sv')
to the names of their corresponding languages (as translated by the
language itself).

This functionality will be used in the language picker, to present a
nice list of the translated languages.

I renamed testing.go to conform to go's testing conventions, and
added a test for i18n.ListLanguages().
  • Loading branch information
cblgh committed Apr 14, 2021
1 parent 9f88cab commit 5aa43b9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
1 change: 1 addition & 0 deletions web/i18n/defaults/active.en.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# default localiztion file for english
MetaLanguage = "English"

# generic terms
GenericConfirm = "Yes"
Expand Down
57 changes: 43 additions & 14 deletions web/i18n/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package i18ntesting
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 5aa43b9

Please sign in to comment.