Giter Club home page Giter Club logo

go-i18n's Introduction

go-i18n Build Status Sourcegraph

go-i18n is a Go package and a command that helps you 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.

Here is an example of the default file format that go-i18n supports:

[
  {
    "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."
    }
  }
]

To use a different file format, write a parser for the format and add the parsed translations using AddTranslation.

Note that TOML only supports the flat format, which is described below.

More examples of translation files: JSON, TOML, YAML.

Flat Format

You can also write shorter translation files with flat format. E.g the example above can be written in this way:

{
  "d_days": {
    "one":  "{{.Count}} day.",
    "other":  "{{.Count}} days."
  },

  "my_height_in_meters": {
    "one":  "I am {{.Count}} meter tall.",
    "other":  "I am {{.Count}} meters tall."
  },

  "person_greeting": {
    "other": "Hello {{.Person}}"
  },

  "person_unread_email_count": {
    "one":  "{{.Person}} has {{.Count}} unread email.",
    "other":  "{{.Person}} has {{.Count}} unread emails."
  },

  "person_unread_email_count_timeframe": {
    "one":  "{{.Person}} has {{.Count}} unread email in the past {{.Timeframe}}.",
    "other":  "{{.Person}} has {{.Count}} unread emails in the past {{.Timeframe}}."
  },

  "program_greeting": {
    "other": "Hello world"
  },

  "your_unread_email_count": {
    "one":  "You have {{.Count}} unread email.",
    "other":  "You have {{.Count}} unread emails."
  }
}

The logic of flat format is, what it is structure of structures and name of substructures (ids) should be always a string. If there is only one key in substructure and it is "other", then it's non-plural translation, else plural.

More examples of flat format translation files can be found in goi18n/testdata/input/flat.

Contributions

If you would like to submit a pull request, please

  1. Write tests
  2. Format code with goimports.
  3. Read the common code review comments.

License

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

go-i18n's People

Contributors

nicksnyder avatar parkr avatar n10v avatar mash avatar dorajistyle avatar udokmeci avatar rtfb avatar mh-cbon avatar soktherat avatar wichert avatar rodcorsi avatar ultimaweapon avatar ottob avatar zippoxer avatar jondal avatar sogko avatar emosbaugh avatar dennisfaust avatar

Stargazers

Kornel Maczyński avatar

Watchers

Bjørn Erik Pedersen avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.