From 3e0e49ffaa01998b8a357ef0bd453309c74ec580 Mon Sep 17 00:00:00 2001 From: sg3des Date: Thu, 5 Jul 2018 23:24:27 +0300 Subject: [PATCH 1/5] fix search return localized values --- i18n.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/i18n.go b/i18n.go index be054e5..199259c 100644 --- a/i18n.go +++ b/i18n.go @@ -322,6 +322,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, @@ -336,6 +337,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) } @@ -365,7 +372,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 From d020d496cc7f254230a21ea1d39213b7fee4ad5f Mon Sep 17 00:00:00 2001 From: sg3des Date: Tue, 31 Jul 2018 00:06:59 +0300 Subject: [PATCH 2/5] drop sanitizer --- controller.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/controller.go b/controller.go index 6a2a45b..bc8ea29 100644 --- a/controller.go +++ b/controller.go @@ -2,7 +2,6 @@ package i18n import ( "github.com/qor/admin" - "github.com/qor/qor/utils" ) type i18nController struct { @@ -15,7 +14,7 @@ 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 err := controller.I18n.SaveTranslation(&translation); err == nil { context.Writer.Write([]byte("OK")) From 07fb53cb6de6e0be036607d5ee2c74370add7828 Mon Sep 17 00:00:00 2001 From: sg3des Date: Mon, 13 Aug 2018 14:33:13 +0300 Subject: [PATCH 3/5] key is not displayed there is a value is empty --- i18n.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/i18n.go b/i18n.go index 199259c..09bcf03 100644 --- a/i18n.go +++ b/i18n.go @@ -179,11 +179,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 From a7bbc2bf2da4ea7e2e75ee777925683b3afa3e32 Mon Sep 17 00:00:00 2001 From: sg3des Date: Tue, 21 Aug 2018 19:23:38 +0300 Subject: [PATCH 4/5] validate locale --- controller.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/controller.go b/controller.go index bc8ea29..bdf9a08 100644 --- a/controller.go +++ b/controller.go @@ -1,6 +1,8 @@ package i18n import ( + "fmt" + "github.com/qor/admin" ) @@ -16,6 +18,11 @@ func (controller *i18nController) Update(context *admin.Context) { form := context.Request.Form 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")) } else { @@ -23,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 +} From 0ca720b9c8e424b502d2b25cc906c69e4a046531 Mon Sep 17 00:00:00 2001 From: sg3des Date: Tue, 9 Oct 2018 15:20:38 +0300 Subject: [PATCH 5/5] changed resolving locales from a qor.User by self field --- i18n.go | 82 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 35 deletions(-) diff --git a/i18n.go b/i18n.go index 09bcf03..6029b5d 100644 --- a/i18n.go +++ b/i18n.go @@ -5,7 +5,6 @@ import ( "fmt" "html/template" "io/ioutil" - "net/http" "path/filepath" "sort" "strconv" @@ -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 @@ -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 } @@ -242,38 +246,46 @@ func getLocaleFromContext(context *qor.Context) string { return Default } -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} +// 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 } -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 @@ -288,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 "" } @@ -405,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}