golang实现GNU gettext国际化文本处理的插件库gotext的使用

Golang实现GNU gettext国际化文本处理的插件库gotext的使用

简介

Gotext是一个用于Go语言的GNU gettext工具实现,支持PO文件格式和国际化文本处理。

GitHub release

MIT license

特性

  • 原生Go实现的GNU gettext支持
  • 完整支持PO文件,包括:
    • 多行字符串和头部支持
    • 使用Go的fmt语法在翻译字符串中支持变量
    • 支持复数形式规则
    • 支持消息上下文
  • 支持MO文件
  • 线程安全:可在多个goroutine中并发使用
  • 使用UTF-8编码
  • 语言代码自动简化(如en_UK到en)
  • 可在Go模板中使用
  • 对象可序列化为[]byte以便缓存
  • 支持Go Modules

安装

go get github.com/leonelquinteros/gotext

使用示例

单语言/域设置

import (
    "fmt"
    "github.com/leonelquinteros/gotext"
)

func main() {
    // 配置包
    gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name")

    // 从默认域翻译文本
    fmt.Println(gotext.Get("My text on 'domain-name' domain"))

    // 不重新配置从不同域翻译文本
    fmt.Println(gotext.GetD("domain2", "Another text on a different domain"))
}

使用动态变量

import (
    "fmt"
    "github.com/leonelquinteros/gotext"
)

func main() {
    // 配置包
    gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name")

    // 设置变量
    name := "John"

    // 翻译带变量的文本
    fmt.Println(gotext.Get("Hi, my name is %s", name))
}

使用Locale对象

import (
    "fmt"
    "github.com/leonelquinteros/gotext"
)

func main() {
    // 创建Locale对象
    l := gotext.NewLocale("/path/to/locales/root/dir", "es_UY")

    // 加载域 '/path/to/locales/root/dir/es_UY/default.po'
    l.AddDomain("default")

    // 从默认域翻译文本
    fmt.Println(l.Get("Translate this"))

    // 加载不同域
    l.AddDomain("translations")

    // 从域翻译文本
    fmt.Println(l.GetD("translations", "Translate this"))
}

在模板中使用

{{ .Loc.Get "Translate this" }}

使用Po对象处理.po文件

import (
    "fmt"
    "github.com/leonelquinteros/gotext"
)

func main() {
    // 设置PO内容
    str := `
msgid "Translate this"
msgstr "Translated text"

msgid "Another string"
msgstr ""

msgid "One with var: %s"
msgstr "This one sets the var: %s"
`

    // 创建Po对象
    po := gotext.NewPo()
    po.Parse(str)

    fmt.Println(po.Get("Translate this"))
}

使用复数形式

import (
    "fmt"
    "github.com/leonelquinteros/gotext"
)

func main() {
    // 设置PO内容
    str := `
msgid ""
msgstr ""

# Header below
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "Translate this"
msgstr "Translated text"

msgid "Another string"
msgstr ""

msgid "One with var: %s"
msgid_plural "Several with vars: %s"
msgstr[0] "This one is the singular: %s"
msgstr[1] "This one is the plural: %s"
`

    // 创建Po对象
    po := new(gotext.Po)
    po.Parse(str)

    fmt.Println(po.GetN("One with var: %s", "Several with vars: %s", 54, v))
    // 输出: "This one is the plural: Variable"
}

目录结构

Gotext假设的目录结构如下:

/path/to/locales
/path/to/locales/en_US
/path/to/locales/en_US/LC_MESSAGES
/path/to/locales/en_US/LC_MESSAGES/default.po
/path/to/locales/en_US/LC_MESSAGES/extras.po
/path/to/locales/en_UK
/path/to/locales/en_UK/LC_MESSAGES
/path/to/locales/en_UK/LC_MESSAGES/default.po
/path/to/locales/en_UK/LC_MESSAGES/extras.po
/path/to/locales/en_AU
/path/to/locales/en_AU/LC_MESSAGES
/path/to/locales/en_AU/LC_MESSAGES/default.po
/path/to/locales/en_AU/LC_MESSAGES/extras.po
/path/to/locales/es
/path/to/locales/es/default.po
/path/to/locales/es/extras.po
/path/to/locales/es_ES
/path/to/locales/es_ES/default.po
/path/to/locales/es_ES/extras.po
/path/to/locales/fr
/path/to/locales/fr/default.po
/path/to/locales/fr/extras.po

贡献

  • 欢迎贡献
  • 在你的项目中使用这个包
  • 在Github上报告问题
  • 提交bug修复和改进的pull请求
  • 在Github issues上提出建议

