Add validation tests to translations files

This commit is contained in:
Deluan 2020-05-04 19:54:10 -04:00
parent 8f66e87099
commit 8d99c3ab92
3 changed files with 63 additions and 10 deletions

View file

@ -27,6 +27,8 @@ const (
RequestThrottleBacklogLimit = 100
RequestThrottleBacklogTimeout = time.Minute
I18nFolder = "i18n"
)
// Cache options
@ -62,5 +64,4 @@ var (
VariousArtists = "Various Artists"
VariousArtistsID = fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(VariousArtists))))
UnknownArtist = "[Unknown Artist]"
UnknownArtistID = fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(UnknownArtist))))
)

View file

@ -4,17 +4,18 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"sync"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/resources"
"github.com/deluan/rest"
)
const i18nFolder = "i18n"
type translation struct {
ID string `json:"id"`
Name string `json:"name"`
@ -27,7 +28,8 @@ var (
)
func newTranslationRepository(context.Context) rest.Repository {
if err := loadTranslations(); err != nil {
dir := resources.AssetFile()
if err := loadTranslations(dir); err != nil {
log.Error("Error loading translation files", err)
}
return &translationRepository{}
@ -65,10 +67,10 @@ func (r *translationRepository) NewInstance() interface{} {
return &translation{}
}
func loadTranslations() (loadError error) {
func loadTranslations(fs http.FileSystem) (loadError error) {
once.Do(func() {
translations = make(map[string]translation)
dir, err := resources.AssetFile().Open(i18nFolder)
dir, err := fs.Open(consts.I18nFolder)
if err != nil {
loadError = err
return
@ -80,7 +82,7 @@ func loadTranslations() (loadError error) {
}
var languages []string
for _, f := range files {
t, err := loadTranslation(f.Name())
t, err := loadTranslation(fs, f.Name())
if err != nil {
log.Error("Error loading translation file", "file", f.Name(), err)
continue
@ -93,17 +95,18 @@ func loadTranslations() (loadError error) {
return
}
func loadTranslation(fileName string) (translation translation, err error) {
func loadTranslation(fs http.FileSystem, fileName string) (translation translation, err error) {
// Get id and full path
name := filepath.Base(fileName)
id := strings.TrimSuffix(name, filepath.Ext(name))
filePath := filepath.Join(i18nFolder, name)
filePath := filepath.Join(consts.I18nFolder, name)
// Load translation from json file
data, err := resources.Asset(filePath)
file, err := fs.Open(filePath)
if err != nil {
return
}
data, err := ioutil.ReadAll(file)
var out map[string]interface{}
if err = json.Unmarshal(data, &out); err != nil {
return

View file

@ -0,0 +1,49 @@
package app
import (
"encoding/json"
"io/ioutil"
"net/http"
"path/filepath"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/resources"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Translations", func() {
Describe("I18n files", func() {
var fs http.FileSystem
BeforeEach(func() {
fs = resources.AssetFile()
})
It("contains only valid json language files", func() {
dir, _ := fs.Open(consts.I18nFolder)
files, _ := dir.Readdir(0)
for _, f := range files {
name := filepath.Base(f.Name())
filePath := filepath.Join(consts.I18nFolder, name)
file, _ := fs.Open(filePath)
data, _ := ioutil.ReadAll(file)
var out map[string]interface{}
Expect(filepath.Ext(filePath)).To(Equal(".json"), filePath)
Expect(json.Unmarshal(data, &out)).To(BeNil(), filePath)
Expect(out["languageName"]).ToNot(BeEmpty(), filePath)
}
})
})
Describe("loadTranslation", func() {
It("loads a translation file correctly", func() {
fs := http.Dir("ui/src")
tr, err := loadTranslation(fs, "en.json")
Expect(err).To(BeNil())
Expect(tr.ID).To(Equal("en"))
Expect(tr.Name).To(Equal("English"))
var out map[string]interface{}
Expect(json.Unmarshal([]byte(tr.Data), &out)).To(BeNil())
})
})
})