Initial import
This commit is contained in:
7
README.md
Normal file
7
README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# translations-test
|
||||
|
||||
```shell
|
||||
go get
|
||||
go run main.go
|
||||
LC_MESSAGES=es_ES go run main.go
|
||||
```
|
||||
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module example/translations-test
|
||||
|
||||
go 1.24.5
|
||||
|
||||
require github.com/leonelquinteros/gotext v1.7.2
|
||||
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/leonelquinteros/gotext v1.7.2 h1:bDPndU8nt+/kRo1m4l/1OXiiy2v7Z7dfPQ9+YP7G1Mc=
|
||||
github.com/leonelquinteros/gotext v1.7.2/go.mod h1:9/haCkm5P7Jay1sxKDGJ5WIg4zkz8oZKw4ekNpALob8=
|
||||
BIN
locales/es_ES/LC_MESSAGES/default.mo
Normal file
BIN
locales/es_ES/LC_MESSAGES/default.mo
Normal file
Binary file not shown.
8
locales/es_ES/LC_MESSAGES/default.po
Normal file
8
locales/es_ES/LC_MESSAGES/default.po
Normal file
@ -0,0 +1,8 @@
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Language: es_ES\n"
|
||||
"Content-Type: text/plain; charset=UTF-8\n"
|
||||
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
|
||||
|
||||
msgid "Hello World"
|
||||
msgstr "Hola Mundo"
|
||||
47
main.go
Normal file
47
main.go
Normal file
@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/leonelquinteros/gotext"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Set locale from environment
|
||||
locale := getLocale()
|
||||
|
||||
// Configure gettext
|
||||
gotext.Configure("locales", locale, "default")
|
||||
|
||||
// Translate and print
|
||||
fmt.Println(gotext.Get("Hello World"))
|
||||
}
|
||||
|
||||
// Determine locale from environment variables
|
||||
func getLocale() string {
|
||||
// Check LC_MESSAGES first
|
||||
if loc := os.Getenv("LC_MESSAGES"); loc != "" {
|
||||
return normalizeLocale(loc)
|
||||
}
|
||||
|
||||
// Fallback to LANG if LC_MESSAGES is empty
|
||||
if loc := os.Getenv("LANG"); loc != "" {
|
||||
return normalizeLocale(loc)
|
||||
}
|
||||
|
||||
// Default to English
|
||||
return "en_US"
|
||||
}
|
||||
|
||||
// Normalize locale format (es_ES.UTF-8 → es_ES)
|
||||
func normalizeLocale(loc string) string {
|
||||
if idx := strings.Index(loc, "."); idx != -1 {
|
||||
return loc[:idx]
|
||||
}
|
||||
if idx := strings.Index(loc, "@"); idx != -1 {
|
||||
return loc[:idx]
|
||||
}
|
||||
return loc
|
||||
}
|
||||
Reference in New Issue
Block a user