From dd3f5874c437061a51c2bbd847d916713f115e05 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Tue, 17 Dec 2024 18:07:28 -0800 Subject: [PATCH] add ability to configure language in config file --- backend/config.go | 6 ++++-- main.go | 22 +++++++++++++++++++++- res/translations.go | 20 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/backend/config.go b/backend/config.go index 4399e222..26263eda 100644 --- a/backend/config.go +++ b/backend/config.go @@ -48,7 +48,8 @@ type AppConfig struct { ShowTrackChangeNotification bool EnableLrcLib bool SkipSSLVerify bool - EnqueueBatchSize int + EnqueueBatchSize int + Language string // Experimental - may be removed in future FontNormalTTF string @@ -174,7 +175,8 @@ func DefaultConfig(appVersionTag string) *Config { ShowTrackChangeNotification: false, EnableLrcLib: true, SkipSSLVerify: false, - EnqueueBatchSize: 100, + EnqueueBatchSize: 100, + Language: "auto", }, AlbumPage: AlbumPageConfig{ TracklistColumns: []string{"Artist", "Time", "Plays", "Favorite", "Rating"}, diff --git a/main.go b/main.go index d9eb4d4c..7da1c446 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "log" "os" "runtime" + "slices" "sync" "time" @@ -46,7 +47,26 @@ func main() { os.Setenv("FYNE_SCALE", "1.1") } - lang.AddTranslationsFS(res.Translations, "translations") + // load configured app language, or all otherwise + lIdx := slices.IndexFunc(res.TranslationsInfo, func(t res.TranslationInfo) bool { + return t.Name == myApp.Config.Application.Language + }) + success := false + if lIdx > 0 { + tr := res.TranslationsInfo[lIdx] + content, err := res.Translations.ReadFile("translations/" + tr.TranslationFileName) + if err == nil { + // "trick" Fyne into loading translations for configured language + // by pretending it's the translation for the system locale + name := lang.SystemLocale().LanguageString() + lang.AddTranslations(fyne.NewStaticResource(name+".json", content)) + success = true + } + } + if !success { + lang.AddTranslationsFS(res.Translations, "translations") + } + fyneApp := app.New() fyneApp.SetIcon(res.ResAppicon256Png) diff --git a/res/translations.go b/res/translations.go index fd6a93fe..a2398e6c 100644 --- a/res/translations.go +++ b/res/translations.go @@ -4,3 +4,23 @@ import "embed" //go:embed translations var Translations embed.FS + +type TranslationInfo struct { + Name string + DisplayName string + TranslationFileName string +} + +var TranslationsInfo = []TranslationInfo{ + {Name: "de", DisplayName: "Deutsch", TranslationFileName: "de.json"}, + {Name: "en", DisplayName: "English", TranslationFileName: "en.json"}, + {Name: "es", DisplayName: "Español", TranslationFileName: "es.json"}, + {Name: "fr", DisplayName: "François", TranslationFileName: "fr.json"}, + {Name: "it", DisplayName: "Italiano", TranslationFileName: "it.json"}, + {Name: "ja", DisplayName: "日本語", TranslationFileName: "de.json"}, + {Name: "pl", DisplayName: "Polski", TranslationFileName: "pl.json"}, + {Name: "pt_BR", DisplayName: "Português (BR)", TranslationFileName: "pt_BR.json"}, + {Name: "ro", DisplayName: "Română", TranslationFileName: "ro"}, + {Name: "zhHans", DisplayName: "中文", TranslationFileName: "zhHans.json"}, + {Name: "zhHant", DisplayName: "中文 (trad.)", TranslationFileName: "zhHant.json"}, +}