Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nejdetkadir/i18ngo
Simple internationalization library for Golang
https://github.com/nejdetkadir/i18ngo
go go-i18n golang i18n i18n-tool localization localization-tool translation
Last synced: about 2 months ago
JSON representation
Simple internationalization library for Golang
- Host: GitHub
- URL: https://github.com/nejdetkadir/i18ngo
- Owner: nejdetkadir
- License: mit
- Created: 2024-10-02T21:24:07.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-10-02T21:44:58.000Z (5 months ago)
- Last Synced: 2024-12-25T10:51:26.581Z (about 2 months ago)
- Topics: go, go-i18n, golang, i18n, i18n-tool, localization, localization-tool, translation
- Language: Go
- Homepage:
- Size: 130 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README


# I18nGo
I18nGo is a simple internationalization library for Golang that enables translation and locale management. It provides utilities to change locales, handle translations with scoped paths, and supports dynamic translation replacement using templates.
## Features
- Multiple Locales: Load and manage multiple locales for translations.
- Change Locales Dynamically: Easily switch between different languages.
- Translation with Scoped Paths: Fetch translations using dot-separated (or custom) paths.
- Dynamic Value Replacement: Replace placeholders in translations dynamically with provided values.
- Debugging: Enable debugging to log details about missing translations and locale management.
- Customizable Separator: Use a custom separator for translation paths.## Installation
To install I18nGo, use the following:```bash
go get github.com/nejdetkadir/i18ngo
```## Usage
### Initialize I18nGo
First, create and initialize I18nGo with options like default locale and available locales.```go
package mainimport (
"github.com/nejdetkadir/i18ngo"
"os"
)func main() {
enData, _ := os.ReadFile("foo/bar/en.json")
trData, _ := os.ReadFile("foo/bar/tr.json")
/*
json files should be like:
{
"pages": {
"login": {
"buttons": {
"login": "Login"
}
}
}
}
*/i18n, err := i18ngo.New(i18ngo.Options{
DefaultLocale: "en",
Debug: true, // Enable debugging (default: false)
Locales: []i18ngo.LocaleOptions{
{Locale: "en", File: enData},
{Locale: "tr", File: trData},
},
})if err != nil {
panic(err)
}// Use the i18ngo instance for translations
translated := i18n.T("pages.login.buttons.login")
println(translated) // Output: Login
}
```### Changing Locale
Change the locale dynamically using ChangeLocale.```go
i18n.ChangeLocale("tr")
translated := i18n.T("pages.login.buttons.login")
println(translated) // Output: Giriş
```### Dynamic Value Replacement
You can pass dynamic values for translation using the T function.```go
/*
json files should be like:
{
"pages": {
"welcome": "Welcome, {{name}}!"
}
}
*/params := map[string]interface{}{"name": "John"}
translated := i18n.T("pages.welcome", ¶ms)
println(translated) // Output: Welcome, John!
```### Scoped Translations
Use a scope to define a context for your translation paths.```go
/*
json files should be like:
{
"common": {
"greetings": {
"hello": "Hello"
}
}
}
*/params := map[string]interface{}{"scope": "common.greetings"}
translated := i18n.T("hello", ¶ms)
println(translated) // Output: Hello
```### Custom Separator
You can use a custom separator for translation paths. **You don't need to change the JSON files; just update the separator in the options.**```go
i18n, _ := i18ngo.New(i18ngo.Options{
DefaultLocale: "en",
Separator: "/", // Use a custom separator (default: ".")
Locales: []i18ngo.LocaleOptions{
{Locale: "en", File: enData},
{Locale: "tr", File: trData},
},
})translated := i18n.T("pages/login/buttons/login")
println(translated) // Output: Login
```## Example
Check out the [example](example/main.go) for a complete demonstration of I18nGo usage.## Unit Testing
The package includes comprehensive unit tests to ensure correct behavior across various scenarios. To run the tests, use the following:```bash
go test ./...
```## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/nejdetkadir/i18ngo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nejdetkadir/i18ngo/blob/main/CODE_OF_CONDUCT.md).## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.