{"id":13413573,"url":"https://github.com/youthlin/t","last_synced_at":"2026-01-18T09:59:28.600Z","repository":{"id":47532871,"uuid":"373756374","full_name":"youthlin/t","owner":"youthlin","description":"t: translation util for go, using GNU gettext","archived":false,"fork":false,"pushed_at":"2024-01-05T12:00:11.000Z","size":167,"stargazers_count":19,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-07-31T20:52:35.407Z","etag":null,"topics":["gettext","gnu-gettext","go-gettext","go-i18n","go-l10n","i18n","l10n","tranlation","translate","translate-api"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/youthlin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-06-04T07:22:41.000Z","updated_at":"2024-06-25T08:48:10.000Z","dependencies_parsed_at":"2024-01-08T15:21:21.655Z","dependency_job_id":null,"html_url":"https://github.com/youthlin/t","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/youthlin/t","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/youthlin%2Ft","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/youthlin%2Ft/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/youthlin%2Ft/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/youthlin%2Ft/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/youthlin","download_url":"https://codeload.github.com/youthlin/t/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/youthlin%2Ft/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28534203,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-18T00:39:45.795Z","status":"online","status_checked_at":"2026-01-18T02:00:07.578Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gettext","gnu-gettext","go-gettext","go-i18n","go-l10n","i18n","l10n","tranlation","translate","translate-api"],"created_at":"2024-07-30T20:01:43.509Z","updated_at":"2026-01-18T09:59:28.585Z","avatar_url":"https://github.com/youthlin.png","language":"Go","funding_links":[],"categories":["Natural Language Processing","自然语言处理","Microsoft Office","Bot Building","Relational Databases"],"sub_categories":["Uncategorized","翻译","Translation"],"readme":"# t\nt: a translation util for go, inspired by GNU gettext  \nt: GNU gettext 的 Go 语言实现，Go 程序的国际化工具  \n[![sync-to-gitee](https://github.com/youthlin/t/actions/workflows/gitee.yaml/badge.svg)](https://github.com/youthlin/t/actions/workflows/gitee.yaml)\n[![test](https://github.com/youthlin/t/actions/workflows/test.yaml/badge.svg)](https://github.com/youthlin/t/actions/workflows/test.yaml)\n[![codecov](https://codecov.io/gh/youthlin/t/branch/main/graph/badge.svg?token=6RyU5nb3YT)](https://codecov.io/gh/youthlin/t)\n[![Go Report Card](https://goreportcard.com/badge/github.com/youthlin/t)](https://goreportcard.com/report/github.com/youthlin/t)\n[![Go Reference](https://pkg.go.dev/badge/github.com/youthlin/t.svg)](https://pkg.go.dev/github.com/youthlin/t)\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fyouthlin%2Ft.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fyouthlin%2Ft?ref=badge_shield)\n\n\n## Install 安装\n\n```bash\ngo get -u github.com/youthlin/t\n```\n\ngo.mod\n```go\nrequire (\n    github.com/youthlin/t latest\n)\n```\n\nGitee 镜像：[gitee.com/youthlin/gottext](gitee.com/youthlin/gottext) (gottext: go + gettext)\n\u003e 鸣谢仓库同步工具：https://github.com/Yikun/hub-mirror-action\n```\n// 使用 gitee 镜像\n// go.mod:\nreplace github.com/youthlin/t latest =\u003e gitee.com/youthlin/gottext latest\n```\n\n\n## Usage 使用\n```go\npath := \"path/to/filename.po\" // .po, .mo file\npath = \"path/to/po_mo/dir\"    // or dir.\n// (mo po 同名的话，po 后加载，会覆盖 mo 文件，因为 po 是文本文件，方便修改生效)\n// 1 bind domain 绑定翻译文件\nt.Load(path) // 默认绑定在 default 域 会自动搜索路径下的文件，读取 po/mo 里的语言标签进行注册\nt.Bind(\"my-domain\", path) // 或者指定Ø文本域\n// 2 set current domain 设置使用的文本域\nt.SetDomain(\"my-domain\")\n// 3 set user language 设置用户语言\n// t.SetLocale(\"zh_CN\")\nt.SetLocale(t.MostMatchLocale()) // empty to use system default\n// 4 use the gettext api 使用 gettext 翻译接口\nfmt.Println(t.T(\"Hello, world\"))\nfmt.Println(t.T(\"Hello, %v\", \"Tom\"))\nfmt.Println(t.N(\"One apple\", \"%d apples\", 1)) // One apple\nfmt.Println(t.N(\"One apple\", \"%d apples\", 2)) // %d apples\n// t.N(single, plural, n, args...)\n// n =\u003e used to choose single or plural\n// args =\u003e to format\n// args... supported, used to format string\n// 支持传入 args... 参数用于格式化输出\nfmt.Println(t.N(\"One apple\", \"%d apples\", 2, 2)) // 2 apples\nfmt.Println(t.N(\"%[2]s has one apple\", \"%[2]s has %[1]d apples\", 2, 200, \"Bob\"))\n// Bob has 200 apples\nt.X(\"msg_context_text\", \"msg_id\")\nt.X(\"msg_context_text\", \"msg_id\")\nt.XN(\"msg_context_text\", \"msg_id\", \"msg_plural\", n)\n```\n\n## API\n```go\nT(msgID, args...)\nN(msgID, msgIDPlural, n, args...) // and N64\nX(msgCTxt, msgID, args...)\nXN(msgCTxt, msgID, msgIDPlural, n, args...) // and XN64\nD(domain)\nL(locale)\n// T:  gettext\n// N:  ngettext\n// X:  pgettext\n// XN: npgettext\n// D:  with domain\n// L:  with locale(language)\n```\n\n## Domain 文本域\n```go\nt.Bind(domain1, path1)\nt.Bind(domain2, path2)\nt.SetLocale(\"zh_CN\")\n\nt.T(\"msg_id\")           // use default domain\n\nt.SetDomain(domain1)\nt.T(\"msg_id\")            // use domain1\nt.D(domain2).T(\"msg_id\") // use domain2\nt.D(\"unknown-domain\").T(\"msg_id\") // return \"msg_id\" directly\n\n```\n\n## Language 指定语言\nIf you are building a web application, you may want each request use diffenrent language, the code below may help you:  \n如果你写的是 web 应用而不是 cli 工具，你可能想让每个 request 使用不同的语言，请看：\n\n```go\nt.Load(path)\n\n// a) Specify a language 可以指定语言\nt.L(\"zh_CN\").T(\"msg_id\")\n\n// b) every one use his own language 每个用户使用他接受的语言\n// b.1) server supports 第一步，服务器支持的语言\nlangs := t.Locales()\n// golang.org/x/text/language\n// EN: https://blog.golang.org/matchlang\n// 中文: https://learnku.com/docs/go-blog/matchlang/6525\nvar supported []language.Tag\nfor _, lang =range langs{\n    supported = append(supported, language.Make(lang))\n}\nmatcher := language.NewMatcher(supported)\n// b.2) user accept 第二步，用户接受的语言\n// Judging by the browser header（Accept-Language）\n// 根据浏览器标头获取用户语言\n// or: userAccept := []language.Tag{ language.Make(\"lang-code-from-cookie\") }\n// 或从 cookie 中获取用户语言偏好\nuserAccept, q, err :=language.ParseAcceptLanguage(\"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6\")\n// b.3) match 第三步，匹配出最合适的\nmatchedTag, index, confidence := matcher.Match(userAccept...)\n// confidence may language.No, language.Low, language.High, language.Exact\n// 这里 confidence 是指匹配度，可以根据你的实际需求决定是否使用匹配的语言。\n// 如服务器支持 英文、简体中文，如果用户是繁体中文，那匹配度就不是 Exact，\n// 这时根据实际需求决定是使用英文，还是简体中文。\nuserLang := langs[index]\nt.L(userLang).T(\"msg_id\")\n\n// with domain, language 同时指定文本域、用户语言\nt.D(domain).L(userLang).T(\"msg_id\")\n```\n\n\u003e more examples can be find at: [example_test.go](example_test.go)\n\n## How to extract string 提取翻译文本\n```bash\n# if you use PoEdit, add a extractor\n# 如果你使用 PoEdit，在设置-提取器中新增一个提取器\n# Language: Go, *.go 语言填 Go 扩展名填 *.go 提取翻译的命令填写\n# xgettext -C --add-comments=TRANSLATORS: --force-po -o %o %C %K %F\n# 最后的三个输入框分别填写\n# -k%k\n# %f\n# --from-code=%c\n# keywords: 关键字这样设置：\n# T:1;N:1,2;N64:1,2;X:2,1c;XN:2,3,1c;XN64:2,3,1c\nxgettext -C --add-comments=TRANSLATORS: --force-po -kT -kN:1,2 -kX:2,1c -kXN:2,3,1c  *.go\n```\n\n## Done 已完成\n- ✅ mo file 支持 mo 二进制文件\n- ✅ extract from html templates 从模板文件中提取: [xtemplate](cmd/xtemplate/)\n```bash\ngo install github.com/youthlin/t/cmd/xtemplate@latest\n```\n\n## Links 链接\n- https://www.gnu.org/software/gettext/manual/html_node/index.html\n- https://github.com/search?l=Go\u0026q=gettext\u0026type=Repositories\n- https://github.com/antlr/antlr4/\n- https://blog.gopheracademy.com/advent-2017/parsing-with-antlr4-and-go/\n- https://xuanwo.io/2019/12/11/golang-i18n/ (中文)\n\n\n\n## License\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fyouthlin%2Ft.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fyouthlin%2Ft?ref=badge_large)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyouthlin%2Ft","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyouthlin%2Ft","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyouthlin%2Ft/lists"}