Basic i18n setup

This commit is contained in:
Philipp Wolfer 2023-12-09 11:46:32 +01:00
parent 20f1732858
commit a41318d822
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
10 changed files with 700 additions and 9 deletions

View file

@ -24,6 +24,7 @@ import (
"github.com/spf13/viper"
"go.uploadedlobster.com/scotty/internal/backends"
"go.uploadedlobster.com/scotty/internal/config"
"go.uploadedlobster.com/scotty/internal/i18n"
"go.uploadedlobster.com/scotty/internal/models"
"go.uploadedlobster.com/scotty/internal/storage"
)
@ -84,7 +85,7 @@ func (c *TransferCmd[E, I, R]) resolveBackends(source string, target string) err
}
func (c *TransferCmd[E, I, R]) Transfer(exp backends.ExportProcessor[R], imp backends.ImportProcessor[R]) error {
fmt.Printf("Transferring %s from %s to %s...\n", c.entity, c.sourceName, c.targetName)
fmt.Println(i18n.Tr("Transferring %s from %s to %s...", c.entity, c.sourceName, c.targetName))
// Authenticate backends, if needed
config := viper.GetViper()
@ -103,7 +104,7 @@ func (c *TransferCmd[E, I, R]) Transfer(exp backends.ExportProcessor[R], imp bac
if err != nil {
return err
}
fmt.Printf("From timestamp: %v (%v)\n", timestamp, timestamp.Unix())
fmt.Println(i18n.Tr("From timestamp: %v (%v)", timestamp, timestamp.Unix()))
// Prepare progress bars
exportProgress := make(chan models.Progress)
@ -123,11 +124,12 @@ func (c *TransferCmd[E, I, R]) Transfer(exp backends.ExportProcessor[R], imp bac
wg.Wait()
progress.Wait()
if result.Error != nil {
fmt.Printf("Import failed, last reported timestamp was %v (%v)\n", result.LastTimestamp, result.LastTimestamp.Unix())
fmt.Println(i18n.Tr("Import failed, last reported timestamp was %v (%v)",
result.LastTimestamp, result.LastTimestamp.Unix()))
return result.Error
}
fmt.Printf("Imported %v of %v %s into %v.\n",
result.ImportCount, result.TotalCount, c.entity, c.targetName)
fmt.Println(i18n.Tr("Imported %v of %v %s into %v.",
result.ImportCount, result.TotalCount, c.entity, c.targetName))
// Update timestamp
err = c.updateTimestamp(result, timestamp)
@ -137,9 +139,10 @@ func (c *TransferCmd[E, I, R]) Transfer(exp backends.ExportProcessor[R], imp bac
// Print errors
if len(result.ImportErrors) > 0 {
fmt.Printf("\nDuring the import the following errors occurred:\n")
fmt.Println()
fmt.Println(i18n.Tr("During the import the following errors occurred:"))
for _, err := range result.ImportErrors {
fmt.Printf("Error: %v\n", err)
fmt.Println(i18n.Tr("Error: %v\n", err))
}
}
@ -159,7 +162,7 @@ func (c *TransferCmd[E, I, R]) updateTimestamp(result models.ImportResult, oldTi
if result.LastTimestamp.Unix() < oldTimestamp.Unix() {
result.LastTimestamp = oldTimestamp
}
fmt.Printf("Latest timestamp: %v (%v)\n", result.LastTimestamp, result.LastTimestamp.Unix())
fmt.Println(i18n.Tr("Latest timestamp: %v (%v)\n", result.LastTimestamp, result.LastTimestamp.Unix()))
err := c.db.SetImportTimestamp(c.sourceName, c.targetName, c.entity, result.LastTimestamp)
return err
}

39
internal/i18n/i18n.go Normal file
View file

@ -0,0 +1,39 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package i18n
import (
"log"
"github.com/Xuanwo/go-locale"
_ "go.uploadedlobster.com/scotty/internal/translations"
"golang.org/x/text/message"
)
var localizer Localizer
func init() {
tag, err := locale.Detect()
if err != nil {
log.Fatal(err)
}
localizer = New(tag)
}
func Tr(key message.Reference, args ...interface{}) string {
return localizer.Translate(key, args...)
}

View file

@ -0,0 +1,37 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package i18n
import (
"golang.org/x/text/language"
"golang.org/x/text/message"
)
type Localizer struct {
printer *message.Printer
}
// Create a new Localizer for a language tag
func New(lang language.Tag) Localizer {
return Localizer{
printer: message.NewPrinter(lang),
}
}
// Return the translated string, with variables replaced.
func (l Localizer) Translate(key message.Reference, args ...interface{}) string {
return l.printer.Sprintf(key, args...)
}

View file

@ -0,0 +1,75 @@
// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
package translations
import (
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/message/catalog"
)
type dictionary struct {
index []uint32
data string
}
func (d *dictionary) Lookup(key string) (data string, ok bool) {
p, ok := messageKeyToIndex[key]
if !ok {
return "", false
}
start, end := d.index[p], d.index[p+1]
if start == end {
return "", false
}
return d.data[start:end], true
}
func init() {
dict := map[string]catalog.Dictionary{
"de": &dictionary{index: deIndex, data: deData},
"en": &dictionary{index: enIndex, data: enData},
}
fallback := language.MustParse("en")
cat, err := catalog.NewFromMap(dict, catalog.Fallback(fallback))
if err != nil {
panic(err)
}
message.DefaultCatalog = cat
}
var messageKeyToIndex = map[string]int{
"During the import the following errors occurred:": 4,
"Error: %v\n": 5,
"From timestamp: %v (%v)": 1,
"Import failed, last reported timestamp was %v (%v)": 2,
"Imported %v of %v %s into %v.": 3,
"Latest timestamp: %v (%v)\n": 6,
"Transferring %s from %s to %s...": 0,
}
var deIndex = []uint32{ // 8 elements
0x00000000, 0x00000029, 0x00000047, 0x00000087,
0x000000b2, 0x000000e9, 0x000000fc, 0x00000125,
} // Size: 56 bytes
const deData string = "" + // Size: 293 bytes
"\x02Übertrage %[1]s von %[2]s nach %[3]s...\x02Ab Zeitstempel: %[1]v (%[" +
"2]v)\x02Import fehlgeschlagen, der letzte Zeitstempel war %[1]v (%[2]v)" +
"\x02%[1]v von %[2]v %[3]s in %[4]v importiert.\x02Während des Imports si" +
"nd folgende Fehler aufgetreten:\x04\x00\x01\x0a\x0e\x02Fehler: %[1]v\x04" +
"\x00\x01\x0a$\x02Neuester Zeitstempel: %[1]v (%[2]v)"
var enIndex = []uint32{ // 8 elements
0x00000000, 0x0000002a, 0x00000048, 0x00000081,
0x000000ab, 0x000000dc, 0x000000ee, 0x00000113,
} // Size: 56 bytes
const enData string = "" + // Size: 275 bytes
"\x02Transferring %[1]s from %[2]s to %[3]s...\x02From timestamp: %[1]v (" +
"%[2]v)\x02Import failed, last reported timestamp was %[1]v (%[2]v)\x02Im" +
"ported %[1]v of %[2]v %[3]s into %[4]v.\x02During the import the followi" +
"ng errors occurred:\x04\x00\x01\x0a\x0d\x02Error: %[1]v\x04\x00\x01\x0a " +
"\x02Latest timestamp: %[1]v (%[2]v)"
// Total table size 680 bytes (0KiB); checksum: D0CBF9F5

View file

@ -0,0 +1,164 @@
{
"language": "de",
"messages": [
{
"id": "Transferring {Entity} from {SourceName} to {TargetName}...",
"message": "Transferring {Entity} from {SourceName} to {TargetName}...",
"translation": "Übertrage {Entity} von {SourceName} nach {TargetName}...",
"placeholders": [
{
"id": "Entity",
"string": "%[1]s",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "c.entity"
},
{
"id": "SourceName",
"string": "%[2]s",
"type": "string",
"underlyingType": "string",
"argNum": 2,
"expr": "c.sourceName"
},
{
"id": "TargetName",
"string": "%[3]s",
"type": "string",
"underlyingType": "string",
"argNum": 3,
"expr": "c.targetName"
}
]
},
{
"id": "From timestamp: {Timestamp} ({Unix})",
"message": "From timestamp: {Timestamp} ({Unix})",
"translation": "Ab Zeitstempel: {Timestamp} ({Unix})",
"placeholders": [
{
"id": "Timestamp",
"string": "%[1]v",
"type": "time.Time",
"underlyingType": "struct{wall uint64; ext int64; loc *time.Location}",
"argNum": 1,
"expr": "timestamp"
},
{
"id": "Unix",
"string": "%[2]v",
"type": "int64",
"underlyingType": "int64",
"argNum": 2,
"expr": "timestamp.Unix()"
}
]
},
{
"id": "Import failed, last reported timestamp was {LastTimestamp} ({Unix})",
"message": "Import failed, last reported timestamp was {LastTimestamp} ({Unix})",
"translation": "Import fehlgeschlagen, der letzte Zeitstempel war {LastTimestamp} ({Unix})",
"placeholders": [
{
"id": "LastTimestamp",
"string": "%[1]v",
"type": "time.Time",
"underlyingType": "struct{wall uint64; ext int64; loc *time.Location}",
"argNum": 1,
"expr": "result.LastTimestamp"
},
{
"id": "Unix",
"string": "%[2]v",
"type": "int64",
"underlyingType": "int64",
"argNum": 2,
"expr": "result.LastTimestamp.Unix()"
}
]
},
{
"id": "Imported {ImportCount} of {TotalCount} {Entity} into {TargetName}.",
"message": "Imported {ImportCount} of {TotalCount} {Entity} into {TargetName}.",
"translation": "{ImportCount} von {TotalCount} {Entity} in {TargetName} importiert.",
"placeholders": [
{
"id": "ImportCount",
"string": "%[1]v",
"type": "int",
"underlyingType": "int",
"argNum": 1,
"expr": "result.ImportCount"
},
{
"id": "TotalCount",
"string": "%[2]v",
"type": "int",
"underlyingType": "int",
"argNum": 2,
"expr": "result.TotalCount"
},
{
"id": "Entity",
"string": "%[3]s",
"type": "string",
"underlyingType": "string",
"argNum": 3,
"expr": "c.entity"
},
{
"id": "TargetName",
"string": "%[4]v",
"type": "string",
"underlyingType": "string",
"argNum": 4,
"expr": "c.targetName"
}
]
},
{
"id": "During the import the following errors occurred:",
"message": "During the import the following errors occurred:",
"translation": "Während des Imports sind folgende Fehler aufgetreten:"
},
{
"id": "Error: {Err}",
"message": "Error: {Err}",
"translation": "Fehler: {Err}",
"placeholders": [
{
"id": "Err",
"string": "%[1]v",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "err"
}
]
},
{
"id": "Latest timestamp: {LastTimestamp} ({Unix})",
"message": "Latest timestamp: {LastTimestamp} ({Unix})",
"translation": "Neuester Zeitstempel: {LastTimestamp} ({Unix})",
"placeholders": [
{
"id": "LastTimestamp",
"string": "%[1]v",
"type": "time.Time",
"underlyingType": "struct{wall uint64; ext int64; loc *time.Location}",
"argNum": 1,
"expr": "result.LastTimestamp"
},
{
"id": "Unix",
"string": "%[2]v",
"type": "int64",
"underlyingType": "int64",
"argNum": 2,
"expr": "result.LastTimestamp.Unix()"
}
]
}
]
}

View file

@ -0,0 +1,164 @@
{
"language": "de",
"messages": [
{
"id": "Transferring {Entity} from {SourceName} to {TargetName}...",
"message": "Transferring {Entity} from {SourceName} to {TargetName}...",
"translation": "Übertrage {Entity} von {SourceName} nach {TargetName}...",
"placeholders": [
{
"id": "Entity",
"string": "%[1]s",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "c.entity"
},
{
"id": "SourceName",
"string": "%[2]s",
"type": "string",
"underlyingType": "string",
"argNum": 2,
"expr": "c.sourceName"
},
{
"id": "TargetName",
"string": "%[3]s",
"type": "string",
"underlyingType": "string",
"argNum": 3,
"expr": "c.targetName"
}
]
},
{
"id": "From timestamp: {Timestamp} ({Unix})",
"message": "From timestamp: {Timestamp} ({Unix})",
"translation": "Ab Zeitstempel: {Timestamp} ({Unix})",
"placeholders": [
{
"id": "Timestamp",
"string": "%[1]v",
"type": "time.Time",
"underlyingType": "struct{wall uint64; ext int64; loc *time.Location}",
"argNum": 1,
"expr": "timestamp"
},
{
"id": "Unix",
"string": "%[2]v",
"type": "int64",
"underlyingType": "int64",
"argNum": 2,
"expr": "timestamp.Unix()"
}
]
},
{
"id": "Import failed, last reported timestamp was {LastTimestamp} ({Unix})",
"message": "Import failed, last reported timestamp was {LastTimestamp} ({Unix})",
"translation": "Import fehlgeschlagen, der letzte Zeitstempel war {LastTimestamp} ({Unix})",
"placeholders": [
{
"id": "LastTimestamp",
"string": "%[1]v",
"type": "time.Time",
"underlyingType": "struct{wall uint64; ext int64; loc *time.Location}",
"argNum": 1,
"expr": "result.LastTimestamp"
},
{
"id": "Unix",
"string": "%[2]v",
"type": "int64",
"underlyingType": "int64",
"argNum": 2,
"expr": "result.LastTimestamp.Unix()"
}
]
},
{
"id": "Imported {ImportCount} of {TotalCount} {Entity} into {TargetName}.",
"message": "Imported {ImportCount} of {TotalCount} {Entity} into {TargetName}.",
"translation": "{ImportCount} von {TotalCount} {Entity} in {TargetName} importiert.",
"placeholders": [
{
"id": "ImportCount",
"string": "%[1]v",
"type": "int",
"underlyingType": "int",
"argNum": 1,
"expr": "result.ImportCount"
},
{
"id": "TotalCount",
"string": "%[2]v",
"type": "int",
"underlyingType": "int",
"argNum": 2,
"expr": "result.TotalCount"
},
{
"id": "Entity",
"string": "%[3]s",
"type": "string",
"underlyingType": "string",
"argNum": 3,
"expr": "c.entity"
},
{
"id": "TargetName",
"string": "%[4]v",
"type": "string",
"underlyingType": "string",
"argNum": 4,
"expr": "c.targetName"
}
]
},
{
"id": "During the import the following errors occurred:",
"message": "During the import the following errors occurred:",
"translation": "Während des Imports sind folgende Fehler aufgetreten:"
},
{
"id": "Error: {Err}",
"message": "Error: {Err}",
"translation": "Fehler: {Err}",
"placeholders": [
{
"id": "Err",
"string": "%[1]v",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "err"
}
]
},
{
"id": "Latest timestamp: {LastTimestamp} ({Unix})",
"message": "Latest timestamp: {LastTimestamp} ({Unix})",
"translation": "Neuester Zeitstempel: {LastTimestamp} ({Unix})",
"placeholders": [
{
"id": "LastTimestamp",
"string": "%[1]v",
"type": "time.Time",
"underlyingType": "struct{wall uint64; ext int64; loc *time.Location}",
"argNum": 1,
"expr": "result.LastTimestamp"
},
{
"id": "Unix",
"string": "%[2]v",
"type": "int64",
"underlyingType": "int64",
"argNum": 2,
"expr": "result.LastTimestamp.Unix()"
}
]
}
]
}

View file

@ -0,0 +1,178 @@
{
"language": "en",
"messages": [
{
"id": "Transferring {Entity} from {SourceName} to {TargetName}...",
"message": "Transferring {Entity} from {SourceName} to {TargetName}...",
"translation": "Transferring {Entity} from {SourceName} to {TargetName}...",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "Entity",
"string": "%[1]s",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "c.entity"
},
{
"id": "SourceName",
"string": "%[2]s",
"type": "string",
"underlyingType": "string",
"argNum": 2,
"expr": "c.sourceName"
},
{
"id": "TargetName",
"string": "%[3]s",
"type": "string",
"underlyingType": "string",
"argNum": 3,
"expr": "c.targetName"
}
],
"fuzzy": true
},
{
"id": "From timestamp: {Timestamp} ({Unix})",
"message": "From timestamp: {Timestamp} ({Unix})",
"translation": "From timestamp: {Timestamp} ({Unix})",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "Timestamp",
"string": "%[1]v",
"type": "time.Time",
"underlyingType": "struct{wall uint64; ext int64; loc *time.Location}",
"argNum": 1,
"expr": "timestamp"
},
{
"id": "Unix",
"string": "%[2]v",
"type": "int64",
"underlyingType": "int64",
"argNum": 2,
"expr": "timestamp.Unix()"
}
],
"fuzzy": true
},
{
"id": "Import failed, last reported timestamp was {LastTimestamp} ({Unix})",
"message": "Import failed, last reported timestamp was {LastTimestamp} ({Unix})",
"translation": "Import failed, last reported timestamp was {LastTimestamp} ({Unix})",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "LastTimestamp",
"string": "%[1]v",
"type": "time.Time",
"underlyingType": "struct{wall uint64; ext int64; loc *time.Location}",
"argNum": 1,
"expr": "result.LastTimestamp"
},
{
"id": "Unix",
"string": "%[2]v",
"type": "int64",
"underlyingType": "int64",
"argNum": 2,
"expr": "result.LastTimestamp.Unix()"
}
],
"fuzzy": true
},
{
"id": "Imported {ImportCount} of {TotalCount} {Entity} into {TargetName}.",
"message": "Imported {ImportCount} of {TotalCount} {Entity} into {TargetName}.",
"translation": "Imported {ImportCount} of {TotalCount} {Entity} into {TargetName}.",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "ImportCount",
"string": "%[1]v",
"type": "int",
"underlyingType": "int",
"argNum": 1,
"expr": "result.ImportCount"
},
{
"id": "TotalCount",
"string": "%[2]v",
"type": "int",
"underlyingType": "int",
"argNum": 2,
"expr": "result.TotalCount"
},
{
"id": "Entity",
"string": "%[3]s",
"type": "string",
"underlyingType": "string",
"argNum": 3,
"expr": "c.entity"
},
{
"id": "TargetName",
"string": "%[4]v",
"type": "string",
"underlyingType": "string",
"argNum": 4,
"expr": "c.targetName"
}
],
"fuzzy": true
},
{
"id": "During the import the following errors occurred:",
"message": "During the import the following errors occurred:",
"translation": "During the import the following errors occurred:",
"translatorComment": "Copied from source.",
"fuzzy": true
},
{
"id": "Error: {Err}",
"message": "Error: {Err}",
"translation": "Error: {Err}",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "Err",
"string": "%[1]v",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "err"
}
],
"fuzzy": true
},
{
"id": "Latest timestamp: {LastTimestamp} ({Unix})",
"message": "Latest timestamp: {LastTimestamp} ({Unix})",
"translation": "Latest timestamp: {LastTimestamp} ({Unix})",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "LastTimestamp",
"string": "%[1]v",
"type": "time.Time",
"underlyingType": "struct{wall uint64; ext int64; loc *time.Location}",
"argNum": 1,
"expr": "result.LastTimestamp"
},
{
"id": "Unix",
"string": "%[2]v",
"type": "int64",
"underlyingType": "int64",
"argNum": 2,
"expr": "result.LastTimestamp.Unix()"
}
],
"fuzzy": true
}
]
}

View file

@ -0,0 +1,18 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package translations
//go:generate gotext -srclang=en update -out=catalog.go -lang=en,de go.uploadedlobster.com/scotty