1
0
Fork 0
miniflux/locale/translator.go

45 lines
1.1 KiB
Go
Raw Normal View History

2017-11-20 00:10:04 -05:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package locale
import (
"encoding/json"
"fmt"
"strings"
)
// Translator manage supported locales.
2017-11-20 00:10:04 -05:00
type Translator struct {
locales Locales
2017-11-20 00:10:04 -05:00
}
// AddLanguage loads a new language into the system.
2017-11-20 00:10:04 -05:00
func (t *Translator) AddLanguage(language, translations string) error {
var decodedTranslations Translation
decoder := json.NewDecoder(strings.NewReader(translations))
if err := decoder.Decode(&decodedTranslations); err != nil {
return fmt.Errorf("Invalid JSON file: %v", err)
}
t.locales[language] = decodedTranslations
2017-11-20 00:10:04 -05:00
return nil
}
// GetLanguage returns the given language handler.
2017-11-20 00:10:04 -05:00
func (t *Translator) GetLanguage(language string) *Language {
translations, found := t.locales[language]
2017-11-20 00:10:04 -05:00
if !found {
return &Language{language: language}
}
return &Language{language: language, translations: translations}
}
// NewTranslator creates a new Translator.
2017-11-20 00:10:04 -05:00
func NewTranslator() *Translator {
return &Translator{locales: make(Locales)}
2017-11-20 00:10:04 -05:00
}