Documentation
¶
Overview ¶
Example (BindDomain) ¶
Example_bindDomain 绑定到指定文本域
package main
import (
"fmt"
"sync"
"github.com/youthlin/t"
)
var mu sync.Mutex
func markSeq() func() {
mu.Lock()
t.SetGlobal(t.NewTranslations())
return func() { mu.Unlock() }
}
func main() {
defer markSeq()()
t.SetLocale("zh")
t.Bind("main", "testdata/zh_CN.po")
fmt.Println("HasDomain(main) =", t.HasDomain("main"))
fmt.Println("HasDomain(no) =", t.HasDomain("no"))
fmt.Println("Domains =", t.Domains())
fmt.Println(t.T("Hello, World"))
t.SetDomain("main")
fmt.Println(t.T("Hello, World"))
}
Output: HasDomain(main) = true HasDomain(no) = false Domains = [main] Hello, World 你好,世界
Example (Gettext) ¶
package main
import (
"fmt"
"sync"
"github.com/youthlin/t"
)
var mu sync.Mutex
func markSeq() func() {
mu.Lock()
t.SetGlobal(t.NewTranslations())
return func() { mu.Unlock() }
}
func main() {
defer markSeq()()
t.Load("testdata")
t.SetLocale("zh_CN")
fmt.Println(t.T("Hello, World")) // 你好,世界
fmt.Println(t.T("Hello, %s", "Tom")) // 你好,世界
fmt.Println(t.N("One apple", "%d apples", 1)) // %d 个苹果
fmt.Println(t.N("One apple", "%d apples", 1, 1)) // 1 个苹果
fmt.Println(t.N("One apple", "%d apples", 2)) // %d 个苹果
fmt.Println(t.N("One apple", "%d apples", 2, 2)) // 2 个苹果
fmt.Println(t.N64("One apple", "%d apples", 200, 200)) // 200 个苹果
fmt.Println(t.X("File|", "Open")) // 打开文件
fmt.Println(t.X("Project|", "Open")) // 打开工程
fmt.Println(t.XN("File|", "Open One", "Open %d", 1, 1)) // 打开 1 个文件
fmt.Println(t.XN("Project|", "Open One", "Open %d", 1)) // 打开 %d 个工程
fmt.Println(t.XN("Project|", "Open One", "Open %d", 1, 1)) // 打开 1 个工程
fmt.Println(t.XN64("Project|", "Open One", "Open %d", 100, 100)) // 打开 100 个工程
}
Output: 你好,世界 你好,Tom %d 个苹果 1 个苹果 %d 个苹果 2 个苹果 200 个苹果 打开文件 打开工程 打开 1 个文件 打开 %d 个工程 打开 1 个工程 打开 100 个工程
Example (InitFS) ¶
Example_initFS LoadFS 绑定文件系统到默认文本域
package main
import (
"embed"
"fmt"
"sync"
"github.com/youthlin/t"
)
var mu sync.Mutex
func markSeq() func() {
mu.Lock()
t.SetGlobal(t.NewTranslations())
return func() { mu.Unlock() }
}
//go:embed testdata
var pathFS embed.FS
func main() {
defer markSeq()()
t.LoadFS(pathFS)
t.SetLocale("zh")
fmt.Println(t.T("Hello, World"))
}
Output: 你好,世界
Example (Init_file) ¶
Example_init_file Load 绑定 path 到默认文本域, path 可以是单个文件
package main
import (
"fmt"
"sync"
"github.com/youthlin/t"
)
var mu sync.Mutex
func markSeq() func() {
mu.Lock()
t.SetGlobal(t.NewTranslations())
return func() { mu.Unlock() }
}
func main() {
defer markSeq()()
t.Load("testdata/zh_CN.po")
// will normalize to ll_CC form => zh_CN
// 会格式化为 ll_CC 的形式
t.SetLocale("zh_hans")
fmt.Println(t.T("Hello, World"))
}
Output: 你好,世界
Example (Init_fileFS) ¶
Example_init_fileFS embed.FS 可以是单个文件或文件夹
package main
import (
"embed"
"fmt"
"sync"
"github.com/youthlin/t"
)
var mu sync.Mutex
func markSeq() func() {
mu.Lock()
t.SetGlobal(t.NewTranslations())
return func() { mu.Unlock() }
}
//go:embed testdata/zh_CN.po
var fileFS embed.FS
func main() {
defer markSeq()()
t.LoadFS(fileFS)
t.SetLocale("zh_hans")
fmt.Println("Current locale =", t.Locale()) // zh_CN
fmt.Println(t.T("Hello, World"))
// 设置不支持的语言,会原样返回
t.SetLocale("zh_hant")
fmt.Println("Current locale =", t.Locale()) // zh_TW
fmt.Println(t.T("Hello, World"))
}
Output: Current locale = zh_CN 你好,世界 Current locale = zh_TW Hello, World
Example (Init_path) ¶
Example_init_path Load 绑定 path 到默认文本域
package main
import (
"fmt"
"sync"
"github.com/youthlin/t"
)
var mu sync.Mutex
func markSeq() func() {
mu.Lock()
t.SetGlobal(t.NewTranslations())
return func() { mu.Unlock() }
}
func main() {
defer markSeq()()
t.Load("testdata")
// empty means system default locale
// 设置为使用系统语言这一步骤可以省略,因为初始化时就是使用系统语言
t.SetLocale("")
// 为了能在其他环境测试通过,所以指定 zh_CN
t.SetLocale("zh_CN")
fmt.Println(t.T("Hello, World"))
}
Output: 你好,世界
Example (Locale) ¶
Example_locale 语言设置示例
package main
import (
"embed"
"fmt"
"sync"
"github.com/youthlin/t"
)
var mu sync.Mutex
func markSeq() func() {
mu.Lock()
t.SetGlobal(t.NewTranslations())
return func() { mu.Unlock() }
}
//go:embed testdata
var pathFS embed.FS
func main() {
defer markSeq()()
t.LoadFS(pathFS)
t.SetLocale("zh")
fmt.Println("UsedLocale =", t.UsedLocale()) // zh_CN
t.SetLocale("zh_TW")
fmt.Println("UsedLocale =", t.UsedLocale()) // en_US
fmt.Println("SourceCodeLocale =", t.SourceCodeLocale()) // en_US
ts := t.NewTranslations()
ts.SetSourceCodeLocale("zh_CN")
ts.SetLocale("en_US")
fmt.Println("empty ts SourceCodeLocale =", ts.SourceCodeLocale()) // zh_CN
fmt.Println("empty ts Locale =", ts.Locale()) // en_US
fmt.Println("empty ts UsedLocale =", ts.UsedLocale()) // zh_CN
fmt.Println(ts.T("你好,世界"))
}
Output: UsedLocale = zh_CN UsedLocale = en_US SourceCodeLocale = en_US empty ts SourceCodeLocale = zh_CN empty ts Locale = en_US empty ts UsedLocale = zh_CN 你好,世界
Example (Locales) ¶
package main
import (
"fmt"
"sync"
"github.com/youthlin/t"
)
var mu sync.Mutex
func markSeq() func() {
mu.Lock()
t.SetGlobal(t.NewTranslations())
return func() { mu.Unlock() }
}
func main() {
defer markSeq()()
fmt.Println(t.Locales()) // [en_US] // SourceCodeLocale
t.Load("testdata")
fmt.Println(t.Locales()) // [en_US zh_CN]
t.Bind("main", "testdata")
fmt.Println(t.D("main").Locales()) // [en_US zh_CN]
fmt.Println(t.D("no-such-domain").Locales()) // [en_US]
}
Output: [en_US] [en_US zh_CN] [en_US zh_CN] [en_US]
Example (With) ¶
package main
import (
"fmt"
"sync"
"github.com/youthlin/t"
)
var mu sync.Mutex
func markSeq() func() {
mu.Lock()
t.SetGlobal(t.NewTranslations())
return func() { mu.Unlock() }
}
func main() {
defer markSeq()()
t.Bind("main", "testdata")
l := t.L("zh_CN")
fmt.Println(l.T("Hello, World")) // Hello, World
d := t.D("main").L("zh_CN")
fmt.Println(d.T("Hello, World")) // 你好,世界
}
Output: Hello, World 你好,世界
Index ¶
- Constants
- Variables
- func Bind(domain, path string)
- func BindFS(domain string, fsys fs.FS)
- func Domain() string
- func Domains() []string
- func GetCtxDomain(ctx context.Context) (string, bool)
- func GetCtxLocale(ctx context.Context) (string, bool)
- func GetUserLang(request *http.Request, opts ...getUserLangOpt) string
- func HasDomain(domain string) bool
- func Load(path string)
- func LoadFS(fsys fs.FS)
- func Locale() string
- func Locales() []string
- func Match(supported []string, userAccept []string) (tag language.Tag, index int, c language.Confidence)
- func MatchTag(supportedTags []language.Tag, userAcceptTags []language.Tag) (tag language.Tag, index int, c language.Confidence)
- func MostMatchLocale() string
- func N(msgID, msgIDPlural string, n int, args ...any) string
- func N1(msgId string, n int, args ...any) string
- func N1_64(msgId string, n int64, args ...any) string
- func N64(msgID, msgIDPlural string, n int64, args ...any) string
- func SetCtxDomain(ctx context.Context, domain string) context.Context
- func SetCtxLocale(ctx context.Context, lang string) context.Context
- func SetDomain(domain string)
- func SetGlobal(g *Translations)
- func SetLocale(locale string)
- func SetSourceCodeLocale(locale string)
- func SourceCodeLocale() string
- func T(msgID string, args ...any) string
- func Tag(locale string) language.Tag
- func Tags(locales []string) (tags []language.Tag)
- func UsedLocale() string
- func WithCookieName(cookieName string) getUserLangOpt
- func X(msgCtxt, msgID string, args ...any) string
- func XN(msgCtxt, msgID, msgIDPlural string, n int, args ...any) string
- func XN1(msgCtxt, msgID string, n int, args ...any) string
- func XN1_64(msgCtxt, msgID string, n int64, args ...any) string
- func XN64(msgCtxt, msgID, msgIDPlural string, n int64, args ...any) string
- type Translation
- func (tr *Translation) AddOrReplace(tor Translator) Translator
- func (tr *Translation) Get(lang string) (Translator, bool)
- func (tr *Translation) GetOrNoop(lang string) Translator
- func (tr *Translation) LoadFS(f fs.FS) bool
- func (tr *Translation) LoadFile(file fs.File) error
- func (tr *Translation) LoadMo(content []byte) error
- func (tr *Translation) LoadPo(content []byte) error
- type Translations
- func (ts *Translations) BindFS(domain string, fsys fs.FS)
- func (ts *Translations) D(domain string) *Translations
- func (ts *Translations) Domain() string
- func (ts *Translations) Domains() (domains []string)
- func (ts *Translations) Get(domain string) (*Translation, bool)
- func (ts *Translations) GetOrNoop(domain string) *Translation
- func (ts *Translations) HasDomain(domain string) bool
- func (ts *Translations) L(locale string) *Translations
- func (ts *Translations) Locale() string
- func (ts *Translations) Locales() (locales []string)
- func (ts *Translations) MostMatchLocale() string
- func (ts *Translations) N(msgID, msgIDPlural string, n int, args ...any) string
- func (ts *Translations) N1(msgID string, n int, args ...any) string
- func (ts *Translations) N1_64(msgID string, n int64, args ...any) string
- func (ts *Translations) N64(msgID, msgIDPlural string, n int64, args ...any) string
- func (ts *Translations) SetDomain(domain string)
- func (ts *Translations) SetLocale(lang string)
- func (ts *Translations) SetSourceCodeLocale(lang string)
- func (ts *Translations) SourceCodeLocale() string
- func (ts *Translations) T(msgID string, args ...any) string
- func (ts *Translations) UsedLocale() string
- func (ts *Translations) X(msgCtxt, msgID string, args ...any) string
- func (ts *Translations) XN(msgCtxt, msgID, msgIDPlural string, n int, args ...any) string
- func (ts *Translations) XN1(msgCtxt, msgID string, n int, args ...any) string
- func (ts *Translations) XN1_64(msgCtxt, msgID string, n int64, args ...any) string
- func (ts *Translations) XN64(msgCtxt, msgID, msgIDPlural string, n int64, args ...any) string
- type Translator
Examples ¶
Constants ¶
const DefaultDomain = "default"
DefaultDomain 默认的文本域
const DefaultSourceCodeLocale = "en_US"
DefaultSourceCodeLocale 默认的源代码语言
const HTTPHeaderAcceptLanguage = "Accept-Language"
Variables ¶
var Mark = noop(0)
Mark is used to mark translation texts
Functions ¶
func Bind ¶ added in v0.0.4
func Bind(domain, path string)
Bind bind translation from path to specified domain
func GetUserLang ¶ added in v0.0.7
func Load ¶ added in v0.0.4
func Load(path string)
Load load translation from path to current domain
func Locales ¶ added in v0.0.4
func Locales() []string
Locales return all supported locales of current used domain 返回当前文本域中支持的所有语言
func MostMatchLocale ¶ added in v0.0.6
func MostMatchLocale() string
MostMatchLocale return the most match language 返回最匹配的语言
func N1 ¶ added in v0.1.3
N1 用于单复数同形的简写, 比如 N("%d 个苹果", "%d 个苹果", n, n) 可以简写为 N1("%d 个苹果", n, n) 同时抽取脚本使用 -kN1:1,1 指定, 可正常抽取出 msgid_plural
Example ¶
ExampleN1 单复数同形简写,无翻译时原样返回
defer markSeq()()
fmt.Println(t.N1("%d 个苹果", 1))
fmt.Println(t.N1("%d 个苹果", 1, 1))
fmt.Println(t.N1("%d 个苹果", 2))
fmt.Println(t.N1("%d 个苹果", 2, 2))
Output: %d 个苹果 1 个苹果 %d 个苹果 2 个苹果
Example (WithTranslation) ¶
ExampleN1_withTranslation 加载翻译后 N1 正常工作
defer markSeq()()
t.Load("testdata")
t.SetLocale("zh_CN")
// zh_CN.po 中 "One apple" 的翻译是 "%d 个苹果"
fmt.Println(t.N1("One apple", 1))
fmt.Println(t.N1("One apple", 1, 1))
fmt.Println(t.N1("One apple", 2))
fmt.Println(t.N1("One apple", 2, 2))
Output: %d 个苹果 1 个苹果 %d 个苹果 2 个苹果
func SetCtxDomain ¶ added in v0.0.7
func SetCtxLocale ¶ added in v0.0.7
func SetGlobal ¶ added in v0.0.4
func SetGlobal(g *Translations)
SetGlobal set the global Translations instance
func SetSourceCodeLocale ¶ added in v0.0.4
func SetSourceCodeLocale(locale string)
SetSourceCodeLocale 设置源代码的语言
func UsedLocale ¶ added in v0.0.4
func UsedLocale() string
UsedLocale return the actually used locale
func WithCookieName ¶ added in v0.0.7
func WithCookieName(cookieName string) getUserLangOpt
Types ¶
type Translation ¶
type Translation struct {
// contains filtered or unexported fields
}
Translation can provide different language translation of a domain tr. [翻译域]包含各个语言的翻译
func NewTranslation ¶
func NewTranslation(domain string, translators ...Translator) *Translation
NewTranslation create a new Translation
func (*Translation) AddOrReplace ¶
func (tr *Translation) AddOrReplace(tor Translator) Translator
AddOrReplace add a translator and return the previous translator of this language
func (*Translation) Get ¶ added in v0.0.4
func (tr *Translation) Get(lang string) (Translator, bool)
Get get the Translator of the specified lang
func (*Translation) GetOrNoop ¶ added in v0.0.4
func (tr *Translation) GetOrNoop(lang string) Translator
GetOrNoop return the Translator of the specified language 获取指定语言的翻译
func (*Translation) LoadFS ¶ added in v0.0.4
func (tr *Translation) LoadFS(f fs.FS) bool
LoadFS load a translator from file system
func (*Translation) LoadFile ¶ added in v0.0.4
func (tr *Translation) LoadFile(file fs.File) error
LoadFile load a translator from a file
func (*Translation) LoadMo ¶ added in v0.0.4
func (tr *Translation) LoadMo(content []byte) error
LoadMo load mo file
func (*Translation) LoadPo ¶ added in v0.0.4
func (tr *Translation) LoadPo(content []byte) error
LoadPo load po file
type Translations ¶
type Translations struct {
// contains filtered or unexported fields
}
Translations holds several translation domains ts. [翻译集]包含多个翻译,每个翻译分别属于一个文本域
func D ¶ added in v0.0.4
func D(domain string) *Translations
D return a new Translations with domain
func Global ¶ added in v0.0.4
func Global() *Translations
Global return the global Translations instance
func L ¶ added in v0.0.4
func L(locale string) *Translations
L return a new Translations with locale
func NewTranslations ¶
func NewTranslations() *Translations
NewTranslations create a new Translations 新建翻译集
func WithContext ¶ added in v0.0.7
func WithContext(ctx context.Context) *Translations
func (*Translations) BindFS ¶ added in v0.0.4
func (ts *Translations) BindFS(domain string, fsys fs.FS)
BindFS load a Translation form file system and bind to a domain 从文件系统绑定翻译域
func (*Translations) D ¶ added in v0.0.4
func (ts *Translations) D(domain string) *Translations
D return a new Translations with domain
func (*Translations) Domain ¶ added in v0.0.4
func (ts *Translations) Domain() string
Domain return current domain 返回当前使用的文本域
func (*Translations) Domains ¶ added in v0.0.4
func (ts *Translations) Domains() (domains []string)
Domains return all domains
func (*Translations) Get ¶
func (ts *Translations) Get(domain string) (*Translation, bool)
Get return the Translation of the specified domain 获取指定的的翻译域
func (*Translations) GetOrNoop ¶
func (ts *Translations) GetOrNoop(domain string) *Translation
GetOrNoop return the Translation of the specified domain 获取指定的的翻译域
func (*Translations) HasDomain ¶ added in v0.0.4
func (ts *Translations) HasDomain(domain string) bool
HasDomain return true if ts has the specified domain
func (*Translations) L ¶ added in v0.0.4
func (ts *Translations) L(locale string) *Translations
L return a new Translations with locale
func (*Translations) Locale ¶
func (ts *Translations) Locale() string
Locale return current locale 返回设置的希望使用的语言
func (*Translations) Locales ¶ added in v0.0.4
func (ts *Translations) Locales() (locales []string)
Locales return all supported locales of domain 返回文本域中支持的所有语言
func (*Translations) MostMatchLocale ¶ added in v0.0.6
func (ts *Translations) MostMatchLocale() string
MostMatchLocale return the most match language 返回最匹配的语言
func (*Translations) N ¶
func (ts *Translations) N(msgID, msgIDPlural string, n int, args ...any) string
N is a short name of nettext
func (*Translations) N1 ¶ added in v0.1.3
func (ts *Translations) N1(msgID string, n int, args ...any) string
N1 用于单复数同形的语言
Example ¶
ExampleTranslations_N1 Translations 上的 N1 方法
defer markSeq()()
t.Load("testdata")
ts := t.D("default").L("zh_CN")
fmt.Println(ts.N1("One apple", 1))
fmt.Println(ts.N1("One apple", 1, 1))
fmt.Println(ts.N1("One apple", 3))
fmt.Println(ts.N1("One apple", 3, 3))
Output: %d 个苹果 1 个苹果 %d 个苹果 3 个苹果
func (*Translations) N1_64 ¶ added in v0.1.6
func (ts *Translations) N1_64(msgID string, n int64, args ...any) string
N1_64 用于单复数同形的语言, n 为 int64
func (*Translations) N64 ¶
func (ts *Translations) N64(msgID, msgIDPlural string, n int64, args ...any) string
N64 is a short name of nettext
func (*Translations) SetDomain ¶ added in v0.0.4
func (ts *Translations) SetDomain(domain string)
SetDomain set current domain 设置要使用的文本域
func (*Translations) SetLocale ¶
func (ts *Translations) SetLocale(lang string)
SetLocale set current locale 设置要使用的语言
func (*Translations) SetSourceCodeLocale ¶ added in v0.0.4
func (ts *Translations) SetSourceCodeLocale(lang string)
SetSourceCodeLocale 设置源代码语言
func (*Translations) SourceCodeLocale ¶ added in v0.0.4
func (ts *Translations) SourceCodeLocale() string
SourceCodeLocale 设置源代码语言
func (*Translations) T ¶
func (ts *Translations) T(msgID string, args ...any) string
T is a short name of gettext
func (*Translations) UsedLocale ¶ added in v0.0.4
func (ts *Translations) UsedLocale() string
UsedLocale return the locale that actually used
func (*Translations) X ¶
func (ts *Translations) X(msgCtxt, msgID string, args ...any) string
X is a short name of pgettext
func (*Translations) XN ¶
func (ts *Translations) XN(msgCtxt, msgID, msgIDPlural string, n int, args ...any) string
XN is a short name of npgettext
func (*Translations) XN1 ¶ added in v0.1.6
func (ts *Translations) XN1(msgCtxt, msgID string, n int, args ...any) string
XN1 单复数同形简写, n 为 int
type Translator ¶ added in v0.0.4
type Translator = translator.Translator
Translator 翻译接口
func NoopTranslator ¶ added in v0.0.4
func NoopTranslator() Translator
NoopTranslator return a no-op Translator