Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #863 from gobuffalo/i18n-available-languages
Browse files Browse the repository at this point in the history
Add a way to get the list of supported languages in a Buffalo app
  • Loading branch information
markbates authored Jan 18, 2018
2 parents 1a7c88a + 667a34c commit 5ce0e31
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions middleware/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ func (t *Translator) Translate(c buffalo.Context, translationID string, args ...
return T(translationID, args...)
}

// AvailableLanguages gets the list of languages provided by the app
func (t *Translator) AvailableLanguages() []string {
return i18n.LanguageTags()
}

func defaultLanguageFinder(t *Translator, c buffalo.Context) []string {
langs := []string{}

Expand Down
11 changes: 11 additions & 0 deletions middleware/i18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func app() *buffalo.App {
app.GET("/localized", func(c buffalo.Context) error {
return c.Render(200, r.HTML("localized_view.html"))
})
app.GET("/languages-list", func(c buffalo.Context) error {
return c.Render(200, r.JSON(t.AvailableLanguages()))
})
// Disable i18n middleware
noI18n := func(c buffalo.Context) error {
return c.Render(200, r.HTML("localized_view.html"))
Expand Down Expand Up @@ -149,3 +152,11 @@ func Test_i18n_collision(t *testing.T) {
res := w.Request("/collision").Get()
r.Equal("Collision OK", strings.TrimSpace(res.Body.String()))
}

func Test_i18n_availableLanguages(t *testing.T) {
r := require.New(t)

w := willie.New(app())
res := w.Request("/languages-list").Get()
r.Equal("[\"en-us\",\"fr-fr\"]", strings.TrimSpace(res.Body.String()))
}

0 comments on commit 5ce0e31

Please sign in to comment.