ii18n

package module
v0.0.0-...-03d0635 Latest Latest
Warning

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

Go to latest
Published: May 31, 2019 License: MIT Imports: 6 Imported by: 0

README

II18N

GoDoc Go Report Card MIT licensed

Go i18n library.

Download & Install

go get github.com/syyongx/ii18n

Quick Start

import github.com/syyongx/ii18n

func main() {
    config := map[string]Config{
        "app": Config{
            SourceNewFunc: NewJSONSource,
            OriginalLang:  "en-US",
            BasePath:      "./testdata",
            FileMap: map[string]string{
                "app":   "app.json",
                "error": "error.json",
            },
        },
    }
    NewI18N(config)
    message := T("app", "hello", nil, "zh-CN")
}

Apis

NewI18N(config map[string]Config) *I18N
T(category string, message string, params map[string]string, lang string) string

LICENSE

II18N source code is licensed under the MIT Licence.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOriginalLang = "en-US"

DefaultOriginalLang default original language

Functions

func T

func T(category string, message string, params map[string]string, lang string) string

T translate. 1. T('common', 'hot', [], 'zh-CN') // default app.common 2. T('app.common', 'hot', [], 'zh-CN') // result same to 1. 3. T('msg.a', 'hello', ['{foo}' => 'bar', '{key}' => 'val'] 'ja-JP')

Types

type Config

type Config struct {
	SourceNewFunc    func(*Config) Source
	OriginalLang     string
	ForceTranslation bool
	BasePath         string
	FileMap          map[string]string
	// contains filtered or unexported fields
}

Config config

type Formatter

type Formatter struct {
}

Formatter Formatter

func NewFormatter

func NewFormatter() *Formatter

NewFormatter New Formatter

type I18N

type I18N struct {
	Translations map[string]*Config
	// contains filtered or unexported fields
}

I18N i18n

var Translator *I18N

Translator translator

func NewI18N

func NewI18N(config map[string]Config) *I18N

NewI18N returns an instance of I18N.

type JSONSource

type JSONSource struct {
	MessageSource
}

JSONSource JSONSource

func (*JSONSource) GetMsgFilePath

func (js *JSONSource) GetMsgFilePath(category string, lang string) string

GetMsgFilePath Get messages file path.

type MessageSource

type MessageSource struct {
	// string the language that the original messages are in
	OriginalLang     string
	ForceTranslation bool
	BasePath         string
	FileMap          map[string]string
	// contains filtered or unexported fields
}

MessageSource MessageSource

func (*MessageSource) GetMsgFilePath

func (ms *MessageSource) GetMsgFilePath(category string, lang string) string

GetMsgFilePath Get messages file path.

func (*MessageSource) LoadFallbackMsgs

func (ms *MessageSource) LoadFallbackMsgs(category string, fallbackLang string, msgs TMsgs, originalMsgFile string) (TMsgs, error)

LoadFallbackMsgs Loads the message translation for the specified $language and $category. If translation for specific locale code such as `en-US` isn't found it tries more generic `en`. When both are present, the `en-US` messages will be merged

func (*MessageSource) LoadMsgs

func (ms *MessageSource) LoadMsgs(category string, lang string) (TMsgs, error)

LoadMsgs Loads the message translation for the specified $language and $category. If translation for specific locale code such as `en-US` isn't found it tries more generic `en`. When both are present, the `en-US` messages will be merged over `en`. See [[loadFallbackTMsgs]] for details. If the lang is less specific than [[originalLang]], the method will try to load the messages for [[originalLang]]. For example: [[originalLang]] is `en-GB`, language is `en`. The method will load the messages for `en` and merge them over `en-GB`.

func (*MessageSource) Translate

func (ms *MessageSource) Translate(category string, message string, lang string) (string, error)

Translate translate

func (*MessageSource) TranslateMsg

func (ms *MessageSource) TranslateMsg(category string, message string, lang string) (string, error)

TranslateMsg translate message

type Source

type Source interface {
	Translate(category string, message string, lang string) (string, error)
	TranslateMsg(category string, message string, lang string) (string, error)
	GetMsgFilePath(category string, lang string) string
	LoadMsgs(category string, lang string) (TMsgs, error)
	LoadFallbackMsgs(category string, fallbackLang string, msgs TMsgs, originalMsgFile string) (TMsgs, error)
}

Source interface

func NewJSONSource

func NewJSONSource(conf *Config) Source

NewJSONSource New JSONSource

type TMsgs

type TMsgs map[string]string

TMsgs type messages

func LoadMsgsFromFile

func LoadMsgsFromFile(filename string) (TMsgs, error)

LoadMsgsFromFile Get messages file path.

Jump to

Keyboard shortcuts

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