Make sure the package locale pass golint
This commit is contained in:
parent
db77e55119
commit
f072439b79
5 changed files with 24 additions and 13 deletions
|
@ -116,5 +116,5 @@ func main() {
|
||||||
generateFile("js", "static", "Javascript", "server/static/js/*.js", "server/static/js.go")
|
generateFile("js", "static", "Javascript", "server/static/js/*.js", "server/static/js.go")
|
||||||
generateFile("none", "template", "templateViewsMap", "server/template/html/*.html", "server/template/views.go")
|
generateFile("none", "template", "templateViewsMap", "server/template/html/*.html", "server/template/views.go")
|
||||||
generateFile("none", "template", "templateCommonMap", "server/template/html/common/*.html", "server/template/common.go")
|
generateFile("none", "template", "templateCommonMap", "server/template/html/common/*.html", "server/template/common.go")
|
||||||
generateFile("none", "locale", "Translations", "locale/translations/*.json", "locale/translations.go")
|
generateFile("none", "locale", "translations", "locale/translations/*.json", "locale/translations.go")
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,11 +6,13 @@ package locale
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
|
// Language represents a language in the system.
|
||||||
type Language struct {
|
type Language struct {
|
||||||
language string
|
language string
|
||||||
translations Translation
|
translations Translation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get fetch the translation for the given key.
|
||||||
func (l *Language) Get(key string, args ...interface{}) string {
|
func (l *Language) Get(key string, args ...interface{}) string {
|
||||||
var translation string
|
var translation string
|
||||||
|
|
||||||
|
@ -24,11 +26,12 @@ func (l *Language) Get(key string, args ...interface{}) string {
|
||||||
return fmt.Sprintf(translation, args...)
|
return fmt.Sprintf(translation, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Plural returns the translation of the given key by using the language plural form.
|
||||||
func (l *Language) Plural(key string, n int, args ...interface{}) string {
|
func (l *Language) Plural(key string, n int, args ...interface{}) string {
|
||||||
translation := key
|
translation := key
|
||||||
slices, found := l.translations[key]
|
slices, found := l.translations[key]
|
||||||
if found {
|
|
||||||
|
|
||||||
|
if found {
|
||||||
pluralForm, found := pluralForms[l.language]
|
pluralForm, found := pluralForms[l.language]
|
||||||
if !found {
|
if !found {
|
||||||
pluralForm = pluralForms["default"]
|
pluralForm = pluralForms["default"]
|
||||||
|
|
|
@ -6,16 +6,19 @@ package locale
|
||||||
|
|
||||||
import "log"
|
import "log"
|
||||||
|
|
||||||
|
// Translation is the translation mapping table.
|
||||||
type Translation map[string]interface{}
|
type Translation map[string]interface{}
|
||||||
|
|
||||||
|
// Locales represents locales supported by the system.
|
||||||
type Locales map[string]Translation
|
type Locales map[string]Translation
|
||||||
|
|
||||||
|
// Load prepare the locale system by loading all translations.
|
||||||
func Load() *Translator {
|
func Load() *Translator {
|
||||||
translator := NewTranslator()
|
translator := NewTranslator()
|
||||||
|
|
||||||
for language, translations := range Translations {
|
for language, tr := range translations {
|
||||||
log.Println("Loading translation:", language)
|
log.Println("Loading translation:", language)
|
||||||
translator.AddLanguage(language, translations)
|
translator.AddLanguage(language, tr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return translator
|
return translator
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
// Code generated by go generate; DO NOT EDIT.
|
// Code generated by go generate; DO NOT EDIT.
|
||||||
// 2017-11-25 10:47:30.371865884 -0800 PST m=+0.027993990
|
// 2017-11-25 15:50:52.572283626 -0800 PST m=+0.030941705
|
||||||
|
|
||||||
package locale
|
package locale
|
||||||
|
|
||||||
var Translations = map[string]string{
|
var translations = map[string]string{
|
||||||
"en_US": `{
|
"en_US": `{
|
||||||
"plural.feed.error_count": [
|
"plural.feed.error_count": [
|
||||||
"%d error",
|
"%d error",
|
||||||
|
@ -149,12 +149,13 @@ var Translations = map[string]string{
|
||||||
"Sign in with Google": "Se connecter avec Google",
|
"Sign in with Google": "Se connecter avec Google",
|
||||||
"Unlink my Google account": "Dissocier mon compte Google",
|
"Unlink my Google account": "Dissocier mon compte Google",
|
||||||
"Link my Google account": "Associer mon compte Google",
|
"Link my Google account": "Associer mon compte Google",
|
||||||
"Category not found for this user.": "Cette catégorie n'existe pas pour cet utilisateur."
|
"Category not found for this user.": "Cette catégorie n'existe pas pour cet utilisateur.",
|
||||||
|
"Invalid theme.": "Le thème est invalide."
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
var TranslationsChecksums = map[string]string{
|
var translationsChecksums = map[string]string{
|
||||||
"en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897",
|
"en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897",
|
||||||
"fr_FR": "92b360ebbfae5c243897c05f7869e6bef0129a2c8275b6802fbaba0d05410e91",
|
"fr_FR": "48622d4796fe4a461221565d84f52e22fb167a44a870b08ba32887897bdfbb1a",
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,12 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Translator manage supported locales.
|
||||||
type Translator struct {
|
type Translator struct {
|
||||||
Locales Locales
|
locales Locales
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddLanguage loads a new language into the system.
|
||||||
func (t *Translator) AddLanguage(language, translations string) error {
|
func (t *Translator) AddLanguage(language, translations string) error {
|
||||||
var decodedTranslations Translation
|
var decodedTranslations Translation
|
||||||
|
|
||||||
|
@ -22,12 +24,13 @@ func (t *Translator) AddLanguage(language, translations string) error {
|
||||||
return fmt.Errorf("Invalid JSON file: %v", err)
|
return fmt.Errorf("Invalid JSON file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Locales[language] = decodedTranslations
|
t.locales[language] = decodedTranslations
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLanguage returns the given language handler.
|
||||||
func (t *Translator) GetLanguage(language string) *Language {
|
func (t *Translator) GetLanguage(language string) *Language {
|
||||||
translations, found := t.Locales[language]
|
translations, found := t.locales[language]
|
||||||
if !found {
|
if !found {
|
||||||
return &Language{language: language}
|
return &Language{language: language}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +38,7 @@ func (t *Translator) GetLanguage(language string) *Language {
|
||||||
return &Language{language: language, translations: translations}
|
return &Language{language: language, translations: translations}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewTranslator creates a new Translator.
|
||||||
func NewTranslator() *Translator {
|
func NewTranslator() *Translator {
|
||||||
return &Translator{Locales: make(Locales)}
|
return &Translator{locales: make(Locales)}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue