i18n

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2020 License: Apache-2.0 Imports: 2 Imported by: 0

README

Internationalisation

The core/i18n package functions for dealing with internationalisation of services.

Examples

Parse locale from
locale, _ := i18n.ParseLocale("EN_gb")
fmt.Println(locale)
// Output: "en-GB"
Put locale through context

Setting the locale in a context.

ctx = i18n.ContextWithLocale(context.Background(), "sv")

Retreiving a locale from context.

locale = i18n.LocaleFromContext(ctx)
Change the default locale

You can set the default locale to be any value you'd like. By default it's set to en.

import "github.com/LUSHDigital/core/i18n"

func main() {
    i18n.DefaultLocale = "es"
}

Documentation

Overview

Package i18n provides functions for dealing with internationalisation of services.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLocale is the default locale is set to english for simplicity.
	DefaultLocale = "en"
)

Functions

func ContextWithLocale

func ContextWithLocale(parent context.Context, locale string) context.Context

ContextWithLocale takes a context and a locale and returns a new context with the locale embedded.

Example
package main

import (
	"context"

	"github.com/LUSHDigital/core/i18n"
)

var ctx context.Context

func main() {
	ctx = i18n.ContextWithLocale(context.Background(), "sv")
}
Output:

func LocaleFromContext

func LocaleFromContext(ctx context.Context) string

LocaleFromContext extracts the locale from the supplied context.

Example
package main

import (
	"context"

	"github.com/LUSHDigital/core/i18n"
)

var (
	ctx    context.Context
	locale string
)

func main() {
	locale = i18n.LocaleFromContext(ctx)
}
Output:

func ParseLocale

func ParseLocale(s string) (string, error)

ParseLocale will attempt to read the locale from a string.

Example
package main

import (
	"fmt"

	"github.com/LUSHDigital/core/i18n"
)

func main() {
	locale, _ := i18n.ParseLocale("EN_gb")
	fmt.Println(locale)
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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