i18n

package
v0.0.0-...-10dc113 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2017 License: MIT, Apache-2.0 Imports: 5 Imported by: 0

README

i18n

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, under conf/locale folder of gogsweb, 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 srouce_file.ini other1.ini other2.ini

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

More information

If the key does not exist, then i18n will return the key string to caller. For instance, when key name is hi and it does not exist in locale file, simply return hi as output.

Documentation

Overview

Package i18n is for app Internationalization and Localization.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLangAlreadyExist = errors.New("Lang already exists")
)

Functions

func Count

func Count() int

Count returns number of languages that are registered.

func GetDescriptionByIndex

func GetDescriptionByIndex(index int) string

func GetDescriptionByLang

func GetDescriptionByLang(lang string) string

func GetLangByIndex

func GetLangByIndex(index int) string

GetLangByIndex return language by given index.

func IndexLang

func IndexLang(lang string) int

IndexLang returns index of language locale, it returns -1 if locale not exists.

func IsExist

func IsExist(lang string) bool

IsExist returns true if given language locale exists.

func ListLangDescs

func ListLangDescs() []string

func ListLangs

func ListLangs() []string

ListLangs returns list of all locale languages.

func ReloadLangs

func ReloadLangs(langs ...string) error

ReloadLangs reloads locale files.

func SetDefaultLang

func SetDefaultLang(lang string)

SetDefaultLang sets default language which is a indicator that when target language is not found, try find in default language again.

func SetMessage

func SetMessage(lang string, localeFile interface{}, otherLocaleFiles ...interface{}) error

SetMessage sets the message file for localization.

func SetMessageWithDesc

func SetMessageWithDesc(lang, langDesc string, localeFile interface{}, otherLocaleFiles ...interface{}) error

func Tr

func Tr(lang, format string, args ...interface{}) string

Tr translates content to target language.

Types

type Locale

type Locale struct {
	Lang string
}

Locale represents the information of localization.

func (Locale) Index

func (l Locale) Index() int

Index returns lang index of LangStore.

func (Locale) Tr

func (l Locale) Tr(format string, args ...interface{}) string

Tr translates content to target language.

Directories

Path Synopsis
ui18n is a helper tool for Unknwon/i18n package.
ui18n is a helper tool for Unknwon/i18n package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL