i18n

package module
v2.0.0-...-fd1bf06 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

i18n

Build Status GitHub license Go Reference

v2 版本开发中

Getting started

Installation:

go get -u github.com/kingzcheung/i18n/v2

example:

package main

import (
	"encoding/json"
	"fmt"
	"github.com/kingzcheung/i18n/v2"
	"github.com/kingzcheung/i18n/v2/testdata"
	"golang.org/x/text/language"
)

func main() {

	bundle := i18n.NewBundle(language.English)
	//err := bundle.LoadMessageFromFile("lang/en.json", func(bytes []byte, m *map[string]interface{}) error {
	//	return json.Unmarshal(bytes,m)
	//})
	//if err != nil {
	//	panic(err)
	//}
	data, err := testdata.TestDataFs.ReadFile("lang/en.json")
	if err != nil {
		panic(err)
	}
	err = bundle.LoadMessageFromBytes(data, language.English, func(bytes []byte, m *map[string]interface{}) error {
		return json.Unmarshal(bytes, m)
	})
	if err != nil {
		panic(err)
	}

	// or
	entries, err := testdata.TestDataFs.ReadDir("lang")
	if err != nil {
		panic(err)
	}
	_ = bundle.LoadMessageFromFsEntries(entries, func(bytes []byte, m *map[string]interface{}) error {
		return json.Unmarshal(bytes, m)
	})

	e := i18n.NewLocalization(bundle, "zh-CN")

	fmt.Println(e.Localize("Antarctica"))                                      //南极洲
	fmt.Println(e.Localize("Africa", map[string]interface{}{"welcome": "你好"})) //非洲,你好
	fmt.Println(e.With("zh-HK").Localize("Asia"))                              //亞洲
	fmt.Println(e.With("en").Localize("Asia"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bundle

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

func NewBundle

func NewBundle(defaultLanguage language.Tag) *Bundle

func (*Bundle) DefaultLanguage

func (b *Bundle) DefaultLanguage() language.Tag

func (*Bundle) LoadMessageFromBytes

func (b *Bundle) LoadMessageFromBytes(s []byte, tag language.Tag, umn typ.UnmarshalFunc) error

func (*Bundle) LoadMessageFromDir

func (b *Bundle) LoadMessageFromDir(dir string, umn typ.UnmarshalFunc) error

func (*Bundle) LoadMessageFromFile

func (b *Bundle) LoadMessageFromFile(filename string, umn typ.UnmarshalFunc) error

func (*Bundle) LoadMessageFromFsEntries

func (b *Bundle) LoadMessageFromFsEntries(entries []fs.DirEntry, umn typ.UnmarshalFunc) error

func (*Bundle) LoadMessageFromString

func (b *Bundle) LoadMessageFromString(s string, tag language.Tag, umn typ.UnmarshalFunc) error

func (*Bundle) Locales

func (b *Bundle) Locales() []*locale

func (*Bundle) ParseMessageFromBytes

func (b *Bundle) ParseMessageFromBytes(rf []byte, umn typ.UnmarshalFunc) (map[string]string, error)

type Localization

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

func NewLocalization

func NewLocalization(bundle *Bundle, fallbackLangs ...string) *Localization

func (*Localization) Localize

func (l *Localization) Localize(key string, variables ...map[string]interface{}) string

func (*Localization) With

func (l *Localization) With(lang string) *Localization

func (*Localization) WithRequest

func (l *Localization) WithRequest(r *http.Request) *Localization

func (*Localization) WithTag

func (l *Localization) WithTag(tag language.Tag) *Localization

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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