locale

package module
v0.0.0-...-ce5225d Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: MIT Imports: 2 Imported by: 21

README

go-locale

PkgGoDev Go Report Card Coverage Status test

Go library used to retrieve the current locale(s) of the operating system.

OS Support

  • Windows
    Using GetUserDefaultLocaleName and GetSystemDefaultLocaleName.
  • macOS
    Using defaults read -g AppleLocale and defaults read -g AppleLanguages (since environment variables like LANG are not usually set on macOS).
  • Unix-like systems (Linux, BSD, etc.)
    Using the LANGUAGE, LC_ALL, LC_MESSAGES and LANG environment variables.
  • WASM (JavaScript)
    Using navigator.language and navigator.languages.
  • iOS
    Using [NSLocale preferredLanguages].
    • Android
      Using getResources().getConfiguration().getLocales for Android N or later, or getResources().getConfiguration().locale otherwise.

      Note: for Android, you'll first need to call SetRunOnJVM, depending on which mobile framework you're using:

      • For Fyne:

        import (
            "fyne.io/fyne/v2/driver"
            "github.com/jeandeaual/go-locale"
        )
        
        func init() {
            locale.SetRunOnJVM(func(fn func(vm, env, ctx uintptr) error) error {
                driver.RunNative(func(ctx interface{}) error {
                    and := ctx.(*driver.AndroidContext)
                    return fn(and.VM, and.Env, and.Ctx)
                })
                return nil
            })
        }
        
      • For gomobile:

        import (
            "golang.org/x/mobile/app"
            "github.com/jeandeaual/go-locale"
        )
        
        func init() {
            locale.SetRunOnJVM(app.RunOnJVM)
        }
        

Usage

GetLocales

GetLocales returns the user's preferred locales, by order of preference, as a slice of IETF BCP 47 language tag (e.g. []string{"en-US", "fr-FR", "ja-JP"}).

This works if the user set multiple languages on macOS and other Unix systems. Otherwise, it returns a slice with a single locale.

userLocales, err := locale.GetLocales()
if err == nil {
	fmt.Println("Locales:", userLocales)
}

This can be used with golang.org/x/text or go-i18n to set the localizer's language preferences:

import (
	"github.com/jeandeaual/go-locale"
	"golang.org/x/text/message"
)

func main() {
	userLocales, _ := locale.GetLocales()
	p := message.NewPrinter(message.MatchLanguage(userLocales...))
	...
}
import (
	"github.com/jeandeaual/go-locale"
	"github.com/nicksnyder/go-i18n/v2/i18n"
	"golang.org/x/text/language"
)

func main() {
	userLocales, _ := locale.GetLocales()
	bundle := i18n.NewBundle(language.English)
	localizer := i18n.NewLocalizer(bundle, userLocales...)
	...
}

For a complete example, see here.

GetLocale

GetLocale returns the current locale as defined in IETF BCP 47 (e.g. "en-US").

userLocale, err := locale.GetLocale()
if err == nil {
	fmt.Println("Locale:", userLocale)
}

GetLanguage

GetLanguage returns the current language as an ISO 639 language code (e.g. "en").

userLanguage, err := locale.GetLanguage()
if err == nil {
	fmt.Println("Language:", userLocale)
}

GetRegion

GetRegion returns the current language as an ISO 3166 country code (e.g. "US").

userRegion, err := locale.GetRegion()
if err == nil {
	fmt.Println("Region:", userRegion)
}

Aknowledgements

Inspired by jibber_jabber.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLanguage

func GetLanguage() (string, error)

GetLanguage retrieves the IETF BCP 47 language tag set on the system and returns the language part of the tag.

func GetLocale

func GetLocale() (string, error)

GetLocale retrieves the IETF BCP 47 language tag set on the system.

func GetLocales

func GetLocales() ([]string, error)

GetLocales retrieves the IETF BCP 47 language tags set on the system.

func GetRegion

func GetRegion() (string, error)

GetRegion retrieves the IETF BCP 47 language tag set on the system and returns the region part of the tag.

func SetRunOnJVM

func SetRunOnJVM(_ func(fn func(vm, env, ctx uintptr) error) error)

SetRunOnJVM is a noop, this function is only valid on Android

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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