{"id":13694561,"url":"https://github.com/Unknwon/i18n","last_synced_at":"2025-05-03T04:30:31.346Z","repository":{"id":18193872,"uuid":"21315901","full_name":"unknwon/i18n","owner":"unknwon","description":"Package i18n is for app Internationalization and Localization.","archived":true,"fork":false,"pushed_at":"2021-09-04T04:57:58.000Z","size":41,"stargazers_count":84,"open_issues_count":0,"forks_count":11,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-11-12T21:38:51.195Z","etag":null,"topics":["go","i18n","internationalization","l10n","localization","lsif-enabled"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/unknwon.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}},"created_at":"2014-06-29T03:38:01.000Z","updated_at":"2024-02-08T16:18:42.000Z","dependencies_parsed_at":"2022-07-30T21:07:55.726Z","dependency_job_id":null,"html_url":"https://github.com/unknwon/i18n","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unknwon%2Fi18n","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unknwon%2Fi18n/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unknwon%2Fi18n/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unknwon%2Fi18n/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/unknwon","download_url":"https://codeload.github.com/unknwon/i18n/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252144275,"owners_count":21701379,"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":["go","i18n","internationalization","l10n","localization","lsif-enabled"],"created_at":"2024-08-02T17:01:34.789Z","updated_at":"2025-05-03T04:30:31.077Z","avatar_url":"https://github.com/unknwon.png","language":"Go","funding_links":[],"categories":["开源类库","Open source library"],"sub_categories":["未归类","Not Categorized"],"readme":"# i18n\n\n[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/unknwon/i18n/Go?logo=github\u0026style=for-the-badge)](https://github.com/unknwon/i18n/actions?query=workflow%3AGo)\n[![codecov](https://img.shields.io/codecov/c/github/unknwon/i18n/master?logo=codecov\u0026style=for-the-badge)](https://codecov.io/gh/unknwon/i18n)\n[![GoDoc](https://img.shields.io/badge/GoDoc-Reference-blue?style=for-the-badge\u0026logo=go)](https://pkg.go.dev/github.com/unknwon/i18n?tab=doc)\n[![Sourcegraph](https://img.shields.io/badge/view%20on-Sourcegraph-brightgreen.svg?style=for-the-badge\u0026logo=sourcegraph)](https://sourcegraph.com/github.com/unknwon/i18n)\n\nPackage i18n is for app Internationalization and Localization.\n\n## Introduction\n\nThis package provides multiple-language options to improve user experience. Sites like [Go Walker](http://gowalker.org) and [gogs.io](http://gogs.io) are using this module to implement Chinese and English user interfaces.\n\nYou can use following command to install this module:\n\n    go get github.com/unknwon/i18n\n\n## Usage\n\nFirst of all, you have to import this package:\n\n```go\nimport \"github.com/unknwon/i18n\"\n```\n\nThe format of locale files is very like INI format configuration file, which is basically key-value pairs. But this module has some improvements. Every language corresponding to a locale file, for example, suppose there are two files called `locale_en-US.ini` and `locale_zh-CN.ini`.\n\nThe name and extensions of locale files can be anything, but we strongly recommend you to follow the style of gogsweb.\n\n## Minimal example\n\nHere are two simplest locale file examples:\n\nFile `locale_en-US.ini`:\n\n```ini\nhi = hello, %s\nbye = goodbye\n```\n\nFile `locale_zh-CN.ini`:\n\n```ini\nhi = 您好，%s\nbye = 再见\n```\n\n### Do Translation\n\nThere are two ways to do translation depends on which way is the best fit for your application or framework.\n\nDirectly use package function to translate:\n\n```go\ni18n.Tr(\"en-US\", \"hi\", \"Unknwon\")\ni18n.Tr(\"en-US\", \"bye\")\n```\n\nOr create a struct and embed it:\n\n```go\ntype MyController struct{\n    // ...other fields\n    i18n.Locale\n}\n\n//...\n\nfunc ... {\n    c := \u0026MyController{\n        Locale: i18n.Locale{\"en-US\"},\n    }\n    _ = c.Tr(\"hi\", \"Unknwon\")\n    _ = c.Tr(\"bye\")\n}\n```\n\nCode above will produce correspondingly:\n\n- English `en-US`：`hello, Unknwon`, `goodbye`\n- Chinese `zh-CN`：`您好，Unknwon`, `再见`\n\n## Section\n\nFor different pages, one key may map to different values. Therefore, i18n module also uses the section feature of INI format configuration to achieve section.\n\nFor example, the key name is `about`, and we want to show `About` in the home page and `About Us` in about page. Then you can do following:\n\nContent in locale file:\n\n```ini\nabout = About\n\n[about]\nabout = About Us\n```\n\nGet `about` in home page:\n\n```go\ni18n.Tr(\"en-US\", \"about\")\n```\n\nGet `about` in about page:\n\n```go\ni18n.Tr(\"en-US\", \"about.about\")\n```\n\n### Ambiguity\n\nBecause dot `.` is sign of section in both [INI parser](https://github.com/go-ini/ini) and locale files, so when your key name contains `.` will cause ambiguity. At this point, you just need to add one more `.` in front of the key.\n\nFor example, the key name is `about.`, then we can use:\n\n```go\ni18n.Tr(\"en-US\", \".about.\")\n```\n\nto get expect result.\n\n## Helper tool\n\nModule i18n provides a command line helper tool beei18n for simplify steps of your development. You can install it as follows:\n\n\tgo get github.com/unknwon/i18n/ui18n\n\n### Sync locale files\n\nCommand `sync` allows you use a exist local file as the template to create or sync other locale files:\n\n\tui18n sync source_file.ini other1.ini other2.ini\n\nThis command can operate 1 or more files in one command.\n\n## More information\n\n- The first locale you load to the module is considered as **default locale**.\n- When matching non-default locale and didn't find the string, i18n will have a second try on default locale.\n- If i18n still cannot find string in the default locale, raw string will be returned. For instance, when the string is `hi` and it does not exist in locale file, simply return `hi` as output.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FUnknwon%2Fi18n","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FUnknwon%2Fi18n","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FUnknwon%2Fi18n/lists"}