Initial import

This commit is contained in:
3wc
2025-08-04 14:05:43 +01:00
commit 2b249f58fd
6 changed files with 69 additions and 0 deletions

7
README.md Normal file
View 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
View 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
View 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=

Binary file not shown.

View 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
View 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
}