Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix pagination for i18n #13

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions controller.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package i18n

import (
"fmt"

"github.com/qor/admin"
"github.com/qor/qor/utils"
)

type i18nController struct {
Expand All @@ -15,7 +16,12 @@ func (controller *i18nController) Index(context *admin.Context) {

func (controller *i18nController) Update(context *admin.Context) {
form := context.Request.Form
translation := Translation{Key: form.Get("Key"), Locale: form.Get("Locale"), Value: utils.HTMLSanitizer.Sanitize(form.Get("Value"))}
translation := Translation{Key: form.Get("Key"), Locale: form.Get("Locale"), Value: form.Get("Value")}

if !controller.validateLocale(&translation) {
context.Writer.WriteHeader(400)
fmt.Fprintf(context.Writer, "requested locale '%s' is unexpected", translation.Locale)
}

if err := controller.I18n.SaveTranslation(&translation); err == nil {
context.Writer.Write([]byte("OK"))
Expand All @@ -24,3 +30,12 @@ func (controller *i18nController) Update(context *admin.Context) {
context.Writer.Write([]byte(err.Error()))
}
}

func (controller *i18nController) validateLocale(translation *Translation) (ok bool) {
for locale := range controller.LoadTranslations() {
if locale == translation.Locale {
ok = true
}
}
return
}
103 changes: 62 additions & 41 deletions i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"html/template"
"io/ioutil"
"net/http"
"path/filepath"
"sort"
"strconv"
Expand All @@ -32,6 +31,8 @@ type I18n struct {
FallbackLocales map[string][]string
fallbackLocales []string
cacheStore cache.CacheStoreInterface

locales []string
}

// ResourceName change display name in qor admin
Expand All @@ -58,6 +59,9 @@ type Translation struct {
func New(backends ...Backend) *I18n {
i18n := &I18n{Backends: backends, cacheStore: memory.New()}
i18n.loadToCacheStore()
for locale := range i18n.FallbackLocales {
i18n.locales = append(i18n.locales, locale)
}
return i18n
}

Expand Down Expand Up @@ -179,11 +183,13 @@ func (i18n *I18n) T(locale, key string, args ...interface{}) template.HTML {
}
}

if translation.Value != "" {
value = translation.Value
} else {
value = key
}
value = translation.Value //values is a value, even if it is empty

// if translation.Value != "" {
// value = translation.Value
// } else {
// value = key
// }

if str, err := cldr.Parse(locale, value, args...); err == nil {
value = str
Expand Down Expand Up @@ -240,38 +246,46 @@ func getLocaleFromContext(context *qor.Context) string {
return Default
}

type availableLocalesInterface interface {
AvailableLocales() []string
}

type viewableLocalesInterface interface {
ViewableLocales() []string
// type availableLocalesInterface interface {
// AvailableLocales() []string
// }

// type viewableLocalesInterface interface {
// ViewableLocales() []string
// }

// type editableLocalesInterface interface {
// EditableLocales() []string
// }

// func getAvailableLocales(req *http.Request, currentUser qor.CurrentUser) []string {
// if user, ok := currentUser.(viewableLocalesInterface); ok {
// return user.ViewableLocales()
// }

// if user, ok := currentUser.(availableLocalesInterface); ok {
// return user.AvailableLocales()
// }
// return []string{Default}
// }

// func getEditableLocales(req *http.Request, currentUser qor.CurrentUser) []string {
// if user, ok := currentUser.(editableLocalesInterface); ok {
// return user.EditableLocales()
// }

// if user, ok := currentUser.(availableLocalesInterface); ok {
// return user.AvailableLocales()
// }
// return []string{Default}
// }

func (i18n *I18n) Locales() []string {
return i18n.locales
}

type editableLocalesInterface interface {
EditableLocales() []string
}

func getAvailableLocales(req *http.Request, currentUser qor.CurrentUser) []string {
if user, ok := currentUser.(viewableLocalesInterface); ok {
return user.ViewableLocales()
}

if user, ok := currentUser.(availableLocalesInterface); ok {
return user.AvailableLocales()
}
return []string{Default}
}

func getEditableLocales(req *http.Request, currentUser qor.CurrentUser) []string {
if user, ok := currentUser.(editableLocalesInterface); ok {
return user.EditableLocales()
}

if user, ok := currentUser.(availableLocalesInterface); ok {
return user.AvailableLocales()
}
return []string{Default}
func (i18n *I18n) SetLocales(locales []string) {
i18n.locales = locales
}

// ConfigureQorResource configure qor resource for qor admin
Expand All @@ -286,8 +300,8 @@ func (i18n *I18n) ConfigureQorResource(res resource.Resourcer) {
if locale := context.Request.Form.Get("primary_locale"); locale != "" {
return locale
}
if availableLocales := getAvailableLocales(context.Request, context.CurrentUser); len(availableLocales) > 0 {
return availableLocales[0]
if len(i18n.locales) > 0 {
return i18n.locales[0]
}
return ""
}
Expand Down Expand Up @@ -322,6 +336,7 @@ func (i18n *I18n) ConfigureQorResource(res resource.Resourcer) {
for key, translation := range translations {
if (keyword == "") || (strings.Index(strings.ToLower(translation.Key), keyword) != -1 ||
strings.Index(strings.ToLower(translation.Value), keyword) != -1) {

if _, ok := matchedTranslations[key]; !ok {
var t = matchedTranslation{
Key: key,
Expand All @@ -336,6 +351,12 @@ func (i18n *I18n) ConfigureQorResource(res resource.Resourcer) {
}
}

if localeTranslations, ok := translationsMap[editingLocale]; ok {
if v, ok := localeTranslations[key]; ok {
t.EditingValue = v.Value
}
}

matchedTranslations[key] = t
keys = append(keys, key)
}
Expand Down Expand Up @@ -365,7 +386,7 @@ func (i18n *I18n) ConfigureQorResource(res resource.Resourcer) {
}

if pagination.CurrentPage > 0 {
pagination.Pages = pagination.Total / pagination.PerPage
pagination.Pages = pagination.Total/pagination.PerPage + 1
}

context.Searcher.Pagination = pagination
Expand Down Expand Up @@ -396,11 +417,11 @@ func (i18n *I18n) ConfigureQorResource(res resource.Resourcer) {
res.GetAdmin().RegisterFuncMap("i18n_editing_locale", getEditingLocale)

res.GetAdmin().RegisterFuncMap("i18n_viewable_locales", func(context admin.Context) []string {
return getAvailableLocales(context.Request, context.CurrentUser)
return i18n.locales
})

res.GetAdmin().RegisterFuncMap("i18n_editable_locales", func(context admin.Context) []string {
return getEditableLocales(context.Request, context.CurrentUser)
return i18n.locales
})

controller := i18nController{i18n}
Expand Down