Skip to content

Commit

Permalink
Merge pull request #510 from dweymouth/feat/choose-language
Browse files Browse the repository at this point in the history
Add option to override app language detection
  • Loading branch information
dweymouth authored Dec 18, 2024
2 parents 507d633 + e9863fa commit 8dff226
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 4 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
26 changes: 25 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,30 @@ 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
} else {
log.Printf("Error loading translation file %s: %s\n", tr.TranslationFileName, err.Error())
}
}
if !success {
if err := lang.AddTranslationsFS(res.Translations, "translations"); err != nil {
log.Printf("Error loading translations: %s", err.Error())
}
}

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: "ja.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.json"},
{Name: "zhHans", DisplayName: "中文", TranslationFileName: "zhHans.json"},
{Name: "zhHant", DisplayName: "中文 (trad.)", TranslationFileName: "zhHant.json"},
}
1 change: 1 addition & 0 deletions res/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"Interview": "Interview",
"Is favorite": "Is favorite",
"Is not favorite": "Is not favorite",
"Language": "Language",
"Last played": "Last played",
"Live": "Live",
"Locally": "Locally",
Expand Down
1 change: 1 addition & 0 deletions res/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"Interview": "Entrevista",
"Is favorite": "Es favorito",
"Is not favorite": "No es favorito",
"Language": "Idioma",
"Last played": "Última reproducción",
"Live": "En vivo",
"Locally": "Localmente",
Expand Down
1 change: 1 addition & 0 deletions res/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"Interview": "Interview",
"Is favorite": "Favoris",
"Is not favorite": "Non favoris",
"Language": "Langue",
"Last played": "Dernière lecture",
"Live": "Live",
"Locally": "En local",
Expand Down
1 change: 1 addition & 0 deletions res/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"Interview": "Interview",
"Is favorite": "Nei preferiti",
"Is not favorite": "Non nei preferiti",
"Language": "Lingua",
"Last played": "Ultimo ascolto",
"Live": "Live",
"Locally": "Localmente",
Expand Down
1 change: 1 addition & 0 deletions res/translations/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"Interview": "インタビュー",
"Is favorite": "お気に入り",
"Is not favorite": "お気に入りでない",
"Language": "言語",
"Last played": "最後の再生",
"Live": "ライブ",
"Locally": "ローカル",
Expand Down
1 change: 1 addition & 0 deletions res/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"Interview": "Wywiad",
"Is favorite": "Jest ulubione",
"Is not favorite": "Nie jest ulubione",
"Language": "Język",
"Last played": "Ostatnio odtwarzane",
"Live": "Live",
"Locally": "Lokalnie",
Expand Down
1 change: 1 addition & 0 deletions res/translations/pt_BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"Interview": "Entrevista",
"Is favorite": "É favorito",
"Is not favorite": "Não é favorito",
"Language": "Linguagem",
"Last played": "Tocados recentemente",
"Live": "Ao Vivo",
"Locally": "Localmente",
Expand Down
1 change: 1 addition & 0 deletions res/translations/ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"Interview": "Interviu",
"Is favorite": "Este favorit",
"Is not favorite": "Nu este favorit",
"Language": "Limbă",
"Last played": "Ultimul redat",
"Live": "Live",
"Locally": "Local",
Expand Down
1 change: 1 addition & 0 deletions res/translations/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"Interview": "采访",
"Is favorite": "已收藏",
"Is not favorite": "未收藏",
"Language": "语言",
"Last played": "上次播放",
"Live": "现场",
"To server": "到服务器",
Expand Down
1 change: 1 addition & 0 deletions res/translations/zhHans.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"Interview": "采访",
"Is favorite": "已收藏",
"Is not favorite": "未收藏",
"Language": "语言",
"Last played": "上次播放",
"Live": "现场",
"Locally": "到本地",
Expand Down
1 change: 1 addition & 0 deletions res/translations/zhHant.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"Interview": "訪談",
"Is favorite": "已收藏",
"Is not favorite": "未收藏",
"Language": "語言",
"Last played": "上次播放",
"Live": "現場",
"Locally": "到本地",
Expand Down
26 changes: 25 additions & 1 deletion ui/dialogs/settingsdialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/dweymouth/supersonic/backend"
"github.com/dweymouth/supersonic/backend/player/mpv"
"github.com/dweymouth/supersonic/res"
myTheme "github.com/dweymouth/supersonic/ui/theme"
"github.com/dweymouth/supersonic/ui/util"
"github.com/dweymouth/supersonic/ui/widgets"
Expand Down Expand Up @@ -146,6 +147,28 @@ func (s *SettingsDialog) createGeneralTab(canSaveQueueToServer bool) *container.
if startupPage.Selected == "" {
startupPage.SetSelectedIndex(0)
}

languageList := make([]string, len(res.TranslationsInfo)+1)
languageList[0] = lang.L("Auto")
var langSelIndex int
for i, tr := range res.TranslationsInfo {
languageList[i+1] = tr.DisplayName
if tr.Name == s.config.Application.Language {
langSelIndex = i + 1
}
}

languageSelect := widget.NewSelect(languageList, nil)
languageSelect.SetSelectedIndex(langSelIndex)
languageSelect.OnChanged = func(_ string) {
lang := "auto"
if i := languageSelect.SelectedIndex(); i > 0 {
lang = res.TranslationsInfo[i-1].Name
}
s.config.Application.Language = lang
s.setRestartRequired()
}

closeToTray := widget.NewCheckWithData(lang.L("Close to system tray"),
binding.BindBool(&s.config.Application.CloseToSystemTray))
if !s.config.Application.EnableSystemTray {
Expand Down Expand Up @@ -285,8 +308,9 @@ func (s *SettingsDialog) createGeneralTab(canSaveQueueToServer bool) *container.
scrobbleEnabled.Checked = s.config.Scrobbling.Enabled

return container.NewTabItem(lang.L("General"), container.NewVBox(
container.NewHBox(widget.NewLabel(lang.L("Language")), languageSelect),
container.NewBorder(nil, nil, widget.NewLabel(lang.L("Theme")), /*left*/
container.NewHBox(widget.NewLabel("Mode"), themeModeSelect, util.NewHSpace(5)), // right
container.NewHBox(widget.NewLabel(lang.L("Mode")), themeModeSelect, util.NewHSpace(5)), // right
themeFileSelect, // center
),
container.NewHBox(
Expand Down

0 comments on commit 8dff226

Please sign in to comment.