language

package
v1.22.13 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: Apache-2.0 Imports: 16 Imported by: 25

Documentation

Overview

Copyright 2016 Wenhui Shen <www.webx.top>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var (
	LangVarName = `lang`
	DefaultLang = `zh-CN`
)

Functions

func ParseHeader added in v1.6.0

func ParseHeader(al string, n int) []string

func SetDefault added in v1.14.3

func SetDefault(ins *I18n)

func T

func T(langCode, key string, args ...interface{}) (t string)

T 多语言翻译

Types

type Config

type Config struct {
	Project  string
	Default  string
	Fallback string
	AllList  []string

	// key: language, value: map[key]value.
	//
	// Example:
	// {
	// 	"en":{
	// 		"label":"English",
	// 		"flag":"🇺🇸"
	// 	},
	// 	"zh-CN":{
	// 		"label":"简体中文",
	// 		"flag":"🇨🇳"
	// 	},
	// }
	Extra map[string]param.Store

	RulesPath    []string
	MessagesPath []string

	// Reload indicates whether to reload the language file each time it is modified.
	Reload bool
	// contains filtered or unexported fields
}

func (Config) Clone added in v1.21.3

func (c Config) Clone() Config

func (Config) ExtraBy added in v1.22.9

func (c Config) ExtraBy(lang string) param.Store

ExtraBy returns the extra parameters for the specified language. If no extra parameters exist for the language or Extra is nil, returns an empty Store.

func (Config) FSFunc added in v1.3.0

func (c Config) FSFunc() func(string) http.FileSystem

FSFunc returns the configured http.FileSystem function.

func (*Config) Init added in v1.22.12

func (c *Config) Init()

func (Config) KVList added in v1.22.12

func (c Config) KVList() echo.KVList

func (*Config) SetFSFunc added in v1.3.0

func (c *Config) SetFSFunc(fsFunc func(string) http.FileSystem) *Config

type I18n

type I18n struct {
	*i18n.TranslatorFactory
	// contains filtered or unexported fields
}

func NewI18n

func NewI18n(c *Config) *I18n

func (*I18n) Close added in v1.22.13

func (a *I18n) Close()

func (*I18n) Get

func (a *I18n) Get(langCode string) *i18n.Translator

func (*I18n) GetAndCache added in v1.6.0

func (a *I18n) GetAndCache(langCode string) *i18n.Translator

func (*I18n) Monitor

func (a *I18n) Monitor() *I18n

func (*I18n) Reload

func (a *I18n) Reload(langCode string)

func (*I18n) T

func (a *I18n) T(langCode, key string, args ...interface{}) (t string)

func (*I18n) Translate

func (a *I18n) Translate(langCode, key string, args map[string]string) string

type Language

type Language struct {
	List    map[string]bool //语种列表
	Index   []string        //索引
	Default string          //默认语种
	I18n    *I18n
	// contains filtered or unexported fields
}

func New

func New(c ...*Config) *Language

func (*Language) AcquireTranslator added in v1.20.0

func (a *Language) AcquireTranslator(langCode string) *Translate

func (*Language) Close added in v1.22.13

func (a *Language) Close()

func (*Language) Config added in v1.18.0

func (a *Language) Config() *Config

func (*Language) DetectHeader added in v1.1.0

func (a *Language) DetectHeader(r engine.Request) string

func (*Language) DetectURI

func (a *Language) DetectURI(c echo.Context) string

func (*Language) Handler added in v1.6.0

func (a *Language) Handler(e echo.RouteRegister, i18nJSVarName string)

func (*Language) Init

func (a *Language) Init(c *Config)

func (*Language) Middleware

func (a *Language) Middleware() echo.MiddlewareFunc

func (*Language) ReleaseTranslator added in v1.21.0

func (a *Language) ReleaseTranslator(tr *Translate)

func (*Language) Set

func (a *Language) Set(lang string, on bool, args ...bool) *Language

Set 记录语言

  • on: 使用启用此语言
  • args[0]: setDefault 是否设置为默认语言
  • args[1]: normalized 是否已经标准格式化 lang 值

func (*Language) Valid

func (a *Language) Valid(lang string) bool

type Translate

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

func NewTranslate

func NewTranslate(language string, langObject *Language) *Translate

NewTranslate creates a new Translate instance and initializes it with the given language and language object. Returns a pointer to the initialized Translate struct.

func (*Translate) E added in v1.4.3

func (t *Translate) E(format string, args ...interface{}) error

E returns a new error with the translated message using the given format string and arguments.

func (*Translate) Lang

func (t *Translate) Lang() echo.LangCode

Lang returns the language code of the Translate instance

func (*Translate) LangDefault added in v1.18.0

func (t *Translate) LangDefault() string

LangDefault default language

func (*Translate) LangExists added in v1.18.0

func (t *Translate) LangExists(langCode string) bool

LangExists language code exists

func (*Translate) LangList added in v1.18.0

func (t *Translate) LangList() []string

LangList language list

func (*Translate) Release added in v1.19.2

func (t *Translate) Release()

Release releases the Translate instance resources and returns it to the pool if it was created from a pool. It sets both code and lang references to nil and optionally returns the instance to the translatePool.

func (*Translate) Reset added in v1.6.0

func (t *Translate) Reset(language string, langObject *Language) *Translate

Reset sets the language code and language object for the translator language: the language code to set langObject: the language object containing translations Returns the modified Translate instance for method chaining

func (*Translate) SetLang

func (t *Translate) SetLang(lang string)

SetLang sets the language code for translation using the specified language string.

func (*Translate) T

func (t *Translate) T(format string, args ...interface{}) string

T translates the given format string using the current language code and optional arguments. Returns the translated string.

Jump to

Keyboard shortcuts

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