Skip to content

Commit

Permalink
use github.com/ojii/gettext.go (pure go) instead of github.com/gosexy…
Browse files Browse the repository at this point in the history
…/gettext (cgo)
  • Loading branch information
mvo5 committed Nov 29, 2016
1 parent d641ddd commit b4542f3
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 46 deletions.
66 changes: 53 additions & 13 deletions i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,67 @@ package i18n
//go:generate update-pot

import (
"github.com/gosexy/gettext"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/ojii/gettext.go"

"github.com/snapcore/snapd/osutil"
)

// TEXTDOMAIN is the message domain used by snappy; see dgettext(3)
// for more information.
//
// Note that we have to use dgettext() here because we are a library
// and we can not use getext.Textdomain() as this would override the
// applications default
var TEXTDOMAIN = "snappy"
var (
TEXTDOMAIN = "snappy"
locale gettext.Catalog
translations gettext.Translations
)

func init() {
bindTextDomain(TEXTDOMAIN, "/usr/share/locale")
setLocale("")
}

func langpackResolver(root string, locale string, domain string) string {
r := filepath.Join(locale, "LC_MESSAGES", fmt.Sprintf("%s.mo", domain))

// ubuntu uses /usr/lib/locale-langpack and patches the glibc gettext
// implementation
langpack := filepath.Join(root, "..", "locale-langpack", r)
if osutil.FileExists(langpack) {
return langpack
}

// default
return filepath.Join(root, r)
}

func bindTextDomain(domain, dir string) {
translations = gettext.NewTranslations(dir, domain, langpackResolver)
}

func setLocale(loc string) {
if loc == "" {
loc = os.Getenv("LC_MESSAGES")
if loc == "" {
loc = os.Getenv("LANG")
}
}
// de_DE.UTF-8, de_DE@euro all need to get simplified
loc = strings.Split(loc, "@")[0]
loc = strings.Split(loc, ".")[0]

locale = translations.Locale(loc)
}

// G is the shorthand for Gettext
func G(msgid string) string {
return gettext.DGettext(TEXTDOMAIN, msgid)
return locale.Gettext(msgid)
}

// NG is the shorthand for NGettext
func NG(msgid string, msgidPlural string, n uint64) string {
return gettext.DNGettext(TEXTDOMAIN, msgid, msgidPlural, n)
}

