Skip to content

Commit

Permalink
add ability to configure language in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Dec 18, 2024
1 parent 507d633 commit dd3f587
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
6 changes: 4 additions & 2 deletions backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down
22 changes: 21 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"runtime"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions res/translations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}

0 comments on commit dd3f587

Please sign in to comment.