regionlang

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2021 License: MIT Imports: 1 Imported by: 0

README

go-regionlang - Detect languages from regions / countries

go get github.com/modernice/go-regionlang

Use Case

Sometimes you need to find the appropriate language for a country code in order to localize messages for a user with an unknown language. go-regionlang does exactly that, relying purely on the golang.org/x/text/language package:

package example

func getLanguageForRegion() {
	region := "be" // Belgium
	base, conf := regionlang.Find(region)

	base.String() == "fr" // French
	conf == language.Exact
}

Custom Language Tags

By default, go-regionlang matches against all built-in language tags. Your application most probably does not support every single built-in language. You can specify which language tags to match against:

package example

func getLanguageForRegion(allowedTags []language.Tag) {
	base, conf := regionlang.Find("some-region-code", allowedTags...)
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllTags

func AllTags() []language.Tag

AllTags returns the built-in language tags from the golang.org/x/text/language package.

func Find

func Find(region string, tags ...language.Tag) (language.Base, language.Confidence)

Find returns the base language for the given region (country).

If `tags` are provided, only the provided tags are considered. Otherwise all built-in languages are considered (see AllTags). When a region matches multiple of the provided language tags, the last matched tag is returned.

Example:

base, confidence := regionlang.Find("at") // Find language for Austria
base.String() == "de" // German is the Austrian language
conf == language.Exact

Find searches through the allowed tags (f.tags) and compares the regions of the tags with the provided region. If the region of a language tag matches with the provided region, the base language of the tag is returned. In cases where the provided region matches multiple language tags, the last match with the highest confidence is returned.

If Find fails to parse the region, language.Make(region).Base() is returned.

Types

type Finder

type Finder struct {
	// contains filtered or unexported fields
}

Finder finds base languages for regions (countries). Use NewFinder to create a Finder.

func NewFinder

func NewFinder(tags ...language.Tag) *Finder

NewFinder returns a Finder for the given language tags.

func (*Finder) Find

func (f *Finder) Find(region string) (language.Base, language.Confidence)

Find returns the base language for the given region (country).

Example:

base, confidence := f.Find("at") // Find language for Austria
base.String() == "de" // German is the Austrian language
conf == language.Exact

Find searches through the allowed tags (f.tags) and compares the regions of the tags with the provided region. If the region of a language tag matches with the provided region, the base language of the tag is returned. In cases where the provided region matches multiple language tags, the last match with the highest confidence is returned.

If Find fails to parse the region, language.Make(region).Base() is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL