• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..24-Mar-2022-

.gitignoreH A D24-Mar-2022252 2317

LICENSEH A D24-Mar-202210 KiB191155

MakefileH A D24-Mar-2022142 128

README.mdH A D24-Mar-20224 KiB14189

codecov.ymlH A D24-Mar-2022123 108

i18n.goH A D24-Mar-20225.3 KiB237169

README.md

1# i18n
2
3[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/unknwon/i18n/Go?logo=github&style=for-the-badge)](https://github.com/unknwon/i18n/actions?query=workflow%3AGo)
4[![codecov](https://img.shields.io/codecov/c/github/unknwon/i18n/master?logo=codecov&style=for-the-badge)](https://codecov.io/gh/unknwon/i18n)
5[![GoDoc](https://img.shields.io/badge/GoDoc-Reference-blue?style=for-the-badge&logo=go)](https://pkg.go.dev/github.com/unknwon/i18n?tab=doc)
6[![Sourcegraph](https://img.shields.io/badge/view%20on-Sourcegraph-brightgreen.svg?style=for-the-badge&logo=sourcegraph)](https://sourcegraph.com/github.com/unknwon/i18n)
7
8Package i18n is for app Internationalization and Localization.
9
10## Introduction
11
12This 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.
13
14You can use following command to install this module:
15
16    go get github.com/Unknwon/i18n
17
18## Usage
19
20First of all, you have to import this package:
21
22```go
23import "github.com/unknwon/i18n"
24```
25
26The 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`.
27
28The name and extensions of locale files can be anything, but we strongly recommend you to follow the style of gogsweb.
29
30## Minimal example
31
32Here are two simplest locale file examples:
33
34File `locale_en-US.ini`:
35
36```ini
37hi = hello, %s
38bye = goodbye
39```
40
41File `locale_zh-CN.ini`:
42
43```ini
44hi = 您好,%s
45bye = 再见
46```
47
48### Do Translation
49
50There are two ways to do translation depends on which way is the best fit for your application or framework.
51
52Directly use package function to translate:
53
54```go
55i18n.Tr("en-US", "hi", "Unknwon")
56i18n.Tr("en-US", "bye")
57```
58
59Or create a struct and embed it:
60
61```go
62type MyController struct{
63    // ...other fields
64    i18n.Locale
65}
66
67//...
68
69func ... {
70    c := &MyController{
71        Locale: i18n.Locale{"en-US"},
72    }
73    _ = c.Tr("hi", "Unknwon")
74    _ = c.Tr("bye")
75}
76```
77
78Code above will produce correspondingly:
79
80- English `en-US`:`hello, Unknwon`, `goodbye`
81- Chinese `zh-CN`:`您好,Unknwon`, `再见`
82
83## Section
84
85For different pages, one key may map to different values. Therefore, i18n module also uses the section feature of INI format configuration to achieve section.
86
87For 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:
88
89Content in locale file:
90
91```ini
92about = About
93
94[about]
95about = About Us
96```
97
98Get `about` in home page:
99
100```go
101i18n.Tr("en-US", "about")
102```
103
104Get `about` in about page:
105
106```go
107i18n.Tr("en-US", "about.about")
108```
109
110### Ambiguity
111
112Because 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.
113
114For example, the key name is `about.`, then we can use:
115
116```go
117i18n.Tr("en-US", ".about.")
118```
119
120to get expect result.
121
122## Helper tool
123
124Module i18n provides a command line helper tool beei18n for simplify steps of your development. You can install it as follows:
125
126	go get github.com/unknwon/i18n/ui18n
127
128### Sync locale files
129
130Command `sync` allows you use a exist local file as the template to create or sync other locale files:
131
132	ui18n sync source_file.ini other1.ini other2.ini
133
134This command can operate 1 or more files in one command.
135
136## More information
137
138- The first locale you load to the module is considered as **default locale**.
139- When matching non-default locale and didn't find the string, i18n will have a second try on default locale.
140- 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.
141