g11n

package module
v0.0.0-...-4261f93 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2020 License: MIT Imports: 4 Imported by: 0

README

g11n

Build Status Coverage Status Go Report Card GoDoc MIT License

g11n /gopherization/ is an internationalization library inspired by GWT that offers:

  • Statically-typed message keys.
  • Parameterized messages.
  • Extendable message formatting.
  • Custom localization file format.
package main

import (
	"fmt"
	"net/http"

	"github.com/sgatev/g11n"
	locale "github.com/sgatev/g11n/http"
)

type Messages struct {
	Hello func(string) string `default:"Hi %v!"`
}

func main() {
	http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		// Create messages factory.
		factory := g11n.New()

		// Initialize messages value.
		var m Messages
		factory.Init(&m)

		// Set messages locale.
		locale.SetLocale(factory, r)

		fmt.Fprintf(w, m.Hello("World"))
	})

	log.Fatal(http.ListenAndServe(":8080", nil))
}

Documentation

Overview

Package g11n is an internationalization library that offers:

  • Statically-typed message keys.
  • Parameterized messages.
  • Extendable message formatting.
  • Custom localization file format.

I. Initialization

Create a new instance of g11n. Each instance handles messages and locales separately.

G = g11n.New()

Define a struct with messages.

type Messages struct {
	TheAnswer func(string, int) string `default:"The answer to %v is %v."`
}

Initialize an instance of the struct through the g11n object.

var M *Messages
G.Init(M)

Invoke messages on that instance.

M.TheAnswer("everything", 42")

II. Choosing locale

Load a locale in the g11n instance.

G.LoadLocale("json", "bg", bgLocalePath)

Different locale loaders could be registered by implementing the locale.Loader interface.

Specify the locale for every message struct initialized by this g11n instance.

G.SetLocale("en")

III. Format parameters

The parameters of a message call could be formatted by declaring a special type that implements

G11nParam() string

The format method G11nParam is invoked before substituting a parameter in the message.

type PluralFormat int

func (pf PluralFormat) G11nParam() string {
	switch pf {
	case 0:
		return "some"
	case 1:
		return "crazy"
	default:
		return "stuff"
	}
}

type M struct {
	MyLittleSomething func(PluralFormat) string `default:"Count: %v"`
}

IV. Format result

The result of a message call could be further formatted by declaring a special result type that implements

G11nResult(formattedMessage string) string

The format method G11nResult is invoked after all parameters have been substituted in the message.

type SafeHTMLFormat string

func (shf SafeHTMLFormat) G11nResult(formattedMessage string) string {
	r := strings.NewReplacer("<", `\<`, ">", `\>`, "/", `\/`)
	return r.Replace(formattedMessage)
}

type M struct {
	MyLittleSomething func() SafeHTMLFormat `default:"<message>Oops!</message>"`
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MessageFactory

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

MessageFactory initializes message structs and provides language translations to messages.

func New

func New() *MessageFactory

New returns a fresh G11n message factory.

func (*MessageFactory) Init

func (mf *MessageFactory) Init(structPtr interface{}) interface{}

Init initializes the message fields of a structure pointer.

func (*MessageFactory) LoadLocale

func (mf *MessageFactory) LoadLocale(tag language.Tag)

LoadLocale sets the currently active locale for the messages generated by this factory.

func (*MessageFactory) Locales

func (mf *MessageFactory) Locales() []language.Tag

Locales returns the registered locales in a message factory.

func (*MessageFactory) SetLocale

func (mf *MessageFactory) SetLocale(tag language.Tag, format, path string)

SetLocale registers a locale file in the specified format.

func (*MessageFactory) SetLocales

func (mf *MessageFactory) SetLocales(locales map[language.Tag]string, format string)

SetLocales registers locale files in the specified format.

Directories

Path Synopsis
Package locale presents localization loaders for the g11n internationalization library.
Package locale presents localization loaders for the g11n internationalization library.
Package test contains functions used in g11n testing.
Package test contains functions used in g11n testing.

Jump to

Keyboard shortcuts

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