Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zzerjae/sejong
simple translate library like ruby's i18n gem
https://github.com/zzerjae/sejong
Last synced: 3 months ago
JSON representation
simple translate library like ruby's i18n gem
- Host: GitHub
- URL: https://github.com/zzerjae/sejong
- Owner: zzerjae
- License: mit
- Created: 2020-03-19T11:51:46.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-08T02:59:37.000Z (almost 5 years ago)
- Last Synced: 2024-08-03T14:07:18.634Z (7 months ago)
- Language: Go
- Homepage:
- Size: 39.1 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# sejong
[](https://goreportcard.com/report/github.com/zzerjae/sejong)
simple translate library like ruby's i18n module
## Example
Suppose you need some greetings messages in English and Korean respectively.
You can write en-GB.yml
```yml
# en-GB.yml
en-GB:
message:
welcome: "Hello, %{nickname}!"
farewell: "It's %{time} o'clock. Good bye, %{nickname}!"
```and ko.yml.
```yml
# ko.yml
ko:
message:
welcome: "안녕, %{nickname}!"
farewell: "%{time}시 입니다. 잘가요, %{nickname}!"
```then you can use `sejong.T` for translate.
```go
package mainimport (
"fmt"
"strconv"
"time""github.com/zzerjae/sejong"
)func main() {
sejong.Locale = "en-GB"message, err := sejong.T("message.welcome", "nickname", "John")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(message) // Hello, John!message, err = sejong.T("message.farewell", "nickname", "John", "time", strconv.Itoa(time.Now().Hour()))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(message) // It's 5 o'clock. Good bye, John!sejong.Locale = "ko"
message, err = sejong.T("message.welcome", "nickname", "길동")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(message) // 안녕, 길동!
}
```## Configuration
### Location of locale files
Default location is the directory of `main.go`, but you can set the env `SEJONG_LOCALE_DIRECTORY` for yours.
### Working with multiple locales
You can also create many different translators for use in your application. Each will have its own unique locale source.
Example:
```go
package mainimport (
"fmt""github.com/zzerjae/sejong"
)func main() {
ko, err := sejong.New("ko")
if err != nil {
fmt.Println(err)
return
}
gb, err := sejong.New("en-GB")
if err != nil {
fmt.Println(err)
return
}message, err := ko.T("message.welcome", "nickname", "길동")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(message) // 안녕, 길동!message, err = gb.T("message.welcome", "nickname", "John")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(message) // Hello, John!
}```
## QNA
### Why is it called "sejong"?
https://en.wikipedia.org/wiki/Sejong_the_Great
## Author
@zzerjae
## License
MIT