1
0
Fork 0
forked from External/ergo
This commit is contained in:
Shivaram Lingamneni 2019-02-19 02:54:57 -05:00
parent 234d011c29
commit c6b9fe0218
7 changed files with 224 additions and 243 deletions

View file

@ -4,10 +4,25 @@
package languages
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"gopkg.in/yaml.v2"
)
const (
// for a language (e.g., `fi-FI`) to be supported
// it must have a metadata file named, e.g., `fi-FI.lang.yaml`
metadataFileSuffix = ".lang.yaml"
)
var (
stringsFileSuffixes = []string{"-irc.lang.json", "-help.lang.json", "-nickserv.lang.json", "-hostserv.lang.json", "-chanserv.lang.json"}
)
// LangData is the data contained in a language file.
@ -16,76 +31,144 @@ type LangData struct {
Code string
Contributors string
Incomplete bool
Translations map[string]string
}
// Manager manages our languages and provides translation abilities.
type Manager struct {
sync.RWMutex
Info map[string]LangData
Languages map[string]LangData
translations map[string]map[string]string
defaultLang string
}
// NewManager returns a new Manager.
func NewManager(defaultLang string, languageData map[string]LangData) *Manager {
lm := Manager{
Info: make(map[string]LangData),
func NewManager(enabled bool, path string, defaultLang string) (lm *Manager, err error) {
lm = &Manager{
Languages: make(map[string]LangData),
translations: make(map[string]map[string]string),
defaultLang: defaultLang,
}
// make fake "en" info
lm.Info["en"] = LangData{
lm.Languages["en"] = LangData{
Code: "en",
Name: "English",
Contributors: "Oragono contributors and the IRC community",
}
// load language data
for name, data := range languageData {
lm.Info[name] = data
// make sure we don't include empty translations
lm.translations[name] = make(map[string]string)
for key, value := range data.Translations {
if strings.TrimSpace(value) == "" {
continue
if enabled {
err = lm.loadData(path)
if err == nil {
// successful load, check that defaultLang is sane
_, ok := lm.Languages[lm.defaultLang]
if !ok {
err = fmt.Errorf("Cannot find default language [%s]", lm.defaultLang)
}
lm.translations[name][key] = value
}
} else {
lm.defaultLang = "en"
}
return &lm
return
}
func (lm *Manager) loadData(path string) (err error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return
}
// 1. for each language that has a ${langcode}.lang.yaml in the languages path
// 2. load ${langcode}.lang.yaml
// 3. load ${langcode}-irc.lang.json and friends as the translations
for _, f := range files {
if f.IsDir() {
continue
}
// glob up *.lang.yaml in the directory
name := f.Name()
if !strings.HasSuffix(name, metadataFileSuffix) {
continue
}
prefix := strings.TrimSuffix(name, metadataFileSuffix)
// load, e.g., `zh-CN.lang.yaml`
var data []byte
data, err = ioutil.ReadFile(filepath.Join(path, name))
if err != nil {
return
}
var langInfo LangData
err = yaml.Unmarshal(data, &langInfo)
if err != nil {
return err
}
if langInfo.Code == "en" {
return fmt.Errorf("Cannot have language file with code 'en' (this is the default language using strings inside the server code). If you're making an English variant, name it with a more specific code")
}
// check for duplicate languages
_, exists := lm.Languages[strings.ToLower(langInfo.Code)]
if exists {
return fmt.Errorf("Language code [%s] defined twice", langInfo.Code)
}
// slurp up all translation files with `prefix` into a single translation map
translations := make(map[string]string)
for _, translationSuffix := range stringsFileSuffixes {
stringsFilePath := filepath.Join(path, prefix+translationSuffix)
data, err = ioutil.ReadFile(stringsFilePath)
if err != nil {
continue // skip missing paths
}
var tlList map[string]string
err = json.Unmarshal(data, &tlList)
if err != nil {
return fmt.Errorf("invalid json for translation file %s: %s", stringsFilePath, err.Error())
}
for key, value := range tlList {
// because of how crowdin works, this is how we skip untranslated lines
if key == value || strings.TrimSpace(value) == "" {
continue
}
translations[key] = value
}
}
if len(translations) == 0 {
// skip empty translations
continue
}
// sanity check the language definition from the yaml file
if langInfo.Code == "" || langInfo.Name == "" || langInfo.Contributors == "" {
return fmt.Errorf("Code, name or contributors is empty in language file [%s]", name)
}
key := strings.ToLower(langInfo.Code)
lm.Languages[key] = langInfo
lm.translations[key] = translations
}
return nil
}
// Default returns the default languages.
func (lm *Manager) Default() []string {
lm.RLock()
defer lm.RUnlock()
if lm.defaultLang == "" {
return []string{}
}
return []string{lm.defaultLang}
}
// Count returns how many languages we have.
func (lm *Manager) Count() int {
lm.RLock()
defer lm.RUnlock()
return len(lm.Info)
return len(lm.Languages)
}
// Translators returns the languages we have and the translators.
func (lm *Manager) Translators() []string {
lm.RLock()
defer lm.RUnlock()
var tlist sort.StringSlice
for _, info := range lm.Info {
for _, info := range lm.Languages {
if info.Code == "en" {
continue
}
@ -98,12 +181,9 @@ func (lm *Manager) Translators() []string {
// Codes returns the proper language codes for the given casefolded language codes.
func (lm *Manager) Codes(codes []string) []string {
lm.RLock()
defer lm.RUnlock()
var newCodes []string
for _, code := range codes {
info, exists := lm.Info[code]
info, exists := lm.Languages[code]
if exists {
newCodes = append(newCodes, info.Code)
}
@ -123,9 +203,6 @@ func (lm *Manager) Translate(languages []string, originalString string) string {
return originalString
}
lm.RLock()
defer lm.RUnlock()
for _, lang := range languages {
lang = strings.ToLower(lang)
if lang == "en" {
@ -149,3 +226,18 @@ func (lm *Manager) Translate(languages []string, originalString string) string {
// didn't find any translation
return originalString
}
func (lm *Manager) CapValue() string {
langCodes := make([]string, len(lm.Languages)+1)
langCodes[0] = strconv.Itoa(len(lm.Languages))
i := 1
for _, info := range lm.Languages {
codeToken := info.Code
if info.Incomplete {
codeToken = "~" + info.Code
}
langCodes[i] = codeToken
i += 1
}
return strings.Join(langCodes, ",")
}