Categorygithub.com/go-playground/universal-translator
modulepackage
0.18.1
Repository: https://github.com/go-playground/universal-translator.git
Documentation: pkg.go.dev

# README

universal-translator

Project status Coverage Status Go Report Card GoDoc License

Universal Translator is an i18n Translator for Go/Golang using CLDR data + pluralization rules

Why another i18n library?

Because none of the plural rules seem to be correct out there, including the previous implementation of this package, so I took it upon myself to create locales for everyone to use; this package is a thin wrapper around locales in order to store and translate text for use in your applications.

Features

  • Rules generated from the CLDR data, v36.0.1
  • Contains Cardinal, Ordinal and Range Plural Rules
  • Contains Month, Weekday and Timezone translations built in
  • Contains Date & Time formatting functions
  • Contains Number, Currency, Accounting and Percent formatting functions
  • Supports the "Gregorian" calendar only ( my time isn't unlimited, had to draw the line somewhere )
  • Support loading translations from files
  • Exporting translations to file(s), mainly for getting them professionally translated
  • Code Generation for translation files -> Go code.. i.e. after it has been professionally translated
  • Tests for all languages, I need help with this, please see here

Installation

Use go get

go get github.com/go-playground/universal-translator

Usage & Documentation

Please see https://godoc.org/github.com/go-playground/universal-translator for usage docs

Examples:

File formatting

All types, Plain substitution, Cardinal, Ordinal and Range translations can all be contained within the same file(s); they are only separated for easy viewing.

Examples:
Basic Makeup

NOTE: not all fields are needed for all translation types, see examples

{
    "locale": "en",
    "key": "days-left",
    "trans": "You have {0} day left.",
    "type": "Cardinal",
    "rule": "One",
    "override": false
}
FieldDescription
localeThe locale for which the translation is for.
keyThe translation key that will be used to store and lookup each translation; normally it is a string or integer.
transThe actual translation text.
typeThe type of translation Cardinal, Ordinal, Range or "" for a plain substitution(not required to be defined if plain used)
ruleThe plural rule for which the translation is for eg. One, Two, Few, Many or Other.(not required to be defined if plain used)
overrideIf you wish to override an existing translation that has already been registered, set this to 'true'. 99% of the time there is no need to define it.

Help With Tests

To anyone interesting in helping or contributing, I sure could use some help creating tests for each language. Please see issue here for details.

License

Distributed under MIT License, please see license file in code for more details.

# Functions

New returns a new UniversalTranslator instance set with the fallback locale and locales it should support.

# Constants

supported Export Formats.

# Variables

ErrUnknowTranslation indicates the translation could not be found.

# Structs

ErrBadParamSyntax is the error representing a bad parameter definition in a translation eg.
ErrBadPluralDefinition is the error representing an incorrect plural definition usually found within translations defined within files during the import process.
ErrCardinalTranslation is the error representing a cardinal translation error.
ErrConflictingTranslation is the error representing a conflicting translation.
ErrExistingTranslator is the error representing a conflicting translator.
ErrMissingBracket is the error representing a missing bracket in a translation eg.
ErrMissingLocale is the error representing an expected locale that could not be found aka locale not registered with the UniversalTranslator Instance.
ErrMissingPluralTranslation is the error signifying a missing translation given the locales plural rules.
ErrOrdinalTranslation is the error representing an ordinal translation error.
ErrRangeTranslation is the error representing a range translation error.
UniversalTranslator holds all locale & translation data.

# Interfaces

Translator is universal translators translator instance which is a thin wrapper around locales.Translator instance providing some extra functionality.

# Type aliases

ImportExportFormat is the format of the file import or export.