aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 3cfc61bff7ae5189c35bb8e28ca5a1c16128ab83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
[![Build Status][travis-image]][travis-url] 
# gosexy/gettext

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

## Requeriments

* [GNU gettext][1]

### Linux

Installation should be straightforward on Linux.

### OSX

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:

```sh
go get github.com/gosexy/gettext
```

## Usage

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

```go
package main

import (
	"fmt"

	"github.com/gosexy/gettext"
)

func main() {
	textDomain := "default"

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

	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:

```sh
export LANGUAGE="es_MX.utf8"
./myapp
```

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`:

```sh
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.

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

## Documentation

Check out the API documentation [godoc.org/github.com/gosexy/gettext)](http://godoc.org/github.com/gosexy/gettext).

The original gettext documentation:

```sh
man 3 gettext
```

And here's a [good tutorial][2] on using gettext.

[1]: http://www.gnu.org/software/gettext/
[2]: http://oriya.sarovar.org/docs/gettext_single.html


[travis-image]: https://travis-ci.org/gosexy/gettext.svg?branch=master
[travis-url]: https://travis-ci.org/gosexy/gettext