Fix i18n file errors
- Fixed an error in the spanish and chinese translation file - Refactored reading the translation files Instead of ignoring errors in the non-default language files the error will now be returned and the remaining files will not be loaded. - Added a unit test to check if all translation files are valid - Added an i18n config to specify the translations dir and the default language
Cette révision appartient à :
Parent
d654e4fb10
révision
6e546facc0
7 fichiers modifiés avec 70 ajouts et 21 suppressions
|
@ -21,7 +21,7 @@ type Config struct {
|
|||
DBType string `json:"db_type"`
|
||||
// DBParams will be directly passed to Gorm, and its internal
|
||||
// structure depends on the dialect for each db type
|
||||
DBParams string `json:"db_params"`
|
||||
DBParams string `json:"db_params"`
|
||||
DBLogMode string `json:"db_logmode"`
|
||||
// tracker scraper config (required)
|
||||
Scrape ScraperConfig `json:"scraper"`
|
||||
|
@ -29,11 +29,13 @@ type Config struct {
|
|||
Cache CacheConfig `json:"cache"`
|
||||
// search config
|
||||
Search SearchConfig `json:"search"`
|
||||
// internationalization config
|
||||
I18n I18nConfig `json:"i18n"`
|
||||
// optional i2p configuration
|
||||
I2P *I2PConfig `json:"i2p"`
|
||||
}
|
||||
|
||||
var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50", "default", DefaultScraperConfig, DefaultCacheConfig, DefaultSearchConfig, nil}
|
||||
var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50", "default", DefaultScraperConfig, DefaultCacheConfig, DefaultSearchConfig, DefaultI18nConfig, nil}
|
||||
|
||||
var allowedDatabaseTypes = map[string]bool{
|
||||
"sqlite3": true,
|
||||
|
@ -57,6 +59,7 @@ func New() *Config {
|
|||
config.DBLogMode = Defaults.DBLogMode
|
||||
config.Scrape = Defaults.Scrape
|
||||
config.Cache = Defaults.Cache
|
||||
config.I18n = Defaults.I18n
|
||||
return &config
|
||||
}
|
||||
|
||||
|
|
11
config/i18n.go
Fichier normal
11
config/i18n.go
Fichier normal
|
@ -0,0 +1,11 @@
|
|||
package config
|
||||
|
||||
type I18nConfig struct {
|
||||
TranslationsDirectory string `json:"translations_directory"`
|
||||
DefaultLanguage string `json:"default_language"`
|
||||
}
|
||||
|
||||
var DefaultI18nConfig = I18nConfig{
|
||||
TranslationsDirectory: "translations",
|
||||
DefaultLanguage: "en-us", // TODO: Remove refs to "en-us" from the code and templates
|
||||
}
|
19
main.go
19
main.go
|
@ -6,7 +6,6 @@ import (
|
|||
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/ewhal/nyaa/cache"
|
||||
|
@ -15,23 +14,12 @@ import (
|
|||
"github.com/ewhal/nyaa/network"
|
||||
"github.com/ewhal/nyaa/router"
|
||||
"github.com/ewhal/nyaa/service/scraper"
|
||||
"github.com/ewhal/nyaa/util/languages"
|
||||
"github.com/ewhal/nyaa/util/log"
|
||||
"github.com/ewhal/nyaa/util/search"
|
||||
"github.com/ewhal/nyaa/util/signals"
|
||||
"github.com/nicksnyder/go-i18n/i18n"
|
||||
)
|
||||
|
||||
func initI18N() {
|
||||
/* Initialize the languages translation */
|
||||
i18n.MustLoadTranslationFile("translations/en-us.all.json")
|
||||
paths, err := filepath.Glob("translations/*.json")
|
||||
if err == nil {
|
||||
for _, path := range paths {
|
||||
i18n.LoadTranslationFile(path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RunServer runs webapp mainloop
|
||||
func RunServer(conf *config.Config) {
|
||||
http.Handle("/", router.Router)
|
||||
|
@ -122,7 +110,10 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
initI18N()
|
||||
err = languages.InitI18n(conf.I18n)
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
err = cache.Configure(&conf.Cache)
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
|
|
|
@ -352,7 +352,7 @@
|
|||
"translation": "Todas las Categorías"
|
||||
},
|
||||
{
|
||||
"id:": "select_a_torrent_category",
|
||||
"id": "select_a_torrent_category",
|
||||
"translation": "Selecciona una Categoría para el Torrent"
|
||||
},
|
||||
{
|
||||
|
|
|
@ -352,7 +352,7 @@
|
|||
"translation": "所有分类"
|
||||
},
|
||||
{
|
||||
"id:": "select_a_torrent_category",
|
||||
"id": "select_a_torrent_category",
|
||||
"translation": "请选择种子分类"
|
||||
},
|
||||
{
|
||||
|
|
|
@ -2,13 +2,40 @@ package languages
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ewhal/nyaa/config"
|
||||
"github.com/ewhal/nyaa/service/user"
|
||||
"github.com/nicksnyder/go-i18n/i18n"
|
||||
"github.com/nicksnyder/go-i18n/i18n/language"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Initialize the languages translation
|
||||
func InitI18n(conf config.I18nConfig) error {
|
||||
defaultFilepath := path.Join(conf.TranslationsDirectory, conf.DefaultLanguage+".all.json")
|
||||
err := i18n.LoadTranslationFile(defaultFilepath)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to load default translation file '%s': %v", defaultFilepath, err))
|
||||
}
|
||||
|
||||
paths, err := filepath.Glob(path.Join(conf.TranslationsDirectory, "*.json"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get translation files: %v", err)
|
||||
}
|
||||
|
||||
for _, path := range paths {
|
||||
err := i18n.LoadTranslationFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load translation file '%s': %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// When go-i18n finds a language with >0 translations, it uses it as the Tfunc
|
||||
// However, if said language has a missing translation, it won't fallback to the "main" language
|
||||
func TfuncAndLanguageWithFallback(language string, languages ...string) (i18n.TranslateFunc, *language.Language, error) {
|
||||
|
@ -81,7 +108,6 @@ func GetTfuncAndLanguageFromRequest(r *http.Request, defaultLanguage string) (T
|
|||
return
|
||||
}
|
||||
|
||||
|
||||
func SetTranslationFromRequest(tmpl *template.Template, r *http.Request, defaultLanguage string) i18n.TranslateFunc {
|
||||
r.Header.Add("Vary", "Accept-Encoding")
|
||||
T, _ := GetTfuncAndLanguageFromRequest(r, defaultLanguage)
|
||||
|
|
18
util/languages/translation_test.go
Fichier normal
18
util/languages/translation_test.go
Fichier normal
|
@ -0,0 +1,18 @@
|
|||
package languages
|
||||
|
||||
import (
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/ewhal/nyaa/config"
|
||||
)
|
||||
|
||||
func TestInitI18n(t *testing.T) {
|
||||
conf := config.DefaultI18nConfig
|
||||
conf.TranslationsDirectory = path.Join("..", "..", conf.TranslationsDirectory)
|
||||
|
||||
err := InitI18n(conf)
|
||||
if err != nil {
|
||||
t.Errorf("failed to initialize language translations: %v", err)
|
||||
}
|
||||
}
|
Référencer dans un nouveau ticket