i18n

package
v0.0.0-...-fd97e0e Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2016 License: Apache-2.0, Apache-2.0 Imports: 10 Imported by: 0

README

i18n

A simple i18n library in Go, only supports text translation currently.

Usage

Setup

Place all you translation json files under 'locale' directory.

  • Use current user's locale UseOSLocale()

  • Specifies locale to use manually SetLocale("en_US")

If your translations is under another place, SetMessagesDir("mydir")

Or feed from in memory data structure. SetMessagesFunc(func)

Use
t := i18n.T("KEY_OF_STRING")
t := i18n.T("KEY_OF_FORMAT_STRING", var1, var1, ...)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLocale

func SetLocale(locale string) error

SetLocale sets the current locale to the given value. If the locale is not in a valid format, this function will return an error and leave the current locale as is.

func SetMessagesDir

func SetMessagesDir(d string)

SetMessagesDir sets the directory from which to load translations if they are not under the default directory 'locale'

func SetMessagesFunc

func SetMessagesFunc(f ReadFunc)

SetMessagesFunc tells i18n to read translations through ReadFunc

func T

func T(key string, args ...interface{}) string

T translates the given key into a message based on the current locale, formatting the string using the supplied (optional) args. This method will fall back to other locales if the key isn't defined for the current locale. The search order (with examples) is as follows:

  1. current locale (zh_CN)
  2. lang only (zh)
  3. default locale (en_US)
  4. lang only of default (en)

func UseOSLocale

func UseOSLocale() error

UseOSLocale detect OS locale for current user and let i18n to use it

Types

type ReadFunc

type ReadFunc func(fileName string) ([]byte, error)

ReadFunc is the func provided to SetMessagesFunc which returns the byte sequence given a file name

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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