Skip to content

Commit

Permalink
i18n: look into core snaps when checking for translations (#2654)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 authored and niemeyer committed Feb 14, 2017
1 parent a7d16e9 commit adcf908
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
30 changes: 19 additions & 11 deletions i18n/i18n.go
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/ojii/gettext.go"

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

Expand All @@ -45,24 +46,31 @@ func init() {
setLocale("")
}

func langpackResolver(root string, locale string, domain string) string {

func langpackResolver(baseRoot string, locale string, domain string) string {
// first check for the real locale (e.g. de_DE)
// then try to simplify the locale (e.g. de_DE -> de)
locales := []string{locale, strings.SplitN(locale, "_", 2)[0]}
for _, locale := range locales {
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
// look into the core snaps first for translations,
// then the main system
candidateDirs := []string{
filepath.Join(dirs.SnapMountDir, "/core/current/", baseRoot),
baseRoot,
}

regular := filepath.Join(root, r)
if osutil.FileExists(regular) {
return regular
for _, root := range candidateDirs {
// 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
}

regular := filepath.Join(root, r)
if osutil.FileExists(regular) {
return regular
}
}
}

Expand Down
35 changes: 34 additions & 1 deletion i18n/i18n_test.go
Expand Up @@ -27,6 +27,8 @@ import (
"testing"

. "gopkg.in/check.v1"

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

// Hook up check.v1 into the "go test" runner
Expand All @@ -35,7 +37,7 @@ func Test(t *testing.T) { TestingT(t) }
var mockLocalePo = []byte(`
msgid ""
msgstr ""
"Project-Id-Version: snappy\n"
"Project-Id-Version: snappy-test\n"
"Report-Msgid-Bugs-To: snappy-devel@lists.ubuntu.com\n"
"POT-Creation-Date: 2015-06-16 09:08+0200\n"
"Language: en_DK\n"
Expand Down Expand Up @@ -127,3 +129,34 @@ func (s *i18nTestSuite) TestInvalidTextDomainDir(c *C) {
var Gtest = G
c.Assert(Gtest("singular"), Equals, "singular")
}

func (s *i18nTestSuite) TestLangpackResolverFromLangpack(c *C) {
root := c.MkDir()
localeDir := filepath.Join(root, "/usr/share/locale")
err := os.MkdirAll(localeDir, 0755)
c.Assert(err, IsNil)

d := filepath.Join(root, "/usr/share/locale-langpack")
makeMockTranslations(c, d)
bindTextDomain("snappy-test", localeDir)
setLocale("")

// no G() to avoid adding the test string to snappy-pot
var Gtest = G
c.Assert(Gtest("singular"), Equals, "translated singular", Commentf("test with %q failed", d))
}

func (s *i18nTestSuite) TestLangpackResolverFromCore(c *C) {
origSnapMountDir := dirs.SnapMountDir
defer func() { dirs.SnapMountDir = origSnapMountDir }()
dirs.SnapMountDir = c.MkDir()

d := filepath.Join(dirs.SnapMountDir, "/core/current/usr/share/locale")
makeMockTranslations(c, d)
bindTextDomain("snappy-test", "/usr/share/locale")
setLocale("")

// no G() to avoid adding the test string to snappy-pot
var Gtest = G
c.Assert(Gtest("singular"), Equals, "translated singular", Commentf("test with %q failed", d))
}

0 comments on commit adcf908

Please sign in to comment.