mirror of
https://git.sr.ht/~phw/scotty
synced 2025-07-01 15:51:56 +02:00
Basic i18n setup
This commit is contained in:
parent
20f1732858
commit
a41318d822
10 changed files with 700 additions and 9 deletions
39
internal/i18n/i18n.go
Normal file
39
internal/i18n/i18n.go
Normal 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...)
|
||||
}
|
37
internal/i18n/localizer.go
Normal file
37
internal/i18n/localizer.go
Normal 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...)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue