langx

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package langx 是内置界面文本的 i18n 层:enum 语言 + 进程级目录 + 用户 覆盖。默认英文(xyz 的规范默认);中文随库携带。选择顺序(根派发器负责 落地):--xyz.lang flag > Config.Lang > LANG/LC_ALL 环境检测 > 英文。

T(key) 返回该语言下的文本;Tf(key, params...) 用 {0}、{1}… 占位符做 参数代入(自研最小格式化器,零第三方依赖)。覆盖表(代码侧配置)优先于 内置译文;未命中任何语言的键回退到键名本身(绝不 panic)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Set

func Set(l Language, override map[string]string)

Set 设置进程级语言与可选覆盖表(override 键覆盖内置译文;可为 nil)。 根派发器在解析完配置后调用;嵌入场景可自行调用。

func T

func T(key string) string

T 返回当前语言下 key 的文本:覆盖 > 内置 > 键名回退。

func Tctx added in v0.4.4

func Tctx(ctx context.Context, key string) string

Tctx 用 ctx 语言翻译(缺省回退进程语言):覆盖 > 内置 > 键名回退。

func Tf

func Tf(key string, params ...string) string

Tf 是带参数的 T:模板中的 {0}、{1}… 依次被 params 替换。

func Tfctx added in v0.4.4

func Tfctx(ctx context.Context, key string, params ...string) string

Tfctx 是带参数的 Tctx:模板中的 {0}、{1}… 依次被 params 替换。

func WithLang added in v0.4.4

func WithLang(ctx context.Context, l Language) context.Context

WithLang 返回携带指定语言的 context(如 HTTP 从 Accept-Language 解析后)。

Types

type Language

type Language int

Language 是受支持的语言。零值 En(规范默认)。

const (
	En Language = iota
	ZhCn
)

func Detect

func Detect() Language

Detect 按 LANG/LC_ALL 环境做语言检测:zh 前缀 → 中文,其余(C/POSIX/ 缺失)→ 英文。

func FromCtx added in v0.4.4

func FromCtx(ctx context.Context) (Language, bool)

FromCtx 取出 ctx 携带的语言;未携带则回退到当前进程语言,第二返回值为 false(表示用的是回退值)。

func Lang

func Lang() Language

Lang 返回当前语言(零配置下为 En)。

func Parse

func Parse(s string) (Language, bool)

Parse 把 --xyz.lang 的取值映射成语言;未知值返回 false(注册期报错)。

func ParseAcceptLanguage added in v0.4.4

func ParseAcceptLanguage(header string) (Language, bool)

ParseAcceptLanguage 解析 HTTP Accept-Language 头,返回最匹配的受支持语言 (zh* → ZhCn,en* → En),按 q 值择优(缺省 q=1)。无可解析的受支持标签 时返回 (En, false),调用方据此回退到默认语言。示例:

"zh-CN,zh;q=0.9,en;q=0.8" → (ZhCn, true)
"en-US,en;q=0.9"          → (En, true)
"fr,de;q=0.9"             → (En, false)  // 无受支持标签

func (Language) String

func (l Language) String() string

String 返回 --xyz.lang 的取值形态。

Jump to

Keyboard shortcuts

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