Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/modernice/go-regionlang
Detect languages from countries / regions in Go.
https://github.com/modernice/go-regionlang
country go golang i18n language map region
Last synced: about 4 hours ago
JSON representation
Detect languages from countries / regions in Go.
- Host: GitHub
- URL: https://github.com/modernice/go-regionlang
- Owner: modernice
- License: mit
- Created: 2021-12-12T17:25:26.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-29T18:58:49.000Z (over 1 year ago)
- Last Synced: 2024-06-20T17:41:26.373Z (5 months ago)
- Topics: country, go, golang, i18n, language, map, region
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# regionlang - Country/Region to Language Mapper
`regionlang` is a Go library that aids in determining the language associated with a given country or region. It comes in handy when you need to localize messages for users whose language preference is unknown.
## Install
```bash
go get github.com/modernice/go-regionlang
```## Usage
The library's primary feature is the `Find` function which takes a region code as an argument and returns the corresponding language.
Here's a basic example:
```go
package mainimport (
"fmt""github.com/modernice/go-regionlang"
"golang.org/x/text/language"
)func main() {
region := "be" // Belgium
base, conf := regionlang.Find(region)fmt.Println(base.String()) // Output: "fr" (for French)
fmt.Println(conf == language.Exact) // Output: true
}
```In the above example, `regionlang.Find("be")` returns French ("fr") as the most likely language for Belgium.
### Custom Language Tags
By default, `go-regionlang` matches against all built-in language tags. However, your application might not support every single built-in language. To specify which language tags to match against, pass the allowed tags to the `Find` function as follows:
```go
package mainimport (
"fmt""github.com/modernice/go-regionlang"
"golang.org/x/text/language"
)func main() {
allowedTags := []language.Tag{language.English, language.Spanish, language.French}
base, conf := regionlang.Find("some-region-code", allowedTags...)fmt.Println(base.String())
fmt.Println(conf == language.Exact)
}
```In this example, `Find` will only consider English, Spanish, and French when determining the language for the given region.
## License
This project is licensed under the [MIT](./LICENSE) License.