go-i18n

module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2015 License: MIT

README

go-i18n Build Status

go-i18n is a Go package and a command that can be used to translate Go programs into multiple languages.

Package i18n GoDoc

The i18n package provides runtime APIs for fetching translated strings.

Command goi18n GoDoc

The goi18n command provides functionality for managing the translation process.

Installation

Make sure you have setup GOPATH.

go get -u github.com/nicksnyder/go-i18n/goi18n
goi18n -help
Workflow

A typical workflow looks like this:

  1. Add a new string to your source code.

    T("settings_title")
    
  2. Add the string to en-US.all.json

    [
      {
        "id": "settings_title",
        "translation": "Settings"
      }
    ]
    
  3. Run goi18n

    goi18n path/to/*.all.json
    
  4. Send path/to/*.untranslated.json to get translated.

  5. Run goi18n again to merge the translations

    goi18n path/to/*.all.json path/to/*.untranslated.json
    

Translation files

A translation file stores translated and untranslated strings.

Example:

[
  {
    "id": "d_days",
    "translation": {
      "one": "{{.Count}} day",
      "other": "{{.Count}} days"
    }
  },
  {
    "id": "my_height_in_meters",
    "translation": {
      "one": "I am {{.Count}} meter tall.",
      "other": "I am {{.Count}} meters tall."
    }
  },
  {
    "id": "person_greeting",
    "translation": "Hello {{.Person}}"
  },
  {
    "id": "person_unread_email_count",
    "translation": {
      "one": "{{.Person}} has {{.Count}} unread email.",
      "other": "{{.Person}} has {{.Count}} unread emails."
    }
  },
  {
    "id": "person_unread_email_count_timeframe",
    "translation": {
      "one": "{{.Person}} has {{.Count}} unread email in the past {{.Timeframe}}.",
      "other": "{{.Person}} has {{.Count}} unread emails in the past {{.Timeframe}}."
    }
  },
  {
    "id": "program_greeting",
    "translation": "Hello world"
  },
  {
    "id": "your_unread_email_count",
    "translation": {
      "one": "You have {{.Count}} unread email.",
      "other": "You have {{.Count}} unread emails."
    }
  }
]

Supported languages

  • Arabic (ar)
  • Belarusian (be)
  • Bulgarian (bg)
  • Burmese (my)
  • Catalan (ca)
  • Chinese (simplified and traditional) (zh)
  • Czech (cs)
  • Danish (da)
  • Dutch (nl)
  • English (en)
  • French (fr)
  • German (de)
  • Icelandic (is)
  • Indonesian (id)
  • Italian (it)
  • Korean (ko)
  • Japanese (ja)
  • Lithuanian (lt)
  • Malay (ms)
  • Polish (pl)
  • Portuguese (pt)
  • Portuguese (Brazilian) (pt-BR)
  • Russian (ru)
  • Spanish (es)
  • Swedish (sv)
  • Turkish (tr)
  • Ukrainian (uk)

Adding new languages

It is easy to add support for additional languages:

  1. Lookup the language's CLDR plural rules.

  2. Add the language to pluralspec.go:

    var pluralSpecs = map[string]*PluralSpec{
        // ...
    			// English
    			"en": &PluralSpec{
    				Plurals: newPluralSet(One, Other),
    				PluralFunc: func(ops *operands) Plural {
    					if ops.I == 1 && ops.V == 0 {
    						return One
    					}
    					return Other
    				},
    			},
        // ...
    }
    
  3. Add a test to pluralspec_test.go

  4. Update this README with the new language.

  5. Submit a pull request!

License

go-i18n is available under the MIT license. See the LICENSE file for more info.

Directories

Path Synopsis
The goi18n command formats and merges translation files.
The goi18n command formats and merges translation files.
Package i18n supports string translations with variable substitution and CLDR pluralization.
Package i18n supports string translations with variable substitution and CLDR pluralization.
bundle
Package bundle manages translations for multiple languages.
Package bundle manages translations for multiple languages.
language
Package language defines languages that implement CLDR pluralization.
Package language defines languages that implement CLDR pluralization.
translation
Package translation defines the interface for a translation.
Package translation defines the interface for a translation.

Jump to

Keyboard shortcuts

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