Skip to content

snapcore/go-gettext

 
 

Repository files navigation

gettext in golang

Build Status

go-gettext is a pure Go implementation of the GNU Gettext internationalisation API. It loads the binary catalogs generated by msgfmt by memory mapping them, so there is very little overhead at startup. Translations are looked up using the data structures in the catalog directly (either a binary search or hash table lookup).

In addition to the basic Gettext API it supports the NGettext and PGettext variants, supporting plural translations and translations requiring a context string respectively.

Example

import "github.com/snapcore/go-gettext"

//go:embed translations
var assets embed.FS

domain := &gettext.TextDomain{
	Name:      "messages",
	LocaleDir: "./translations",
	LocaleFS:  assets,  // leave away to use the filesystem directly
}
// or use domain.Locale(lang...) to open a different locale's catalog
locale := domain.UserLocale()

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

for i := 0; i <= 10; i++ {
	fmt.Printf(locale.NGettext("%d thing\n", "%d things\n", uint32(i)), i)
}

TODO

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

Releases

No releases published

Packages

No packages published

Languages

  • Go 98.3%
  • Python 1.7%