• This repository has been archived on 27/Nov/2021
  • Stars
    star
    84
  • Rank 389,133 (Top 8 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Package i18n is for app Internationalization and Localization.

i18n

GitHub Workflow Status codecov GoDoc Sourcegraph

Package i18n is for app Internationalization and Localization.

Introduction

This package provides multiple-language options to improve user experience. Sites like Go Walker and gogs.io are using this module to implement Chinese and English user interfaces.

You can use following command to install this module:

go get github.com/unknwon/i18n

Usage

First of all, you have to import this package:

import "github.com/unknwon/i18n"

The 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.

The name and extensions of locale files can be anything, but we strongly recommend you to follow the style of gogsweb.

Minimal example

Here are two simplest locale file examples:

File locale_en-US.ini:

hi = hello, %s
bye = goodbye

File locale_zh-CN.ini:

hi = 您好,%s
bye = 再见

Do Translation

There are two ways to do translation depends on which way is the best fit for your application or framework.

Directly use package function to translate:

i18n.Tr("en-US", "hi", "Unknwon")
i18n.Tr("en-US", "bye")

Or create a struct and embed it:

type MyController struct{
    // ...other fields
    i18n.Locale
}

//...

func ... {
    c := &MyController{
        Locale: i18n.Locale{"en-US"},
    }
    _ = c.Tr("hi", "Unknwon")
    _ = c.Tr("bye")
}

Code above will produce correspondingly:

  • English en-UShello, Unknwon, goodbye
  • Chinese zh-CN您好,Unknwon, 再见

Section

For different pages, one key may map to different values. Therefore, i18n module also uses the section feature of INI format configuration to achieve section.

For 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:

Content in locale file:

about = About

[about]
about = About Us

Get about in home page:

i18n.Tr("en-US", "about")

Get about in about page:

i18n.Tr("en-US", "about.about")

Ambiguity

Because dot . is sign of section in both INI parser 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.

For example, the key name is about., then we can use:

i18n.Tr("en-US", ".about.")

to get expect result.

Helper tool

Module i18n provides a command line helper tool beei18n for simplify steps of your development. You can install it as follows:

go get github.com/unknwon/i18n/ui18n

Sync locale files

Command sync allows you use a exist local file as the template to create or sync other locale files:

ui18n sync source_file.ini other1.ini other2.ini

This command can operate 1 or more files in one command.

More information

  • The first locale you load to the module is considered as default locale.
  • When matching non-default locale and didn't find the string, i18n will have a second try on default locale.
  • 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.

More Repositories

1

the-way-to-go_ZH_CN

《The Way to Go》中文译本,中文正式名《Go 入门指南》
Go
34,552
star
2

go-fundamental-programming

《Go 编程基础》是一套针对 Google 出品的 Go 语言的视频语音教程,主要面向新手级别的学习者。
Go
9,024
star
3

go-study-index

Go 语言学习资料索引
6,447
star
4

go-web-foundation

《Go Web 基础》是一套针对 Google 出品的 Go 语言的视频语音教程,主要面向完成《Go 编程基础》教程后希望进一步了解有关 Go Web 开发的学习者。
Go
2,670
star
5

go-rock-libraries-showcases

《Go名库讲解》是一套针对 Google 出品的 Go 语言的第三方库进行评测讲解的集博客、示例与语音视频为一体的综合教程,适合完成学习完成《Go编程基础》教程的学习者。
Go
1,604
star
6

com

This is an open source project for commonly used functions for the Go programming language.
Go
757
star
7

gowalker

Go Walker is a server that generates Go projects API documentation on the fly.
Go
660
star
8

goconfig

Package goconfig is a fully functional and comments-support configuration file (.ini) parser.
Go
605
star
9

building-web-applications-in-go

Go 语言 Web 应用开发系列教程,从新手到双手残废
Go
577
star
10

bra

Bra (Brilliant Ridiculous Assistant) is a command line utility tool.
Go
399
star
11

go-code-convention

@unknwon's opinionated and paranoid Go code convention.
348
star
12

gcblog

An open source project for Golang China blog.
Go
103
star
13

qiniudrive

七牛云盘是基于七牛开放 API 构建的第三方同步程序
C#
70
star
14

paginater

Package paginater is a helper module for custom pagination calculation.
Go
48
star
15

flamego-basics

《Flamego 框架基础》是一套针对 Go 语言 Web 框架 Flamego 出品的视频语音教程,主要面向 Web 应用开发者
Go
47
star
16

cae

Package cae implements PHP-like Compression and Archive Extensions.
Go
36
star
17

go-import-server

HTTP server for canonical "go get" import path
Go
33
star
18

wam

wam(Weibo Automated Management) - Golang中国微博自动化管理程序
Go
23
star
19

orbiter

Orbiter is a tool for collecting and redistributing webhooks over the network.
Go
20
star
20

leetcode.go

My Golang solutions to https://leetcode.com/
Go
19
star
21

CharmControlLibrary

C# GDI+ 实现的伪子类化控件类库
C#
17
star
22

labeler

Labeler is a CLI tool to sync labels for a GitHub repository with templates.
Go
13
star
23

figo

Fig(http://fig.sh) in Go.
Go
11
star
24

log

Package log is a dead simple, levelable, colorful logging library.
Go
10
star
25

docker-sync-to-aliyun

Sync Docker images to Aliyun's registry
8
star
26

gopha

gopha is a package for implementation of Perceptual Hash Algorithm in Go programming language.
Go
8
star
27

sublite

SubLite is a theme converter from Sublime Text to LiteIDE.
Go
7
star
28

unknwon

Go
5
star
29

rods

The collection of RODs written by @unknwon for researching and describing engineering efforts.
5
star
30

Project-Spartan

The Project Spartan is a hybrid-infrastructure and research-oriented HA (High Availability) prototype for IT 485.
JavaScript
4
star
31

fws

Fibonacci Web Server
Go
2
star
32

unknwon.cn

SCSS
1
star
33

sublime-sourcegraph-theme

Sourcegraph Color Scheme for SublimeText 3.
1
star
34

buildkite-flaky-reporter

HTTP server for reporting flaky tests and cancellation on Buildkite
Go
1
star