Skip to content

Commit e239a23

Browse files
committed
Make it translatable
1 parent 9a2d02b commit e239a23

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

main.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,37 @@ package main
22

33
import (
44
"LargeCsvReader/widgets"
5+
"embed"
56
"encoding/csv"
67
"fyne.io/fyne/v2"
78
"fyne.io/fyne/v2/app"
89
"fyne.io/fyne/v2/container"
910
"fyne.io/fyne/v2/dialog"
11+
"fyne.io/fyne/v2/lang"
1012
"fyne.io/fyne/v2/layout"
1113
"fyne.io/fyne/v2/storage"
1214
"fyne.io/fyne/v2/widget"
1315
"io"
1416
"os"
1517
)
1618

19+
//go:embed translation
20+
var translations embed.FS
21+
1722
func showPreviewWindow(filePath string, fyneApp fyne.App) {
18-
window := fyneApp.NewWindow("Preview")
23+
window := fyneApp.NewWindow(lang.X("app.preview", "Preview"))
1924
window.Resize(fyne.NewSize(640, 480))
2025

2126
typeToCharMap := map[string]rune{
22-
"Comma": ',',
23-
"Semicolon": ';',
24-
"Tab": '\t',
27+
lang.X("app.separator.comma", "Comma"): ',',
28+
lang.X("app.separator.semicolon", "Semicolon"): ';',
29+
lang.X("app.separator.tab", "Tab"): '\t',
30+
}
31+
separatorOptions := make([]string, len(typeToCharMap))
32+
i := 0
33+
for key := range typeToCharMap {
34+
separatorOptions[i] = key
35+
i++
2536
}
2637

2738
file, err := os.Open(filePath)
@@ -31,7 +42,7 @@ func showPreviewWindow(filePath string, fyneApp fyne.App) {
3142
var csvFile *csv.Reader
3243
rows := make([][]string, 5)
3344

34-
previewText := widget.NewLabel("Preview")
45+
previewText := widget.NewLabel(lang.X("app.preview", "Preview"))
3546
previewText.Hide()
3647

3748
tableContainer := container.NewStack()
@@ -54,7 +65,7 @@ func showPreviewWindow(filePath string, fyneApp fyne.App) {
5465
cell.SetText(rows[id.Row][id.Col])
5566
cell.OnRightClick = func(event *fyne.PointEvent) {
5667
items := []*fyne.MenuItem{
57-
fyne.NewMenuItem("Copy", func() {
68+
fyne.NewMenuItem(lang.X("app.copy_to_clipboard", "Copy"), func() {
5869
window.Clipboard().SetContent(cell.Text)
5970
}),
6071
}
@@ -69,7 +80,7 @@ func showPreviewWindow(filePath string, fyneApp fyne.App) {
6980
}
7081

7182
var loadMoreButton *widget.Button
72-
loadMoreButton = widget.NewButton("Load more", func() {
83+
loadMoreButton = widget.NewButton(lang.X("app.load_more_button", "Load more"), func() {
7384
for range 5 {
7485
line, err := csvFile.Read()
7586
if err == io.EOF {
@@ -87,7 +98,7 @@ func showPreviewWindow(filePath string, fyneApp fyne.App) {
8798
})
8899
loadMoreButton.Hide()
89100

90-
separatorSelect := widget.NewSelect([]string{"Comma", "Semicolon", "Tab"}, func(selected string) {
101+
separatorSelect := widget.NewSelect(separatorOptions, func(selected string) {
91102
csvFile = csv.NewReader(file)
92103
csvFile.LazyQuotes = true
93104
csvFile.Comma = typeToCharMap[selected]
@@ -125,7 +136,7 @@ func showPreviewWindow(filePath string, fyneApp fyne.App) {
125136

126137
mainContainer := container.NewBorder(
127138
container.NewVBox(
128-
widget.NewLabel("Separator"),
139+
widget.NewLabel(lang.X("app.separator", "Separator")),
129140
separatorSelect,
130141
lineSeparator1,
131142
container.NewHBox(
@@ -153,7 +164,12 @@ func showPreviewWindow(filePath string, fyneApp fyne.App) {
153164

154165
func main() {
155166
fyneApp := app.NewWithID("1af17320-d9ef-4d2b-aae1-8226b14a177a")
156-
window := fyneApp.NewWindow("Large CSV Reader")
167+
err := lang.AddTranslationsFS(translations, "translation")
168+
if err != nil {
169+
panic(err)
170+
}
171+
172+
window := fyneApp.NewWindow(lang.X("app.title", "Large CSV Reader"))
157173
window.Resize(fyne.NewSize(640, 480))
158174
window.SetMaster()
159175

@@ -166,11 +182,11 @@ func main() {
166182

167183
window.SetContent(container.NewVBox(
168184
container.NewCenter(
169-
widget.NewLabel("Using the button below choose a CSV you want to open"),
185+
widget.NewLabel(lang.X("app.choose_csv_button.description", "Using the button below choose a CSV you want to open")),
170186
),
171187
container.New(
172188
layout.NewCustomPaddedLayout(8, 0, 32, 32),
173-
widget.NewButton("Open CSV", func() {
189+
widget.NewButton(lang.X("app.choose_csv_button", "Open CSV"), func() {
174190
openDialog.Show()
175191
}),
176192
),

translation/cs.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"app.preview": "Náhled",
3+
"app.separator.comma": "Čárka",
4+
"app.separator.semicolon": "Středník",
5+
"app.separator.tab": "Tabulátor",
6+
"app.copy_to_clipboard": "Zkopírovat",
7+
"app.load_more_button": "Načíst více",
8+
"app.separator": "Oddělovač",
9+
"app.choose_csv_button.description": "Pomocí tlačítka níže zvolte CSV soubor, který chcete načíst",
10+
"app.choose_csv_button": "Otevřít CSV"
11+
}

0 commit comments

Comments
 (0)