diff --git a/i18n/i18n.go b/i18n/i18n.go index 02be0b62b5f..b03278b0b1b 100644 --- a/i18n/i18n.go +++ b/i18n/i18n.go @@ -29,6 +29,7 @@ import ( "github.com/ojii/gettext.go" + "github.com/snapcore/snapd/dirs" "github.com/snapcore/snapd/osutil" ) @@ -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 + } } } diff --git a/i18n/i18n_test.go b/i18n/i18n_test.go index 0caa2f66aae..81cb050a76b 100644 --- a/i18n/i18n_test.go +++ b/i18n/i18n_test.go @@ -27,6 +27,8 @@ import ( "testing" . "gopkg.in/check.v1" + + "github.com/snapcore/snapd/dirs" ) // Hook up check.v1 into the "go test" runner @@ -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" @@ -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)) +}