更多关于golang实现GNU gettext国际化文本处理的插件库gotext的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang实现GNU gettext国际化文本处理的插件库gotext的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


使用gotext实现Golang国际化(i18n)处理

gotext是一个Golang实现的GNU gettext国际化文本处理库,它兼容标准的gettext工具链,可以方便地为Go应用添加多语言支持。下面我将详细介绍gotext的使用方法。

安装gotext

首先安装gotext库:

go get github.com/leonelquinteros/gotext

基本使用

1. 准备翻译文件

GNU gettext使用.po.mo文件格式。.po是文本格式,.mo是二进制编译格式。

示例目录结构:

locales/
  en_US/
    LC_MESSAGES/
      default.po
      default.mo
  zh_CN/
    LC_MESSAGES/
      default.po
      default.mo

示例default.po文件内容:

msgid "Hello, world!"
msgstr "你好,世界!"

2. 基本代码示例

package main

import (
	"fmt"
	"github.com/leonelquinteros/gotext"
)

func main() {
	// 配置本地化目录
	gotext.Configure("locales", "zh_CN", "default")
	
	// 翻译文本
	fmt.Println(gotext.Get("Hello, world!"))
	
	// 带变量的翻译
	fmt.Println(gotext.Get("My name is %s", "John"))
}

3. 动态切换语言

package main

import (
	"fmt"
	"github.com/leonelquinteros/gotext"
	"os"
)

func main() {
	// 获取命令行参数
	lang := "en_US"
	if len(os.Args) > 1 {
		lang = os.Args[1]
	}

	// 配置本地化
	gotext.Configure("locales", lang, "default")

	// 使用翻译
	fmt.Println(gotext.Get("Hello, world!"))
	fmt.Println(gotext.Get("Welcome to our application"))
	
	// 复数形式处理
	fmt.Println(gotext.NGet("You have %d message", "You have %d messages", 3, 3))
}

高级功能

1. 使用域(Domains)

package main

import (
	"fmt"
	"github.com/leonelquinteros/gotext"
)

func main() {
	// 配置多个域
	gotext.Configure("locales", "zh_CN", "default,errors")
	
	// 使用默认域
	fmt.Println(gotext.Get("Hello"))
	
	// 使用特定域
	po := gotext.NewPo()
	po.ParseFile("locales/zh_CN/LC_MESSAGES/errors.po")
	fmt.Println(po.Get("File not found"))
}

2. 创建翻译文件

gotext提供了工具来从代码中提取需要翻译的字符串:

package main

import (
	"github.com/leonelquinteros/gotext"
)

func main() {
	// 这些字符串会被提取工具识别
	gotext.Get("Welcome message")
	gotext.Get("Goodbye message")
	
	// 复数形式
	gotext.NGet("%d file", "%d files", 1, 1)
}

然后使用xgettext工具提取:

xgettext --from-code=UTF-8 -o locales/default.pot -kGet -kNGet *.go

3. 热加载翻译

package main

import (
	"fmt"
	"github.com/leonelquinteros/gotext"
	"time"
)

func main() {
	// 配置自动重新加载
	gotext.Configure("locales", "zh_CN", "default")
	gotext.Get("Hello") // 第一次加载

	// 模拟文件更改
	time.Sleep(5 * time.Second)
	
	// 手动重新加载
	gotext.Get("Hello") // 自动重新加载
}

最佳实践

  1. 组织翻译文件:按功能模块划分不同的.po文件
  2. 使用上下文:对于相同原文但不同含义的字符串,使用上下文区分
  3. 处理变量:确保翻译字符串中的变量顺序可以调整
  4. 复数处理:不同语言复数规则不同,正确使用NGet方法
  5. 错误处理:检查.po文件是否成功加载

完整示例

package main

import (
	"fmt"
	"github.com/leonelquinteros/gotext"
	"os"
)

func main() {
	// 设置语言环境
	lang := "en_US"
	if len(os.Args) > 1 {
		lang = os.Args[1]
	}

	// 配置本地化
	gotext.Configure("locales", lang, "default")

	// 欢迎消息
	fmt.Println(gotext.Get("Welcome to our application"))
	
	// 带变量的消息
	user := "Alice"
	fmt.Println(gotext.Get("Hello, %s", user))
	
	// 复数处理
	count := 5
	fmt.Println(gotext.NGet("You have %d message", "You have %d messages", count, count))
	
	// 日期格式化
	fmt.Println(gotext.Get("Today is %s", "Monday"))
}

gotext为Golang应用提供了完整的国际化解决方案,兼容GNU gettext标准工具链,可以方便地与现有翻译工作流程集成。通过合理组织翻译文件和正确使用API,可以轻松实现多语言支持。

回到顶部