{"id":13413570,"url":"https://github.com/leonelquinteros/gotext","last_synced_at":"2026-03-03T01:05:41.456Z","repository":{"id":38310450,"uuid":"61498446","full_name":"leonelquinteros/gotext","owner":"leonelquinteros","description":"Go (Golang) GNU gettext utilities package ","archived":false,"fork":false,"pushed_at":"2024-09-27T18:06:58.000Z","size":725,"stargazers_count":434,"open_issues_count":4,"forks_count":58,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-10-25T05:25:11.086Z","etag":null,"topics":["gettext","go","golang","i18n","internationalization","l10n","language","plural-form","po-files","translated-strings","translation"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leonelquinteros.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-06-19T20:14:43.000Z","updated_at":"2024-10-03T05:44:54.000Z","dependencies_parsed_at":"2023-01-30T02:01:14.933Z","dependency_job_id":"35273dbe-4e2e-44c7-a88f-66afeb67d1c2","html_url":"https://github.com/leonelquinteros/gotext","commit_stats":{"total_commits":129,"total_committers":18,"mean_commits":7.166666666666667,"dds":"0.32558139534883723","last_synced_commit":"6f9bcaa9698f6926c1524d7428a1c6223c76c071"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonelquinteros%2Fgotext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonelquinteros%2Fgotext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonelquinteros%2Fgotext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonelquinteros%2Fgotext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leonelquinteros","download_url":"https://codeload.github.com/leonelquinteros/gotext/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243635541,"owners_count":20322957,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","go","golang","i18n","internationalization","l10n","language","plural-form","po-files","translated-strings","translation"],"created_at":"2024-07-30T20:01:43.447Z","updated_at":"2026-03-03T01:05:41.424Z","avatar_url":"https://github.com/leonelquinteros.png","language":"Go","funding_links":[],"categories":["文本处理","Text Processing","自然语言处理","Go","Natural Language Processing","Bot Building","Specific Formats","文本处理`解析和操作文本的代码库`","\u003cspan id=\"文字处理-text-processing\"\u003e文字处理 Text Processing\u003c/span\u003e","Microsoft Office"],"sub_categories":["查询语","Advanced Console UIs","翻译","HTTP Clients","Translation","交流","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"[![GitHub release](https://img.shields.io/github/release/leonelquinteros/gotext.svg)](https://github.com/leonelquinteros/gotext)\n[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)\n![Gotext build](https://github.com/leonelquinteros/gotext/workflows/Gotext%20build/badge.svg)\n[![Go Report Card](https://goreportcard.com/badge/github.com/leonelquinteros/gotext)](https://goreportcard.com/report/github.com/leonelquinteros/gotext)\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/leonelquinteros/gotext)](https://pkg.go.dev/github.com/leonelquinteros/gotext)\n\n\n# Gotext\n\n[GNU gettext utilities](https://www.gnu.org/software/gettext) for Go.\n\n\n# Features\n\n- Implements GNU gettext support in native Go.\n- Complete support for [PO files](https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html) including:\n  - Support for multiline strings and headers.\n  - Support for variables inside translation strings using Go's [fmt syntax](https://golang.org/pkg/fmt/).\n  - Support for [pluralization rules](https://www.gnu.org/software/gettext/manual/html_node/Translating-plural-forms.html).\n  - Support for [message contexts](https://www.gnu.org/software/gettext/manual/html_node/Contexts.html).\n- Support for MO files.\n- Thread-safe: This package is safe for concurrent use across multiple goroutines.\n- It works with UTF-8 encoding as it's the default for Go language.\n- Unit tests available.\n- Language codes are automatically simplified from the form `en_UK` to `en` if the first isn't available.\n- Ready to use inside Go templates.\n- Objects are serializable to []byte to store them in cache.\n- Support for Go Modules.\n\n\n# License\n\n[MIT license](LICENSE)\n\n\n# Documentation\n\nRefer to package documentation at (https://pkg.go.dev/github.com/leonelquinteros/gotext)\n\n\n# Installation\n\n```\ngo get github.com/leonelquinteros/gotext\n```\n\n- There are no requirements or dependencies to use this package.\n- No need to install GNU gettext utilities (unless specific needs of CLI tools).\n- No need for environment variables. Some naming conventions are applied but not needed.\n\n\n# Usage examples\n\n## Using package for single language/domain settings\n\nFor quick/simple translations you can use the package level functions directly.\n\n```go\nimport (\n    \"fmt\"\n    \"github.com/leonelquinteros/gotext\"\n)\n\nfunc main() {\n    // Configure package\n    gotext.Configure(\"/path/to/locales/root/dir\", \"en_UK\", \"domain-name\")\n\n    // Translate text from default domain\n    fmt.Println(gotext.Get(\"My text on 'domain-name' domain\"))\n\n    // Translate text from a different domain without reconfigure\n    fmt.Println(gotext.GetD(\"domain2\", \"Another text on a different domain\"))\n}\n\n```\n\n## Using dynamic variables on translations\n\nAll translation strings support dynamic variables to be inserted without translate.\nUse the fmt.Printf syntax (from Go's \"fmt\" package) to specify how to print the non-translated variable inside the translation string.\n\n```go\nimport (\n    \"fmt\"\n    \"github.com/leonelquinteros/gotext\"\n)\n\nfunc main() {\n    // Configure package\n    gotext.Configure(\"/path/to/locales/root/dir\", \"en_UK\", \"domain-name\")\n\n    // Set variables\n    name := \"John\"\n\n    // Translate text with variables\n    fmt.Println(gotext.Get(\"Hi, my name is %s\", name))\n}\n\n```\n\n\n## Using Locale object\n\nWhen having multiple languages/domains/libraries at the same time, you can create Locale objects for each variation\nso you can handle each settings on their own.\n\n```go\nimport (\n    \"fmt\"\n    \"github.com/leonelquinteros/gotext\"\n)\n\nfunc main() {\n    // Create Locale with library path and language code\n    l := gotext.NewLocale(\"/path/to/locales/root/dir\", \"es_UY\")\n\n    // Load domain '/path/to/locales/root/dir/es_UY/default.po'\n    l.AddDomain(\"default\")\n\n    // Translate text from default domain\n    fmt.Println(l.Get(\"Translate this\"))\n\n    // Load different domain\n    l.AddDomain(\"translations\")\n\n    // Translate text from domain\n    fmt.Println(l.GetD(\"translations\", \"Translate this\"))\n}\n```\n\nThis is also helpful for using inside templates (from the \"text/template\" package), where you can pass the Locale object to the template.\nIf you set the Locale object as \"Loc\" in the template, then the template code would look like:\n\n```\n{{ .Loc.Get \"Translate this\" }}\n```\n\n\n## Using the Po object to handle .po files and PO-formatted strings\n\nFor when you need to work with PO files and strings,\nyou can directly use the Po object to parse it and access the translations in there in the same way.\n\n```go\nimport (\n    \"fmt\"\n    \"github.com/leonelquinteros/gotext\"\n)\n\nfunc main() {\n    // Set PO content\n    str := `\nmsgid \"Translate this\"\nmsgstr \"Translated text\"\n\nmsgid \"Another string\"\nmsgstr \"\"\n\nmsgid \"One with var: %s\"\nmsgstr \"This one sets the var: %s\"\n`\n\n    // Create Po object\n    po := gotext.NewPo()\n    po.Parse(str)\n\n    fmt.Println(po.Get(\"Translate this\"))\n}\n```\n\n\n## Use plural forms of translations\n\nPO format supports defining one or more plural forms for the same translation.\nRelying on the PO file headers, a Plural-Forms formula can be set on the translation file\nas defined in (https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/Plural-forms.html)\n\n```go\nimport (\n    \"fmt\"\n    \"github.com/leonelquinteros/gotext\"\n)\n\nfunc main() {\n    // Set PO content\n    str := `\nmsgid \"\"\nmsgstr \"\"\n\n# Header below\n\"Plural-Forms: nplurals=2; plural=(n != 1);\\n\"\n\nmsgid \"Translate this\"\nmsgstr \"Translated text\"\n\nmsgid \"Another string\"\nmsgstr \"\"\n\nmsgid \"One with var: %s\"\nmsgid_plural \"Several with vars: %s\"\nmsgstr[0] \"This one is the singular: %s\"\nmsgstr[1] \"This one is the plural: %s\"\n`\n\n    // Create Po object\n    po := new(gotext.Po)\n    po.Parse(str)\n\n    fmt.Println(po.GetN(\"One with var: %s\", \"Several with vars: %s\", 54, v))\n    // \"This one is the plural: Variable\"\n}\n```\n\n\n# Locales directories structure\n\nThe package will assume a directories structure starting with a base path that will be provided to the package configuration\nor to object constructors depending on the use, but either will use the same convention to lookup inside the base path.\n\nInside the base directory where will be the language directories named using the language and country 2-letter codes (en_US, es_AR, ...).\nAll package functions can lookup after the simplified version for each language in case the full code isn't present but the more general language code exists.\nSo if the language set is `en_UK`, but there is no directory named after that code and there is a directory named `en`,\nall package functions will be able to resolve this generalization and provide translations for the more general library.\n\nThe language codes are assumed to be [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) codes (2-letter codes).\nThat said, most functions will work with any coding standard as long the directory name matches the language code set on the configuration.\n\nThen, there can be a `LC_MESSAGES` containing all PO files or the PO files themselves.\nA library directory structure can look like:\n\n```\n/path/to/locales\n/path/to/locales/en_US\n/path/to/locales/en_US/LC_MESSAGES\n/path/to/locales/en_US/LC_MESSAGES/default.po\n/path/to/locales/en_US/LC_MESSAGES/extras.po\n/path/to/locales/en_UK\n/path/to/locales/en_UK/LC_MESSAGES\n/path/to/locales/en_UK/LC_MESSAGES/default.po\n/path/to/locales/en_UK/LC_MESSAGES/extras.po\n/path/to/locales/en_AU\n/path/to/locales/en_AU/LC_MESSAGES\n/path/to/locales/en_AU/LC_MESSAGES/default.po\n/path/to/locales/en_AU/LC_MESSAGES/extras.po\n/path/to/locales/es\n/path/to/locales/es/default.po\n/path/to/locales/es/extras.po\n/path/to/locales/es_ES\n/path/to/locales/es_ES/default.po\n/path/to/locales/es_ES/extras.po\n/path/to/locales/fr\n/path/to/locales/fr/default.po\n/path/to/locales/fr/extras.po\n```\n\nAnd so on...\n\n\n# Contribute\n\n- Please, contribute.\n- Use the package on your projects.\n- Report issues on Github.\n- Send pull requests for bugfixes and improvements.\n- Send proposals on Github issues.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonelquinteros%2Fgotext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleonelquinteros%2Fgotext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonelquinteros%2Fgotext/lists"}