i18n

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

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

Go to latest
Published: Sep 12, 2021 License: MIT Imports: 8 Imported by: 0

README

i18n

GitHub Workflow Status codecov GoDoc Sourcegraph

Package i18n provides internationalization and localization for your Go applications.

Installation

The minimum requirement of Go is 1.16.

go get github.com/go-i18n/i18n

Getting started

# locale_en-US.ini
[plurals]
file.one = file
file.other = files

dog.one = dog
dog.other = dogs

[messages]
test1 = This patch has %[1]d changed ${file, 1} and deleted %[2]d ${file, 2}
test2 = I have %[1]d ${dog, 1}
# locale_zh-CN.ini
[plurals]
file.other = 文件

[messages]
test1 = 该补丁变更了 %[1]d 个${file, 1}并删除了 %[2]d 个${file, 2}
package main

import (
	"fmt"

	"github.com/go-i18n/i18n"
)

func main() {
	s := i18n.NewStore()
	l1, err := s.AddLocale("en-US", "English", "locale_en-US.ini")
	// ... handler error

	l2, err := s.AddLocale("zh-CN", "简体中文", "locale_zh-CN.ini")
	// ... handler error

	fmt.Println(l1.Translate("messages::test1", 1, 2))
	// => "This patch has 1 changed file and deleted 2 files"

	fmt.Println(l2.Translate("messages::test1", 1, 2))
	// => "该补丁变更了 1 个文件并删除了 2 个文件"

	fmt.Println(l2.TranslateWithFallback(l1, "messages::test2", 1))
	// => "I have 1 dog"
}

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLocalNotFound = errors.New("locale not found")

Functions

This section is empty.

Types

type Locale

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

Locale represents a locale with target language and a collection of messages.

func (*Locale) Description

func (l *Locale) Description() string

Description returns the descriptive name of the locale.

func (*Locale) Lang

func (l *Locale) Lang() string

Lang returns the BCP 47 language name of the locale.

func (*Locale) Translate

func (l *Locale) Translate(key string, args ...interface{}) string

Translate uses the locale to translate the message of the given key.

func (*Locale) TranslateWithFallback

func (l *Locale) TranslateWithFallback(fallback *Locale, key string, args ...interface{}) string

TranslateWithFallback uses the locale to translate the message of the given key. It attempts to use the `fallback` to translate if the given key does not exist in the locale.

type Message

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

Message represents a message in a locale.

func (*Message) Translate

func (m *Message) Translate(args ...interface{}) string

Translate translates the message with the supplied list of arguments.

type Store

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

Store contains a collection of locales and their descriptive names.

func NewStore

func NewStore() *Store

NewStore initializes and returns a new Store.

func (*Store) AddLocale

func (s *Store) AddLocale(lang, desc string, source interface{}, others ...interface{}) (*Locale, error)

AddLocale adds a locale with given language name and description that is loaded from the list of sources. Please refer to INI documentation regarding what is considered as a valid data source: https://ini.unknwon.io/docs/howto/load_data_sources.

func (*Store) Locale

func (s *Store) Locale(lang string) (*Locale, error)

Locale returns the locale with the given language name.

Directories

Path Synopsis
internal
plural
Package plural provides support for pluralizing messages according to CLDR rules defined in http://cldr.unicode.org/index/cldr-spec/plural-rules.
Package plural provides support for pluralizing messages according to CLDR rules defined in http://cldr.unicode.org/index/cldr-spec/plural-rules.

Jump to

Keyboard shortcuts

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