intergo

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 2 Imported by: 0

README

InterGo

Small library for simple internationalization in Go.

Usage

var ctx InterContext
ctx.Init()
ctx.AddLocale("pt_BR", map[string]string{"hello": "olá"})

Optionally set the prefered locale to properly use ctx.Get():

ctx.SetPreferedLocale("pt_BR")
Get localized strings:

This returns "olá", as we have a "pt_BR" locale set.

txt, err := ctx.GetFromLocale("hello", "pt_BR")

This returns "olá", as we haven't set any Portuguese Portuguese locale, so it falls back to other locales in the same language:

txt, err = ctx.GetFromLocale("hello", "pt_PT")

This returns "hello", as we haven't set any English language locale, so it'll just return the string we have passed.

txt, err = ctx.GetFromLocale("hello", "en_US")
Prefered locale.

It's possible to set a prefered locale. This way, we simply use ctx.Get() to retrive strings instead of passing the locale every time:

err := ctx.SetPreferedLocale(locale)
if err != nil {
    return fmt.Errorf("error parsing locale string: %v", locale)
}
txt := ctx.Get("hello")

Note how ctx.Get() does not need to return any error as it does not parses a locale string.

It's also possible to automatically set the prefered locale from the environment variables LC_ALL and LANG:

err := ctx.AutoSetPreferedLocale()
if err != nil {
    return fmt.Errorf("error parsing environment variables: %v", err)
}

Documentation

Overview

Package intergo implements a simple library for internationalized strings. The library manages a hash map in the form map["language"]["locale"]. The supported format for locale strings is language_locale.encoding. The encoding part is actually ignored, and the form language_locale also works. Of course, it's case-sensitive and the recommended form is language_LOCALE, e.g., en_US is a locale for American English, and pt_BR is for Brazilian Portuguese.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InterContext

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

The library context itself.

func (*InterContext) AddLocale

func (ctx *InterContext) AddLocale(locale string, entries map[string]string) error

Adds a new mapping of strings, i.e., a new locale, to the context. Usually called for all the supported locales right after the context initialization.

func (*InterContext) AutoSetPreferedLocale

func (ctx *InterContext) AutoSetPreferedLocale() error

Automatically sets the prefered locale with the variables `LC_ALL` and `LANG`. Basically tries `LC_ALL`, and if it cannot parse a locale from it, tries `LC_LANG`.

func (*InterContext) Get

func (ctx *InterContext) Get(text string) string

Gets an internationalized string with the prefered locale.

func (*InterContext) GetFromLocale

func (ctx *InterContext) GetFromLocale(text string, locale string) (string, error)

Gets an internationalized string from a specific locale.

func (*InterContext) Init

func (ctx *InterContext) Init()

Initializes the language map, should be called after instanciating an InterContext. Usually called right after the application startup.

func (*InterContext) SetPreferedLocale

func (ctx *InterContext) SetPreferedLocale(locale string) error

Sets the prefered locale.

type Language

type Language map[string]Locale

A collection of locales with the same language. E.g., `en_US` and `en_GB` are in the same Language map `en`.

type Locale

type Locale map[string]string

The type for a specific locale, i.e., the map with internationalized entries. E.g., the map `br` may have entries `br["hello"] == "olá"`.

Jump to

Keyboard shortcuts

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