Skip to content

Commit

Permalink
Merge pull request fyne-io#4685 from andydotxyz/feature/translate
Browse files Browse the repository at this point in the history
Prepare toolkit for translations
  • Loading branch information
andydotxyz authored Mar 1, 2024
2 parents 62c5269 + ca2bdd6 commit 4f9d61b
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 25 deletions.
7 changes: 4 additions & 3 deletions dialog/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
col "fyne.io/fyne/v2/internal/color"
"fyne.io/fyne/v2/lang"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
Expand Down Expand Up @@ -105,15 +106,15 @@ func (p *ColorPickerDialog) updateUI() {
if w := p.win; w != nil {
w.Hide()
}
p.dialog.dismiss = &widget.Button{Text: "Cancel", Icon: theme.CancelIcon(),
p.dialog.dismiss = &widget.Button{Text: lang.L("Cancel"), Icon: theme.CancelIcon(),
OnTapped: p.dialog.Hide,
}
if p.Advanced {
p.picker = newColorAdvancedPicker(p.color, func(c color.Color) {
p.color = c
})

advancedItem := widget.NewAccordionItem("Advanced", p.picker)
advancedItem := widget.NewAccordionItem(lang.L("Advanced"), p.picker)
if p.advanced != nil {
advancedItem.Open = p.advanced.Items[0].Open
}
Expand All @@ -129,7 +130,7 @@ func (p *ColorPickerDialog) updateUI() {
p.advanced,
)

confirm := &widget.Button{Text: "Confirm", Icon: theme.ConfirmIcon(), Importance: widget.HighImportance,
confirm := &widget.Button{Text: lang.L("Confirm"), Icon: theme.ConfirmIcon(), Importance: widget.HighImportance,
OnTapped: func() {
p.selectColor(p.color)
},
Expand Down
5 changes: 3 additions & 2 deletions dialog/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dialog
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/lang"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
Expand Down Expand Up @@ -33,10 +34,10 @@ func (d *ConfirmDialog) SetConfirmImportance(importance widget.Importance) {
func NewConfirm(title, message string, callback func(bool), parent fyne.Window) *ConfirmDialog {
d := newDialog(title, message, theme.QuestionIcon(), callback, parent)

d.dismiss = &widget.Button{Text: "No", Icon: theme.CancelIcon(),
d.dismiss = &widget.Button{Text: lang.L("No"), Icon: theme.CancelIcon(),
OnTapped: d.Hide,
}
confirm := &widget.Button{Text: "Yes", Icon: theme.ConfirmIcon(), Importance: widget.HighImportance,
confirm := &widget.Button{Text: lang.L("Yes"), Icon: theme.ConfirmIcon(), Importance: widget.HighImportance,
OnTapped: func() {
d.hideWithResponse(true)
},
Expand Down
3 changes: 2 additions & 1 deletion dialog/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dialog

import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/lang"
"fyne.io/fyne/v2/widget"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func (i *EntryDialog) SetOnClosed(callback func()) {
func NewEntryDialog(title, message string, onConfirm func(string), parent fyne.Window) *EntryDialog {
i := &EntryDialog{entry: widget.NewEntry()}
items := []*widget.FormItem{widget.NewFormItem(message, i.entry)}
i.FormDialog = NewForm(title, "Ok", "Cancel", items, func(ok bool) {
i.FormDialog = NewForm(title, lang.L("Ok"), lang.L("Cancel"), items, func(ok bool) {
// User has confirmed and entered an input
if ok && onConfirm != nil {
onConfirm(i.entry.Text)
Expand Down
19 changes: 10 additions & 9 deletions dialog/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/lang"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/storage/repository"
"fyne.io/fyne/v2/theme"
Expand Down Expand Up @@ -99,15 +100,15 @@ func (f *fileDialog) makeUI() fyne.CanvasObject {
f.open.Enable()
}
}
saveName.SetPlaceHolder("Enter filename")
saveName.SetPlaceHolder(lang.L("Enter filename"))
f.fileName = saveName
} else {
f.fileName = widget.NewLabel("")
}

label := "Open"
label := lang.L("Open")
if f.file.save {
label = "Save"
label = lang.L("Save")
}
if f.file.confirmText != "" {
label = f.file.confirmText
Expand All @@ -118,7 +119,7 @@ func (f *fileDialog) makeUI() fyne.CanvasObject {
f.fileName.SetText(f.initialFileName)
}

dismissLabel := "Cancel"
dismissLabel := lang.L("Cancel")
if f.file.dismissText != "" {
dismissLabel = f.file.dismissText
}
Expand All @@ -133,9 +134,9 @@ func (f *fileDialog) makeUI() fyne.CanvasObject {

f.breadcrumb = container.NewHBox()
f.breadcrumbScroll = container.NewHScroll(container.NewPadded(f.breadcrumb))
title := label + " File"
title := label + " " + lang.L("File")
if f.file.isDirectory() {
title = label + " Folder"
title = label + " " + lang.L("Folder")
}

view := viewLayout(fyne.CurrentApp().Preferences().Int(viewLayoutKey))
Expand Down Expand Up @@ -188,9 +189,9 @@ func (f *fileDialog) makeUI() fyne.CanvasObject {

newFolderButton := widget.NewButtonWithIcon("", theme.FolderNewIcon(), func() {
newFolderEntry := widget.NewEntry()
ShowForm("New Folder", "Create Folder", "Cancel", []*widget.FormItem{
ShowForm(lang.L("New Folder"), lang.L("Create Folder"), lang.L("Cancel"), []*widget.FormItem{
{
Text: "Name",
Text: lang.X("file.name", "Name"),
Widget: newFolderEntry,
},
}, func(s bool) {
Expand Down Expand Up @@ -326,7 +327,7 @@ func (f *fileDialog) makeDismissButton(label string) *widget.Button {
}

func (f *fileDialog) optionsMenu(position fyne.Position, buttonSize fyne.Size) {
hiddenFiles := widget.NewCheck("Show Hidden Files", func(changed bool) {
hiddenFiles := widget.NewCheck(lang.L("Show Hidden Files"), func(changed bool) {
f.showHidden = changed
f.refreshDir(f.dir)
})
Expand Down
5 changes: 3 additions & 2 deletions dialog/fileitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/lang"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
Expand Down Expand Up @@ -49,7 +50,7 @@ func (i *fileDialogItem) setLocation(l fyne.URI, dir, up bool) {
}

if up {
i.name = "(Parent)"
i.name = "(" + lang.X("file.parent", "Parent") + ")"
}

i.Refresh()
Expand All @@ -69,7 +70,7 @@ func (f *fileDialog) newFileItem(location fyne.URI, dir, up bool) *fileDialogIte
}

if up {
item.name = "(Parent)"
item.name = "(" + lang.X("file.parent", "Parent") + ")"
}

item.ExtendBaseWidget(item)
Expand Down
5 changes: 3 additions & 2 deletions dialog/information.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package dialog
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/lang"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)

func createTextDialog(title, message string, icon fyne.Resource, parent fyne.Window) Dialog {
d := newDialog(title, message, icon, nil, parent)

d.dismiss = &widget.Button{Text: "OK",
d.dismiss = &widget.Button{Text: lang.L("OK"),
OnTapped: d.Hide,
}
d.create(container.NewGridWithColumns(1, d.dismiss))
Expand All @@ -35,7 +36,7 @@ func ShowInformation(title, message string, parent fyne.Window) {
// The message is extracted from the provided error (should not be nil).
// After creation you should call Show().
func NewError(err error, parent fyne.Window) Dialog {
return createTextDialog("Error", err.Error(), theme.ErrorIcon(), parent)
return createTextDialog(lang.L("Error"), err.Error(), theme.ErrorIcon(), parent)
}

// ShowError shows a dialog over the specified window for an application error.
Expand Down
6 changes: 4 additions & 2 deletions internal/driver/glfw/driver_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/painter"
"fyne.io/fyne/v2/internal/svg"
"fyne.io/fyne/v2/lang"
"fyne.io/systray"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -188,15 +189,16 @@ func (d *gLDriver) catchTerm() {
}

func addMissingQuitForMenu(menu *fyne.Menu, d *gLDriver) {
localQuit := lang.L("Quit")
var lastItem *fyne.MenuItem
if len(menu.Items) > 0 {
lastItem = menu.Items[len(menu.Items)-1]
if lastItem.Label == "Quit" {
if lastItem.Label == localQuit {
lastItem.IsQuit = true
}
}
if lastItem == nil || !lastItem.IsQuit { // make sure the menu always has a quit option
quitItem := fyne.NewMenuItem("Quit", nil)
quitItem := fyne.NewMenuItem(localQuit, nil)
quitItem.IsQuit = true
menu.Items = append(menu.Items, fyne.NewMenuItemSeparator(), quitItem)
}
Expand Down
3 changes: 2 additions & 1 deletion lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"strings"
"text/template"

"fyne.io/fyne/v2"
"github.com/jeandeaual/go-locale"
"github.com/nicksnyder/go-i18n/v2/i18n"

"fyne.io/fyne/v2"

"golang.org/x/text/language"
)

Expand Down
26 changes: 26 additions & 0 deletions lang/translations/base.en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"Advanced": "Advanced",
"Cancel": "Cancel",
"Confirm": "Confirm",
"Create Folder": "Create Folder",
"Enter filename": "Enter filename",
"Error": "Error",
"Favourites": "Favourites",
"File": "File",
"Folder": "Folder",
"New Folder": "New Folder",
"No": "No",
"OK": "OK",
"Open": "Open",
"Quit": "Quit",
"Save": "Save",
"Show Hidden Files": "Show Hidden Files",
"Yes": "Yes",

"file.name": {
"other": "Name"
},
"file.parent": {
"other": "Parent"
}
}
3 changes: 0 additions & 3 deletions lang/translations/base.template.json

This file was deleted.

3 changes: 3 additions & 0 deletions locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "strings"
// Since: 2.5
type Locale string

// LanguageString returns a version of the local without the script portion.
// For example "en" or "fr-FR".
func (l Locale) LanguageString() string {
count := strings.Count(string(l), "-")
if count < 2 {
Expand All @@ -17,6 +19,7 @@ func (l Locale) LanguageString() string {
return string(l)[:pos]
}

// String returns the complete locale as a standard string.
func (l Locale) String() string {
return string(l)
}

0 comments on commit 4f9d61b

Please sign in to comment.