Golang (Go) bindings for GNU's gettext (http://www.gnu.org/software/gettext/)


Build Status


Go bindings for GNU gettext, an internationalization and localization library for writing multilingual systems.



Installation should be straightforward on Linux.


Installing gettext on a Mac is a bit awkward:

brew install gettext

export CGO_LDFLAGS=-L/usr/local/opt/gettext/lib
export CGO_CPPFLAGS=-I/usr/local/opt/gettext/include

go get github.com/gosexy/gettext

Getting the library

Use go get to download and install the binding:

go get github.com/gosexy/gettext


This is an example program showing the BindTextdomain, Textdomain and SetLocale bindings:

package main

import (


func main() {
	textDomain := "default"

	gettext.BindTextdomain(textDomain, "path/to/domains")

	gettext.SetLocale(gettext.LcAll, "es_MX.utf8")

	fmt.Println(gettext.Gettext("Hello, world!"))

Set the LANGUAGE env to the name of the language you want to use in your program:

export LANGUAGE="es_MX.utf8"

You can use the xgettext command to extract strings to be translated from a Go program:

go get github.com/gosexy/gettext/go-xgettext

go-xgettext -o outfile.pot --keyword=Gettext --keyword-plural=NGettext infile.go

This will generate a example.pot file.

After actually translating the .pot file, you'll have to generate .po and .mo files with msginit and msgfmt:

msginit -l es_MX -o example.po -i example.pot
msgfmt -c -v -o example.mo example.po

Finally, move the .mo file to an appropriate location.

mv example.mo examples/es_MX.utf8/LC_MESSAGES/example.mo


Check out the API documentation godoc.org/github.com/gosexy/gettext).

The original gettext documentation:

man 3 gettext

And here's a good tutorial on using gettext.

  • Bugfix/fix tests on linux

    Bugfix/fix tests on linux

    Trivial fix, build on top of the previosu travis.yml branch to verify it works on travis too (and not only in my local machine :)

    Unfortunately I have no good explanation on the WHY. Setting the environment via the environment works, setting it via setlocale() does not. I looked into glibcs locale code but got lost in the twisted maze of _nl_global_locale.__names[category]. However I think the following is happening:

    On ubuntu there is only a subset of locales enables by default (locale -a tells which). So setlocale() on my system for spanish fails and this makes the test fail. Same on travis where the only locale is english. However gettext and locale are two different parts of glibc. So setlocale fails (return return value is "") but thats ok because in gcigettext.c it will use guess_category_value() which will look into the LANGUAGE environment to find the right language (thats the theory from my incomplete understanding of the glibc code).

    I am not very happy about this branch, but it seems like this is really a bit tricky to test properly.

    opened by mvo5 4
  • add travis.yml

    add travis.yml

    This is a tiny branch that just adds basic .travis.yml integration.

    Before you merge you need to flick the repository switch on in travis-ci.org under your account. Then you should see the travis build and the travis banner in your readme :)

    Note that the build fails right now, which I can also reproduce locally. I bisected it and it starts breaking with commit 5269cf26ba096845b4a62901b7b1ebb49781177a on ubuntu. This might be a linux specific issue but before digging into it I wanted to add thetravis support first in order to avoid this kind of issue in the future.

    Thanks for your consideration!

    opened by mvo5 4
  • [whishlist] provide xgettext to extract `` strings

    [whishlist] provide xgettext to extract `` strings


    I use gosexy in one of my projects and I'm quite happy. One issue I noticed is that xgettext does not extract `` style strings. I created a custom go xgettext for this that uses the golang/ast module and that seems to make the extraction very simple. If thats something you are interessted in I can create a branch and send you a pull request, my current proof-of-concept is at http://bazaar.launchpad.net/~mvo/snappy/snapy-out-own-xgettext/view/head:/i18n/xgettext-go/main.go (but the real version would look a lot nicer :)

    Cheers, Michael

    opened by mvo5 4
  • Please consider doing a new upstream release

    Please consider doing a new upstream release


    With my downstream packaging hat on I'd like to encourage you to tag and release a formal upstream version. This simplifies downstream packaging.

    Thank You ZK

    opened by zyga 2
  • Add native go xgettext

    Add native go xgettext

    Upstream xgettext does not provide support for the go raw_string_lit (backtick) multi-line strings. Thanks to the excellent go/ast package its easy to build a native xgettext in go itself.

    We use this successfully in our "snappy" and "lxd" projects. You may want to update the main docs to mention that gosexy/gettext has its own xgettext now too :)

    opened by mvo5 2
  • How to set locale per goroutine?

    How to set locale per goroutine?

    I want to use this in a web app, where potentially every request comes in for a different locale.

    The SetLocale() command seems to be global.

    How do you handle this in case where an app is serving in multiple languages similtaneously, different locale per goroutine?

    If this functionality already exists, please document how.

    Thank you

    opened by jacek99 2
  • Fix translations with DGettext (and other D... func) when the domain name is empty.

    Fix translations with DGettext (and other D... func) when the domain name is empty.

    According to the doc, NULL allow to use the current domain as fallback: http://www.gnu.org/software/libc/manual/html_node/Translation-with-gettext.html If the domainname parameter is the null pointer the dgettext function is exactly equivalent to gettext since the default value for the domain name is used.

    opened by sqp 0
  • Travis jobs failing on Intel

    Travis jobs failing on Intel

    Hi All, I am running jobs on travis-ci and found that jobs are failing for go1.11.X and 1.12.X. Below are the logs

    $ git clone --depth=50 --branch=master https://github.com/kishorkunal-raj/gettext.git kishorkunal-raj/gettext $ travis_export_go 1.11.x github.com/kishorkunal-raj/gettext 0.01s0.08s$ travis_setup_go go version go1.11.13 linux/amd64 $ export GOPATH="/home/travis/gopath" $ export PATH="/home/travis/gopath/bin:/home/travis/.gimme/versions/go1.11.13.linux.amd64/bin:/home/travis/bin:/home/travis/bin:/home/travis/.local/bin:/usr/local/lib/jvm/openjdk11/bin:/opt/pyenv/shims:/home/travis/.phpenv/shims:/home/travis/perl5/perlbrew/bin:/home/travis/.nvm/versions/node/v8.12.0/bin:/home/travis/.rvm/gems/ruby-2.5.3/bin:/home/travis/.rvm/gems/[email protected]/bin:/home/travis/.rvm/rubies/ruby-2.5.3/bin:/home/travis/gopath/bin:/home/travis/.gimme/versions/go1.11.1.linux.amd64/bin:/usr/local/maven-3.6.3/bin:/usr/local/cmake-3.12.4/bin:/usr/local/clang-7.0.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/home/travis/.rvm/bin:/home/travis/.phpenv/bin:/opt/pyenv/bin:/home/travis/.yarn/bin" $ export GO111MODULE="auto" $ gimme version v1.5.4 $ go version go version go1.11.13 linux/amd64 go.env $ go env install 13.36s$ go get -t -v ./... 6.36s$ go test -v ./...


    ../../stretchr/testify/assert/assertions.go:1703:5: undefined: errors.Is ../../stretchr/testify/assert/assertions.go:1726:6: undefined: errors.Is ../../stretchr/testify/assert/assertions.go:1749:5: undefined: errors.As ../../stretchr/testify/assert/assertions.go:1766:7: undefined: errors.Unwrap ../../stretchr/testify/assert/assertions.go:1770:7: undefined: errors.Unwrap FAIL github.com/kishorkunal-raj/gettext [build failed] === RUN Test OK: 16 passed --- PASS: Test (0.00s) PASS ok github.com/kishorkunal-raj/gettext/go-xgettext 0.006s The command "go test -v ./..." exited with 2.

    Done. Your build exited with 1.

    Please help me out to fix it.

    opened by kishorkunal-raj 0
  • Panic: runtime error: index out of range

    Panic: runtime error: index out of range

    The following code causes a panic:

    package x
    func fn() {
    go-xgettext -o "./pkg/locales/default.pot" --keyword=gotext.Get pkg/apiserver/controller/site/env_var.go
    panic: runtime error: index out of range
    goroutine 1 [running]:
    main.inspectNodeForTranslations(0xc000050100, 0xc000072100, 0x56c0c0, 0xc000050140, 0x8)
    	/home/presslabs/go/src/github.com/gosexy/gettext/go-xgettext/main.go:155 +0xb7b
    main.processSingleGoSource.func1(0x56c0c0, 0xc000050140, 0xc00000a4a0)
    	/home/presslabs/go/src/github.com/gosexy/gettext/go-xgettext/main.go:236 +0x47
    go/ast.inspector.Visit(0xc00000a1a0, 0x56c0c0, 0xc000050140, 0x10, 0x54b084)
    	/usr/local/go/src/go/ast/walk.go:373 +0x3a
    go/ast.Walk(0x56bd80, 0xc00000a1a0, 0x56c0c0, 0xc000050140)
    	/usr/local/go/src/go/ast/walk.go:52 +0x66
    go/ast.Walk(0x56bd80, 0xc00000a1a0, 0x56c380, 0xc000046590)
    	/usr/local/go/src/go/ast/walk.go:196 +0x1b12
    go/ast.walkStmtList(0x56bd80, 0xc00000a1a0, 0xc0000465a0, 0x1, 0x1)
    	/usr/local/go/src/go/ast/walk.go:32 +0x9e
    go/ast.Walk(0x56bd80, 0xc00000a1a0, 0x56c040, 0xc0000605d0)
    	/usr/local/go/src/go/ast/walk.go:224 +0x1b67
    go/ast.Walk(0x56bd80, 0xc00000a1a0, 0x56c4c0, 0xc000060600)
    	/usr/local/go/src/go/ast/walk.go:344 +0xd6d
    go/ast.walkDeclList(0x56bd80, 0xc00000a1a0, 0xc0000465b0, 0x1, 0x1)
    	/usr/local/go/src/go/ast/walk.go:38 +0x9e
    go/ast.Walk(0x56bd80, 0xc00000a1a0, 0x56c440, 0xc000072100)
    	/usr/local/go/src/go/ast/walk.go:353 +0x2656
    go/ast.Inspect(0x56c440, 0xc000072100, 0xc00000a1a0)
    	/usr/local/go/src/go/ast/walk.go:385 +0x4b
    main.processSingleGoSource(0xc000050100, 0x7fffabd2737f, 0x28, 0xc00000e140, 0x2)
    	/home/presslabs/go/src/github.com/gosexy/gettext/go-xgettext/main.go:235 +0x14e
    main.processFiles(0xc00000e150, 0x1, 0x4, 0x5, 0x5)
    	/home/presslabs/go/src/github.com/gosexy/gettext/go-xgettext/main.go:215 +0xa8
    	/home/presslabs/go/src/github.com/gosexy/gettext/go-xgettext/main.go:344 +0x138

    And the following code doesn't cause panic:

    package x
    func fn() {
    opened by rstefan1 0
  • Panic: interface conversion: ast.Expr is *ast.SelectorExpr, not *ast.Ident

    Panic: interface conversion: ast.Expr is *ast.SelectorExpr, not *ast.Ident

    The following code causes a panic:

    package x
    func fn() {
    go-xgettext -o "./pkg/locales/default.pot" --keyword=gotext.Get pkg/file.go
    panic: interface conversion: ast.Expr is *ast.SelectorExpr, not *ast.Ident
    goroutine 1 [running]:
    main.inspectNodeForTranslations(0xc000050100, 0xc000072100, 0x56c0c0, 0xc000050140, 0x8)
    	/home/presslabs/go/src/github.com/gosexy/gettext/go-xgettext/main.go:163 +0xafd
    main.processSingleGoSource.func1(0x56c0c0, 0xc000050140, 0xc00000a540)
    	/home/presslabs/go/src/github.com/gosexy/gettext/go-xgettext/main.go:236 +0x47
    go/ast.inspector.Visit(0xc00000a240, 0x56c0c0, 0xc000050140, 0x10, 0x54b084)
    	/usr/local/go/src/go/ast/walk.go:373 +0x3a
    go/ast.Walk(0x56bd80, 0xc00000a240, 0x56c0c0, 0xc000050140)
    	/usr/local/go/src/go/ast/walk.go:52 +0x66
    go/ast.Walk(0x56bd80, 0xc00000a240, 0x56c380, 0xc0000465b0)
    	/usr/local/go/src/go/ast/walk.go:196 +0x1b12
    go/ast.walkStmtList(0x56bd80, 0xc00000a240, 0xc0000465c0, 0x1, 0x1)
    	/usr/local/go/src/go/ast/walk.go:32 +0x9e
    go/ast.Walk(0x56bd80, 0xc00000a240, 0x56c040, 0xc0000605d0)
    	/usr/local/go/src/go/ast/walk.go:224 +0x1b67
    go/ast.Walk(0x56bd80, 0xc00000a240, 0x56c4c0, 0xc000060600)
    	/usr/local/go/src/go/ast/walk.go:344 +0xd6d
    go/ast.walkDeclList(0x56bd80, 0xc00000a240, 0xc0000465d0, 0x1, 0x1)
    	/usr/local/go/src/go/ast/walk.go:38 +0x9e
    go/ast.Walk(0x56bd80, 0xc00000a240, 0x56c440, 0xc000072100)
    	/usr/local/go/src/go/ast/walk.go:353 +0x2656
    go/ast.Inspect(0x56c440, 0xc000072100, 0xc00000a240)
    	/usr/local/go/src/go/ast/walk.go:385 +0x4b
    main.processSingleGoSource(0xc000050100, 0x7fffd2dc037f, 0x28, 0xc00000e140, 0x2)
    	/home/presslabs/go/src/github.com/gosexy/gettext/go-xgettext/main.go:235 +0x14e
    main.processFiles(0xc00000e150, 0x1, 0x4, 0x5, 0x5)
    	/home/presslabs/go/src/github.com/gosexy/gettext/go-xgettext/main.go:215 +0xa8
    	/home/presslabs/go/src/github.com/gosexy/gettext/go-xgettext/main.go:344 +0x138

    But the following code doesn't cause the panic:

    package x
    func fn() {
    opened by rstefan1 0
  • Support nested selector

    Support nested selector

    This PR is to support nested selector expression in source code. For example --keyword=bar.T and in the source foo.bar.T("message"). When using translation package https://github.com/leonelquinteros/gotext, a locale object can be created and that locale object can be a field of another object.

    opened by JiangtianLi 0
  • Allow multiple keywords

    Allow multiple keywords

    xgettext supports multiple keywords (xgettext -k i18n.I -k i18n.Translate).

    Do the same for go-xgettext by supporting a comma-separated list of keywords for both --keyword and --keyword-plural options.

    opened by holygeek 0
Go toolbelt
Go toolbelt
:ab: GNU gettext for Go (Imported By Kubernetes)

Go语言QQ群: 102319854, 1055927514 凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa gettext-go: GNU gettext for Go (Imported By Ku

chai2010 80 Nov 12, 2022
Go (golang) implementation of http://www.hashids.org

go-hashids Go (golang) v1 implementation of http://www.hashids.org under MIT License (same as the original implementations) Original implementations b

Rémi Gillig 1.1k Dec 2, 2022
A codename generator meant for naming software releases.

codename-generator This library written in Golang generates a random code name meant for naming software releases if you run short of inspiration. Cur

Jonathan Gautheron 22 Jun 26, 2022
Minimal cgo bindings for libenca

enca This is a minimal cgo bindings for libenca. If you need to detect the language of a string you can use guesslanguage package. Supported Go versio

Nikita Vershinin 13 Nov 6, 2022
Upskirt markdown library bindings for Go

Goskirt Package goskirt provides Go-bindings for the excellent Sundown Markdown parser. (F/K/A Upskirt). To use goskirt, create a new Goskirt-value wi

Jukka-Pekka Kekkonen 32 Oct 23, 2022
[Crawler/Scraper for Golang]🕷A lightweight distributed friendly Golang crawler framework.一个轻量的分布式友好的 Golang 爬虫框架。

Goribot 一个分布式友好的轻量的 Golang 爬虫框架。 完整文档 | Document !! Warning !! Goribot 已经被迁移到 Gospider|github.com/zhshch2002/gospider。修复了一些调度问题并分离了网络请求部分到另一个仓库。此仓库会继续

null 210 Oct 29, 2022
golang 在线预览word,excel,pdf,MarkDown(Online Preview Word,Excel,PPT,PDF,Image by Golang)

Go View File 在线体验地址 (不会经常更新,保留最基本的预览功能。服务器配置较低,如果出现链接超时请等待几秒刷新重试,或者换Chrome) 目前已经完成 docker部署 (不用为运行环境烦恼) Wor

CZC 76 Nov 23, 2022
bluemonday: a fast golang HTML sanitizer (inspired by the OWASP Java HTML Sanitizer) to scrub user generated content of XSS

bluemonday bluemonday is a HTML sanitizer implemented in Go. It is fast and highly configurable. bluemonday takes untrusted user generated content as

Microcosm 2.5k Dec 1, 2022
Elegant Scraper and Crawler Framework for Golang

Colly Lightning Fast and Elegant Scraping Framework for Gophers Colly provides a clean interface to write any kind of crawler/scraper/spider. With Col

Colly 18.3k Nov 24, 2022
A golang package to work with Decentralized Identifiers (DIDs)

did did is a Go package that provides tools to work with Decentralized Identifiers (DIDs). Install go get github.com/ockam-network/did Example packag

Ockam 69 Nov 25, 2022
wcwidth for golang

go-runewidth Provides functions to get fixed width of the character or string. Usage runewidth.StringWidth("つのだ☆HIRO") == 12 Author Yasuhiro Matsumoto

mattn 497 Nov 27, 2022
Parses the Graphviz DOT language in golang

Parses the Graphviz DOT language and creates an interface, in golang, with which to easily create new and manipulate existing graphs which can be writ

Walter Schulze 504 Nov 23, 2022
htmlquery is golang XPath package for HTML query.

htmlquery Overview htmlquery is an XPath query package for HTML, lets you extract data or evaluate from HTML documents by an XPath expression. htmlque

null 537 Nov 27, 2022
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.

omniparser Omniparser is a native Golang ETL parser that ingests input data of various formats (CSV, txt, fixed length/width, XML, EDI/X12/EDIFACT, JS

JF Technology 518 Nov 18, 2022
Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and struct tags for golang crawler

Pagser Pagser inspired by page parser。 Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and str

foolin 72 Nov 14, 2022
iTunes and RSS 2.0 Podcast Generator in Golang

podcast Package podcast generates a fully compliant iTunes and RSS 2.0 podcast feed for GoLang using a simple API. Full documentation with detailed ex

Eric Duncan 113 Nov 30, 2022
TOML parser for Golang with reflection.

THIS PROJECT IS UNMAINTAINED The last commit to this repo before writing this message occurred over two years ago. While it was never my intention to

Andrew Gallant 4k Nov 28, 2022
Your CSV pocket-knife (golang)

csvutil - Your CSV pocket-knife (golang) #WARNING I would advise against using this package. It was a language learning exercise from a time before "e

Bryan Matsuo 44 Oct 24, 2022
agrep-like fuzzy matching, but made faster using Golang and precomputation.

goagrep There are situations where you want to take the user's input and match a primary key in a database. But, immediately a problem is introduced:

Zack 42 Oct 8, 2022