navidrome/server/nativeapi/translations.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
2.8 KiB
Go
Raw Normal View History

2021-06-14 01:15:41 +02:00
package nativeapi
2020-05-02 00:29:50 +02:00
import (
"bytes"
2020-05-02 00:29:50 +02:00
"context"
"encoding/json"
"io"
2021-07-21 01:50:00 +02:00
"io/fs"
2021-10-28 04:45:34 +02:00
"path"
2020-05-02 00:29:50 +02:00
"strings"
"sync"
"github.com/deluan/rest"
"github.com/navidrome/navidrome/consts"
2020-05-02 00:29:50 +02:00
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/resources"
)
type translation struct {
ID string `json:"id"`
Name string `json:"name"`
Data string `json:"data"`
}
var (
once sync.Once
translations map[string]translation
)
func newTranslationRepository(context.Context) rest.Repository {
if err := loadTranslations(resources.FS); err != nil {
2020-05-02 00:29:50 +02:00
log.Error("Error loading translation files", err)
}
return &translationRepository{}
}
type translationRepository struct{}
func (r *translationRepository) Read(id string) (interface{}, error) {
if t, ok := translations[id]; ok {
return t, nil
}
return nil, rest.ErrNotFound
}
// Count simple implementation, does not support any `options`
func (r *translationRepository) Count(...rest.QueryOptions) (int64, error) {
2020-05-02 00:29:50 +02:00
return int64(len(translations)), nil
}
// ReadAll simple implementation, only returns IDs. Does not support any `options`
func (r *translationRepository) ReadAll(...rest.QueryOptions) (interface{}, error) {
2020-05-02 00:29:50 +02:00
var result []translation
for _, t := range translations {
t.Data = ""
result = append(result, t)
}
return result, nil
}
func (r *translationRepository) EntityName() string {
return "translation"
}
func (r *translationRepository) NewInstance() interface{} {
return &translation{}
}
2021-07-21 01:50:00 +02:00
func loadTranslations(fsys fs.FS) (loadError error) {
2020-05-02 00:29:50 +02:00
once.Do(func() {
translations = make(map[string]translation)
2021-07-21 01:50:00 +02:00
dir, err := fsys.Open(consts.I18nFolder)
2020-05-02 00:29:50 +02:00
if err != nil {
loadError = err
return
}
2021-07-21 01:50:00 +02:00
files, err := dir.(fs.ReadDirFile).ReadDir(-1)
2020-05-02 00:29:50 +02:00
if err != nil {
loadError = err
return
}
var languages []string
2020-05-02 00:29:50 +02:00
for _, f := range files {
2021-07-21 01:50:00 +02:00
t, err := loadTranslation(fsys, f.Name())
2020-05-02 00:29:50 +02:00
if err != nil {
log.Error("Error loading translation file", "file", f.Name(), err)
continue
}
translations[t.ID] = t
languages = append(languages, t.ID)
2020-05-02 00:29:50 +02:00
}
log.Info("Loading translations", "languages", languages)
2020-05-02 00:29:50 +02:00
})
return
}
2021-07-21 01:50:00 +02:00
func loadTranslation(fsys fs.FS, fileName string) (translation translation, err error) {
// Get id and full path
2021-10-28 04:45:34 +02:00
name := path.Base(fileName)
id := strings.TrimSuffix(name, path.Ext(name))
filePath := path.Join(consts.I18nFolder, name)
// Load translation from json file
2021-07-21 01:50:00 +02:00
file, err := fsys.Open(filePath)
2020-05-02 00:29:50 +02:00
if err != nil {
return
}
data, err := io.ReadAll(file)
2020-05-05 02:46:16 +02:00
if err != nil {
return
}
2020-05-02 00:29:50 +02:00
var out map[string]interface{}
if err = json.Unmarshal(data, &out); err != nil {
return
}
// Compress JSON
buf := new(bytes.Buffer)
if err = json.Compact(buf, data); err != nil {
2020-05-02 00:29:50 +02:00
return
}
translation.Data = buf.String()
translation.Name = out["languageName"].(string)
translation.ID = id
2020-05-02 00:29:50 +02:00
return
}
var _ rest.Repository = (*translationRepository)(nil)