https://github.com/liblxn/lxn-go
An lxn implementation for Go
https://github.com/liblxn/lxn-go
l10n localization lxn
Last synced: 27 days ago
JSON representation
An lxn implementation for Go
- Host: GitHub
- URL: https://github.com/liblxn/lxn-go
- Owner: liblxn
- License: mit
- Created: 2017-10-16T20:32:59.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2024-03-13T14:47:03.000Z (over 2 years ago)
- Last Synced: 2024-06-20T12:52:38.352Z (about 2 years ago)
- Topics: l10n, localization, lxn
- Language: Go
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lxn-go
`lxn-go` is an [lxn](https://github.com/liblxn/lxn) client library for the
Go programming language.
## Translating Text
To translate text, a dictionary has to be loaded:
```golang
func ReadDictionary(r io.Reader) (*Dictionary, error)
```
This function reads a binary dictionary from `r` and returns the dictionary
with all messages and the locale information needed to format these messages.
Once a dictionary is obtained, it can be used to translate messages.
A message can contain variable parts which can be replaced during runtime.
In order to render a message correctly all of the variables need to be
passed into the translation method of the dictionary. The following variable
types are currently supported:
* [`Int`](https://godoc.org/github.com/liblxn/lxn-go#Int) for signed integer values
* [`Uint`](https://godoc.org/github.com/liblxn/lxn-go#Uint) for unsigned integer values
* [`Float`](https://godoc.org/github.com/liblxn/lxn-go#Float) for floating-point numbers
* [`String`](https://godoc.org/github.com/liblxn/lxn-go#String) for strings
## Example
```golang
package main
import (
"fmt"
"log"
lxn "github.com/liblxn/lxn-go"
)
func main() {
dic := loadDictionary("en")
msg := dic.Translate("the-section", "the-message-key", lxn.Context{
"key1": lxn.Float(3.1415),
"key2": lxn.Uint(7),
})
fmt.Println("message for 'hello.lxn':", msg)
}
func loadDictionary(lang string) *lxn.Dictionary {
dic, err := lxn.FromFile(lxn.ReadDictionary, fmt.Sprintf("translations/%s.lxnc", lang))
if err != nil {
log.Fatal(err)
}
return dic
}
```