func init() {
gettext.SetLocale(gettext.LC_ALL, "")
func NG(msgid string, msgidPlural string, n uint32) string {
return locale.NGettext(msgid, msgidPlural, n)
}
70 changes: 44 additions & 26 deletions i18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,12 @@ import (
"path/filepath"
"testing"

"github.com/gosexy/gettext"
. "gopkg.in/check.v1"
)

// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }

type i18nTestSuite struct {
}

var _ = Suite(&i18nTestSuite{})

func (s *i18nTestSuite) SetUpTest(c *C) {
// this dir contains a special hand-crafted en_DK/snappy-test.mo
// file
localeDir := c.MkDir()
makeMockTranslations(c, localeDir)

// this may fail on systems with no locale support (potentially
// minimal build environments)
gettext.BindTextdomain("snappy-test", localeDir)
locale := gettext.SetLocale(gettext.LC_ALL, "en_DK.UTF-8")
if locale != "en_DK.UTF-8" {
c.Skip("cannot init locale")
}
os.Setenv("LANGUAGE", "en_DK")

// we use a custom test mo file
TEXTDOMAIN = "snappy-test"
}

var mockLocalePo = []byte(`
msgid ""
msgstr ""
Expand All @@ -67,6 +42,7 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;>\n"
msgid "plural_1"
msgid_plural "plural_2"
Expand Down Expand Up @@ -94,8 +70,33 @@ func makeMockTranslations(c *C, localeDir string) {
c.Assert(err, IsNil)
}

type i18nTestSuite struct {
origLang string
origLcMessages string
}

var _ = Suite(&i18nTestSuite{})

func (s *i18nTestSuite) SetUpTest(c *C) {
// this dir contains a special hand-crafted en_DK/snappy-test.mo
// file
localeDir := c.MkDir()
makeMockTranslations(c, localeDir)

// we use a custom test mo file
TEXTDOMAIN = "snappy-test"

s.origLang = os.Getenv("LANG")
s.origLcMessages = os.Getenv("LC_MESSAGES")

bindTextDomain("snappy-test", localeDir)
os.Setenv("LANG", "en_DK.UTF-8")
setLocale("")
}

func (s *i18nTestSuite) TearDownTest(c *C) {
gettext.SetLocale(gettext.LC_ALL, "")
os.Setenv("LANG", s.origLang)
os.Setenv("LC_MESSAGES", s.origLcMessages)
}

func (s *i18nTestSuite) TestTranslatedSingular(c *C) {
Expand All @@ -109,3 +110,20 @@ func (s *i18nTestSuite) TestTranslatesPlural(c *C) {
var NGtest = NG
c.Assert(NGtest("plural_1", "plural_2", 1), Equals, "translated plural_1")
}

func (s *i18nTestSuite) TestTranslatedMissingLangNoCrash(c *C) {
setLocale("invalid")

// no G() to avoid adding the test string to snappy-pot
var Gtest = G
c.Assert(Gtest("singular"), Equals, "singular")
}

func (s *i18nTestSuite) TestInvalidTextDomainDir(c *C) {
bindTextDomain("snappy-test", "/random/not/existing/dir")
setLocale("invalid")

// no G() to avoid adding the test string to snappy-pot
var Gtest = G
c.Assert(Gtest("singular"), Equals, "singular")
}
2 changes: 1 addition & 1 deletion update-pot
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $GOPATH/bin/xgettext-go \
--msgid-bugs-address=snappy-devel@lists.ubuntu.com \
--keyword=i18n.G \
--keyword-plural=i18n.DG \
$(find $HERE -name "*.go")
$(find $HERE -name "*.go" -type f)

#xgettext -d snappy -o "$OUTPUT" --c++ --from-code=UTF-8 \
# --indent --add-comments=TRANSLATORS: --no-location --sort-output \
Expand Down
18 changes: 12 additions & 6 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@
"revision": "234959944d9cf05229b02e8b386e5cffe1e4e04a",
"revisionTime": "2016-01-28T16:48:56Z"
},
{
"checksumSHA1": "e0K21bVI674WQms/1HjP1m1hOVE=",
"path": "github.com/gosexy/gettext",
"revision": "98b7b91596d20b96909e6b60d57411547dd9959c",
"revisionTime": "2013-02-21T11:21:43Z"
},
{
"checksumSHA1": "Fsff4Yngdyqbq9ulSyTT4LGrxck=",
"path": "github.com/jessevdk/go-flags",
Expand All @@ -62,6 +56,18 @@
"revision": "361f6ebcbb54f389d15dc9faefa000e996ba3e37",
"revisionTime": "2015-07-22T06:53:40Z"
},
{
"checksumSHA1": "lG6diF/yE9cGgQIKRAlsaeYAjO4=",
"path": "github.com/ojii/gettext.go",
"revision": "95289a7e0ac17c76737a5ceca3c9471c0adf70c7",
"revisionTime": "2016-07-14T06:47:45Z"
},
{
"checksumSHA1": "j5FytTwC2nAqSrDR7V7O7E4cHKM=",
"path": "github.com/ojii/gettext.go/pluralforms",
"revision": "95289a7e0ac17c76737a5ceca3c9471c0adf70c7",
"revisionTime": "2016-07-14T06:47:45Z"
},
{
"checksumSHA1": "G1Zy6KNKWSz6Nx6GAgaKM2yPDLg=",
"path": "github.com/testing-cabal/subunit-go",
Expand Down

0 comments on commit b4542f3

Please sign in to comment.