gettext

package module
v0.0.0-...-b6dae1d Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2017 License: BSD-3-Clause Imports: 9 Imported by: 0

README

gettext in golang

Build Status

TODO

  • parse mofiles
  • compile plural forms
  • non-utf8 mo files (possible wontfix)
  • gettext
  • ngettext
  • managing mo files / sane API

Example


import gettext

translations := gettext.NewTranslations("path/to/translations/", "messages", gettext.DefaultResolver)

locale := translations.Locale("en")

fmt.Println(locale.Gettext("hello from gettext"))

one := 1
two := 2

fmt.Println(fmt.Sprintf(locale.NGettext("%d thing", "%d things", uint32(one)), one))
fmt.Println(fmt.Sprintf(locale.NGettext("%d thing", "%d things", uint32(two)), two))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultResolver

func DefaultResolver(root string, locale string, domain string) string

DefaultResolver resolves paths in the standard format of: <root>/<locale>/LC_MESSAGES/<domain>.mo

Types

type Catalog

type Catalog interface {
	Gettext(msgid string) string
	NGettext(msgid string, msgid_plural string, n uint32) string
}

Catalog of translations for a given locale.

func ParseMO

func ParseMO(file *os.File) (Catalog, error)

ParseMO parses a mo file into a Catalog if possible.

type PathResolver

type PathResolver func(root string, locale string, domain string) string

PathResolver resolves a path to a mo file

type Translations

type Translations struct {
	// contains filtered or unexported fields
}

Translations holds the translations in the different locales your app supports. Use NewTranslations to create an instance.

func NewTranslations

func NewTranslations(root string, domain string, resolver PathResolver) Translations

NewTranslations is the main entry point for gogettext. Use this to set up the locales for your app. root is the root of your locale folder, domain the domain you want to load and resolver a function that resolves mo file paths. If your structure is <root>/<locale>/LC_MESSAGES/<domain>.mo, you can use DefaultResolver.

func (Translations) Locale

func (t Translations) Locale(locale string) Catalog

Locale returns the catalog translations for a given Locale. If the given locale is not available, a NullCatalog is returned.

func (Translations) Preload

func (t Translations) Preload(locales ...string)

Preload a list of locales (if they're available). This is useful if you want to limit IO to a specific time in your app, for example startup. Subsequent calls to Preload or Locale using a locale given here will not do any IO.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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