-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 52c304b
Showing
22 changed files
with
1,178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,5 @@ | ||
vendor | ||
memprofile.out | ||
profile.out | ||
coverage.out | ||
test_files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,14 @@ | ||
language: go | ||
|
||
sudo: false | ||
|
||
go: | ||
- 1.13.x | ||
|
||
before_install: | ||
- go get golang.org/x/tools/cmd/cover | ||
- go get github.com/mattn/goveralls | ||
script: | ||
- make test | ||
after_success: | ||
- bash <(curl -s https://codecov.io/bash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,19 @@ | ||
Copyright (c) 2019 Miles Croxford <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,5 @@ | ||
PACKAGES := $(shell go list ./... | grep -v cmd | grep -v test_files) | ||
|
||
.PHONY: test | ||
test: | ||
go test -v -failfast -race -coverpkg=./... -covermode=atomic -coverprofile=coverage.out $(PACKAGES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,89 @@ | ||
# go-localize | ||
|
||
[![GoDoc](https://godoc.org/github.com/m1/go-localize?status.svg)](https://godoc.org/github.com/m1/go-localize) | ||
[![Build Status](https://travis-ci.org/m1/go-localize.svg?branch=master)](https://travis-ci.org/m1/go-localize) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/m1/go-localize)](https://goreportcard.com/report/github.com/m1/go-localize) | ||
[![Release](https://img.shields.io/github/release/m1/go-localize.svg)](https://github.com/m1/go-localize/releases/latest) | ||
[![codecov](https://codecov.io/gh/m1/go-localize/branch/master/graph/badge.svg)](https://codecov.io/gh/m1/go-localize) | ||
|
||
__ Simple and easy to use i18n (Internationalization and localization) engine written in Go, used for translating locale strings. | ||
Use with [go generate](#go-generate) or on the [CLI](#cli). Currently supports JSON and YAML translation files __ | ||
|
||
## Usage | ||
|
||
### Go generate | ||
|
||
The suggested way to use go-localize is to use `go generate`. For example, take the following directory structure: | ||
|
||
``` | ||
goapp | ||
└── localizations_src | ||
├── en | ||
│ └── messages.yaml | ||
└── es | ||
├── customer | ||
│ └── messages.json | ||
└── messages.json | ||
``` | ||
|
||
Example of JSON translation file: | ||
|
||
```json | ||
{ | ||
"hello": "Hola", | ||
"how_are_you": "¿Cómo estás?", | ||
"whats_your_name": "¿Cuál es tu nombre?", | ||
"hello_my_name_is": "Hola, mi nombre es {{.name}}" | ||
} | ||
``` | ||
|
||
Example of YAML translation file: | ||
```yaml | ||
hello: hello | ||
how_are_you: How are you? | ||
whats_your_name: "What's your name?" | ||
hello_my_name_is: Hello my name is {{.name}} | ||
hello_firstname_lastname: Hello {{.firstname}} {{.lastname}} | ||
``` | ||
To then generate the localization package, add the following to your `main.go` or another one of your `.go` files: | ||
|
||
```go | ||
//go:generate go-localize -input localizations_src -output localizations | ||
// | ||
``` | ||
|
||
Now you'll be able to use the localization like so: | ||
```go | ||
l := localizations.New("en", "es") | ||
println(l.Get("messages.how_are_you")) // How are you? | ||
println(l.GetWithLocale("es", "messages.hello_my_name_is", &localizations.Replacements{"name":"steve"})) // "Hola, mi nombre es steve" | ||
``` | ||
|
||
With `en` being the locale and `es` being the fallback. The localization keys are worked out using folder structure, eg: | ||
|
||
`en/customer/messages.json` with the contents being: | ||
```json | ||
{ | ||
"hello": "hello customer!" | ||
} | ||
``` | ||
You'll be able to access this using the key: `customer.messages.hello`. | ||
|
||
#### Suggestions | ||
|
||
It is suggested to instead of using hardcoded locale keys i.e. `en` to use the language keys included in key, i.e: `language.BritishEnglish.String()` | ||
which is "en-GB" | ||
|
||
### CLI | ||
|
||
Instead of using go generate you can just generate the localizations manually using `go-localize`: | ||
``` | ||
Usage of go-localize: | ||
-input string | ||
input localizations folder | ||
-output string | ||
where to output the generated package | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.