https://github.com/spacetab-io/i18n-go
A small set of helpers for using as i18n part of companies services and applications.
https://github.com/spacetab-io/i18n-go
Last synced: about 1 month ago
JSON representation
A small set of helpers for using as i18n part of companies services and applications.
- Host: GitHub
- URL: https://github.com/spacetab-io/i18n-go
- Owner: spacetab-io
- License: gpl-3.0
- Created: 2020-05-19T06:29:12.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-21T12:15:48.000Z (about 4 years ago)
- Last Synced: 2025-02-14T22:13:52.250Z (3 months ago)
- Language: Go
- Homepage:
- Size: 43 KB
- Stars: 0
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
i18n-go
------------[](https://circleci.com/gh/spacetab-io/i18n-go)
[](https://codecov.io/gh/spacetab-io/i18n-go)A small set of helpers for using as i18n part of companies services and applications.
# Translation
## Usage
Basic usage a translation string and a context in a record and handling HTTP request
```go
package packimport (
"net/http"
"encoding/json""github.com/spacetab-io/i18n-go/translation"
)var (
// a configuration of default properties of translation context
// it could be loaded from json or yaml configuration file
Conf = translation.Conf{
Display: "ru",
Fallback: "en",
Second: "en",
TranslationList: false,
}
)// A model definition
type Record struct {
Id int `json:"id"`
Name translation.String `json:"name"`
}func (o *Record) ApplyTranslationCtx(ctx translation.Context) {
o.Name.ApplyTranslationCtx(ctx)
}// A handler of an http request
func Handler(w http.ResponseWriter, req *http.Request) {
translateCtx := translation.NewContext(&Conf, req)
rec := &Record{
Id: 10,
Name: translation.String {
Translate: map[string]string {
"en": "Hello world!",
"ru": "Здравствуй мир!",
},
},
}
rec.ApplyTranslationCtx(translateCtx)
json.NewEncoder(w).Encode(rec)
}
```### Response
An expected output without headers:
```json
{
"id": 10,
"name": {
"display": "Здравствуй мир!",
"second": "Hello world!",
"translate": null
}
}
```An expected output for a request with headers:
```http request
GET /record
X-Lang-Display: en
X-Lang-Second: ru
X-Lang-TranslateList: true
```a response:
```json
{
"id": 10,
"name": {
"display": "Здравствуй world!",
"second": "Привет мир!",
"translate": {
"ru": "Hello world!",
"en": "Здравствуй мир!"
}
}
}
```### Request
An example of a request to update of translation string
```http request
PUT /record
Content-Type: application/json{
"id": 12,
"name": {
"translate": {
"ru": "Hi world!",
"en": "Привет мир!"
}
}
}
```