Initial Commit
This commit is contained in:
commit
aa8c0b219c
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/translate
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2022 Arsen Musayelyan
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
5
README.md
Normal file
5
README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Translate
|
||||||
|
|
||||||
|
This library is an incredibly simple translation system for Go. It is designed to work well with [go.arsenm.dev/logger](https://pkg.go.dev/go.arsenm.dev/logger), and doesn't have nearly as many features as other solutions, because those features are not needed for all applications, and they increase complexity unnecessarily in use cases that don't need it. All this package does is parses TOML files for translations, and then gets the raw text from the files for whatever language you want to translate to.
|
||||||
|
|
||||||
|
If you need advanced functionality, such as handling singular/plural words, gender, etc., use other packages such as [golang.org/x/text/message](https://pkg.go.dev/golang.org/x/text/message) or [github.com/nicksnyder/go-i18n](https://pkg.go.dev/github.com/nicksnyder/go-i18n).
|
55
cmd/translate/gen-id.go
Normal file
55
cmd/translate/gen-id.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"hash/crc64"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml/v2"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"go.arsenm.dev/logger/log"
|
||||||
|
"go.arsenm.dev/translate"
|
||||||
|
)
|
||||||
|
|
||||||
|
func genIDCmd(c *cli.Context) error {
|
||||||
|
if c.NArg() < 1 {
|
||||||
|
log.Fatal("This command requires one or more arguments").Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
fl, err := os.Open(c.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error opening file").Err(err).Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
var tr translate.Translations
|
||||||
|
err = toml.NewDecoder(fl).Decode(&tr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fl.Close()
|
||||||
|
|
||||||
|
hash := crc64.New(crc64.MakeTable(crc64.ISO))
|
||||||
|
|
||||||
|
for i, item := range tr.Items {
|
||||||
|
hash.Reset()
|
||||||
|
|
||||||
|
_, err := io.WriteString(hash, item.Value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
tr.Items[i].ID = hash.Sum64()
|
||||||
|
}
|
||||||
|
|
||||||
|
fl, err = os.Create(c.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error creating file").Err(err).Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = toml.NewEncoder(fl).Encode(tr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error writing toml to file").Err(err).Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
32
cmd/translate/main.go
Normal file
32
cmd/translate/main.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := &cli.App{
|
||||||
|
Commands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "gen-id",
|
||||||
|
Usage: "<file>",
|
||||||
|
UsageText: "Generate IDs for each string in the given file",
|
||||||
|
Action: genIDCmd,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "translate",
|
||||||
|
Aliases: []string{"tr"},
|
||||||
|
Usage: "<dir> <lang> <string>",
|
||||||
|
UsageText: "Translate the provided string to the given language using the translations in the directory.",
|
||||||
|
Action: translateCmd,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := app.Run(os.Args)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
29
cmd/translate/translate.go
Normal file
29
cmd/translate/translate.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"go.arsenm.dev/logger/log"
|
||||||
|
"go.arsenm.dev/translate"
|
||||||
|
"golang.org/x/text/language"
|
||||||
|
)
|
||||||
|
|
||||||
|
func translateCmd(c *cli.Context) error {
|
||||||
|
if c.NArg() < 3 {
|
||||||
|
log.Fatal("This command requires three or more arguments").Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := translate.NewFromDir(c.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error creating new translator").Err(err).Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
tag, err := language.Parse(c.Args().Get(1))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error parsing language").Err(err).Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(t.TranslateTo(c.Args().Get(2), tag))
|
||||||
|
return nil
|
||||||
|
}
|
20
go.mod
Normal file
20
go.mod
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
module go.arsenm.dev/translate
|
||||||
|
|
||||||
|
go 1.19
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/pelletier/go-toml/v2 v2.0.6
|
||||||
|
github.com/urfave/cli/v2 v2.23.7
|
||||||
|
go.arsenm.dev/logger v0.0.0-20230104225304-d706171ea6df
|
||||||
|
golang.org/x/text v0.6.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||||
|
github.com/gookit/color v1.5.1 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
|
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
|
||||||
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
|
||||||
|
)
|
41
go.sum
Normal file
41
go.sum
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gookit/color v1.5.1 h1:Vjg2VEcdHpwq+oY63s/ksHrgJYCTo0bwWvmmYWdE9fQ=
|
||||||
|
github.com/gookit/color v1.5.1/go.mod h1:wZFzea4X8qN6vHOSP2apMb4/+w/orMznEzYsIHPaqKM=
|
||||||
|
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||||
|
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||||
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
github.com/urfave/cli/v2 v2.23.7 h1:YHDQ46s3VghFHFf1DdF+Sh7H4RqhcM+t0TmZRJx4oJY=
|
||||||
|
github.com/urfave/cli/v2 v2.23.7/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
|
||||||
|
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
|
||||||
|
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
|
||||||
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||||
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
||||||
|
go.arsenm.dev/logger v0.0.0-20230104225304-d706171ea6df h1:8mBHvEe7BJmpOeKSMA5YLqrGo9dCpePocTeR0C1+/2w=
|
||||||
|
go.arsenm.dev/logger v0.0.0-20230104225304-d706171ea6df/go.mod h1:RV2qydKDdoyaRkhAq8JEGvojR8eJ6bjq5WnSIlH7gYw=
|
||||||
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
|
||||||
|
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
231
logger.go
Normal file
231
logger.go
Normal file
@ -0,0 +1,231 @@
|
|||||||
|
package translate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"go.arsenm.dev/logger"
|
||||||
|
"golang.org/x/text/language"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ logger.Logger = &TranslatedLogger{}
|
||||||
|
|
||||||
|
type TranslatedLogger struct {
|
||||||
|
Logger logger.Logger
|
||||||
|
Translator Translator
|
||||||
|
Language language.Tag
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLogger(l logger.Logger, t Translator, lang language.Tag) *TranslatedLogger {
|
||||||
|
return &TranslatedLogger{l, t, lang}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) NoPanic() {
|
||||||
|
tl.Logger.NoPanic()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) NoExit() {
|
||||||
|
tl.Logger.NoExit()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) SetLevel(level logger.LogLevel) {
|
||||||
|
tl.Logger.SetLevel(level)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Debug(msg string) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Debug(tl.Translator.TranslateTo(msg, tl.Language)),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Debugf(format string, args ...any) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Debugf(tl.Translator.TranslateTo(format, tl.Language), args...),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Info(msg string) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Info(tl.Translator.TranslateTo(msg, tl.Language)),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Infof(format string, args ...any) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Infof(tl.Translator.TranslateTo(format, tl.Language), args...),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Warn(msg string) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Warn(tl.Translator.TranslateTo(msg, tl.Language)),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Warnf(format string, args ...any) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Warnf(tl.Translator.TranslateTo(format, tl.Language), args...),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Error(msg string) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Error(tl.Translator.TranslateTo(msg, tl.Language)),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Errorf(format string, args ...any) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Errorf(tl.Translator.TranslateTo(format, tl.Language), args...),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Fatal(msg string) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Fatal(tl.Translator.TranslateTo(msg, tl.Language)),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Fatalf(format string, args ...any) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Fatalf(tl.Translator.TranslateTo(format, tl.Language), args...),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Panic(msg string) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Panic(tl.Translator.TranslateTo(msg, tl.Language)),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tl *TranslatedLogger) Panicf(format string, args ...any) logger.LogBuilder {
|
||||||
|
return &TranslatedLogBuilder{
|
||||||
|
LogBuilder: tl.Logger.Panicf(tl.Translator.TranslateTo(format, tl.Language), args...),
|
||||||
|
Translator: tl.Translator,
|
||||||
|
Language: tl.Language,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type TranslatedLogBuilder struct {
|
||||||
|
LogBuilder logger.LogBuilder
|
||||||
|
Translator Translator
|
||||||
|
Language language.Tag
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Int(key string, value int) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Int(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Int8(key string, value int8) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Int8(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Int16(key string, value int16) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Int16(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Int32(key string, value int32) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Int32(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Int64(key string, value int64) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Int64(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Uint(key string, value uint) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Uint(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Uint8(key string, value uint8) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Uint8(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Uint16(key string, value uint16) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Uint16(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Uint32(key string, value uint32) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Uint32(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Uint64(key string, value uint64) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Uint64(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Float32(key string, value float32) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Float32(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Float64(key string, value float64) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Float64(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Stringer(key string, value fmt.Stringer) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Stringer(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Bytes(key string, value []byte) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Bytes(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Timestamp() logger.LogBuilder {
|
||||||
|
return tlb.LogBuilder.Timestamp()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Bool(key string, value bool) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Bool(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Str(key string, value string) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Str(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Any(key string, value any) logger.LogBuilder {
|
||||||
|
key = tlb.Translator.TranslateTo(key, tlb.Language)
|
||||||
|
return tlb.LogBuilder.Any(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Err(err error) logger.LogBuilder {
|
||||||
|
return tlb.LogBuilder.Err(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tlb *TranslatedLogBuilder) Send() {
|
||||||
|
tlb.LogBuilder.Send()
|
||||||
|
}
|
120
translate.go
Normal file
120
translate.go
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
package translate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"hash/crc64"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml/v2"
|
||||||
|
"golang.org/x/text/language"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Translations struct {
|
||||||
|
Items []Translation `toml:"translation"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Translation struct {
|
||||||
|
ID uint64 `toml:"id"`
|
||||||
|
Value string `toml:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Dictionary map[uint64]string
|
||||||
|
type Catalog map[language.Tag]Dictionary
|
||||||
|
|
||||||
|
type Translator struct {
|
||||||
|
cat Catalog
|
||||||
|
Fallback language.Tag
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(cat Catalog) Translator {
|
||||||
|
return Translator{cat, language.English}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFromDir(dir string) (Translator, error) {
|
||||||
|
return NewFromFS(os.DirFS(dir))
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFromFS(fsys fs.FS) (Translator, error) {
|
||||||
|
cat := Catalog{}
|
||||||
|
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ext := filepath.Ext(path)
|
||||||
|
if ext != ".toml" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
lang := filepath.Ext(strings.TrimSuffix(path, ext))
|
||||||
|
lang = strings.TrimPrefix(lang, ".")
|
||||||
|
|
||||||
|
tag, err := language.Parse(lang)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fl, err := fsys.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var tr Translations
|
||||||
|
err = toml.NewDecoder(fl).Decode(&tr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fl.Close()
|
||||||
|
|
||||||
|
cat[tag] = toDict(tr)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return Translator{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(cat)
|
||||||
|
|
||||||
|
return Translator{cat, language.English}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Translator) TranslateTo(s string, tag language.Tag) string {
|
||||||
|
dict, ok := t.cat[tag]
|
||||||
|
if !ok {
|
||||||
|
dict, ok = t.cat[t.Fallback]
|
||||||
|
if !ok {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
id := hashString(s)
|
||||||
|
str, ok := dict[id]
|
||||||
|
if !ok {
|
||||||
|
str = s
|
||||||
|
}
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func toDict(tr Translations) Dictionary {
|
||||||
|
out := Dictionary{}
|
||||||
|
for _, item := range tr.Items {
|
||||||
|
out[item.ID] = item.Value
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func hashString(s string) uint64 {
|
||||||
|
hash := crc64.New(crc64.MakeTable(crc64.ISO))
|
||||||
|
_, err := io.WriteString(hash, s)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return hash.Sum64()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user