github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/packaging/debian-sid/patches/0007-i18n-use-dummy-localizations-to-avoid-dependencies.patch (about) 1 From b8cbff70f32d88d14b1ff4fed35d83bd99f37a3a Mon Sep 17 00:00:00 2001 2 From: Zygmunt Krynicki <me@zygoon.pl> 3 Date: Thu, 17 Jan 2019 16:42:35 +0200 4 Subject: [PATCH 7/9] i18n: use dummy localizations to avoid dependencies 5 6 Upstream snapd uses the github.com/ojii/gettext.go package for access to 7 translation catalogs. This package is currently not available in Debian 8 and prevents building the package. As such, replace the real 9 implementation with a simple dummy one that always uses the English 10 input strings. 11 12 Signed-off-by: Zygmunt Krynicki <me@zygoon.pl> 13 --- 14 i18n/i18n.go | 97 +++-------------------------------- 15 i18n/i18n_test.go | 125 ++-------------------------------------------- 16 2 files changed, 11 insertions(+), 211 deletions(-) 17 18 diff --git a/i18n/i18n.go b/i18n/i18n.go 19 index fb50901a8..79f28545b 100644 20 --- a/i18n/i18n.go 21 +++ b/i18n/i18n.go 22 @@ -19,101 +19,16 @@ 23 24 package i18n 25 26 -//go:generate update-pot 27 - 28 -import ( 29 - "fmt" 30 - "os" 31 - "path/filepath" 32 - "strings" 33 - 34 - "github.com/snapcore/go-gettext" 35 - 36 - "github.com/snapcore/snapd/dirs" 37 - "github.com/snapcore/snapd/osutil" 38 -) 39 - 40 -// TEXTDOMAIN is the message domain used by snappy; see dgettext(3) 41 -// for more information. 42 -var ( 43 - TEXTDOMAIN = "snappy" 44 - locale gettext.Catalog 45 - translations gettext.Translations 46 -) 47 - 48 -func init() { 49 - bindTextDomain(TEXTDOMAIN, "/usr/share/locale") 50 - setLocale("") 51 -} 52 - 53 -func langpackResolver(baseRoot string, locale string, domain string) string { 54 - // first check for the real locale (e.g. de_DE) 55 - // then try to simplify the locale (e.g. de_DE -> de) 56 - locales := []string{locale, strings.SplitN(locale, "_", 2)[0]} 57 - for _, locale := range locales { 58 - r := filepath.Join(locale, "LC_MESSAGES", fmt.Sprintf("%s.mo", domain)) 59 - 60 - // look into the core snaps first for translations, 61 - // then the main system 62 - candidateDirs := []string{ 63 - filepath.Join(dirs.SnapMountDir, "/core/current/", baseRoot), 64 - baseRoot, 65 - } 66 - for _, root := range candidateDirs { 67 - // ubuntu uses /usr/lib/locale-langpack and patches the glibc gettext 68 - // implementation 69 - langpack := filepath.Join(root, "..", "locale-langpack", r) 70 - if osutil.FileExists(langpack) { 71 - return langpack 72 - } 73 - 74 - regular := filepath.Join(root, r) 75 - if osutil.FileExists(regular) { 76 - return regular 77 - } 78 - } 79 - } 80 - 81 - return "" 82 -} 83 - 84 -func bindTextDomain(domain, dir string) { 85 - translations = gettext.NewTranslations(dir, domain, langpackResolver) 86 -} 87 - 88 -func setLocale(loc string) { 89 - if loc == "" { 90 - loc = os.Getenv("LC_MESSAGES") 91 - if loc == "" { 92 - loc = os.Getenv("LANG") 93 - } 94 - } 95 - // de_DE.UTF-8, de_DE@euro all need to get simplified 96 - loc = strings.Split(loc, "@")[0] 97 - loc = strings.Split(loc, ".")[0] 98 - 99 - locale = translations.Locale(loc) 100 -} 101 - 102 // G is the shorthand for Gettext 103 func G(msgid string) string { 104 - return locale.Gettext(msgid) 105 -} 106 - 107 -// https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html 108 -// (search for 1000) 109 -func ngn(d int) uint32 { 110 - const max = 1000000 111 - if d < 0 { 112 - d = -d 113 - } 114 - if d > max { 115 - return uint32((d % max) + max) 116 - } 117 - return uint32(d) 118 + return msgid 119 } 120 121 // NG is the shorthand for NGettext 122 func NG(msgid string, msgidPlural string, n int) string { 123 - return locale.NGettext(msgid, msgidPlural, ngn(n)) 124 + if n == 1 { 125 + return msgid 126 + } else { 127 + return msgidPlural 128 + } 129 } 130 diff --git a/i18n/i18n_test.go b/i18n/i18n_test.go 131 index 81cb050a7..86b59f3b4 100644 132 --- a/i18n/i18n_test.go 133 +++ b/i18n/i18n_test.go 134 @@ -20,143 +20,28 @@ 135 package i18n 136 137 import ( 138 - "io/ioutil" 139 - "os" 140 - "os/exec" 141 - "path/filepath" 142 "testing" 143 144 . "gopkg.in/check.v1" 145 - 146 - "github.com/snapcore/snapd/dirs" 147 ) 148 149 // Hook up check.v1 into the "go test" runner 150 func Test(t *testing.T) { TestingT(t) } 151 152 -var mockLocalePo = []byte(` 153 -msgid "" 154 -msgstr "" 155 -"Project-Id-Version: snappy-test\n" 156 -"Report-Msgid-Bugs-To: snappy-devel@lists.ubuntu.com\n" 157 -"POT-Creation-Date: 2015-06-16 09:08+0200\n" 158 -"Language: en_DK\n" 159 -"MIME-Version: 1.0\n" 160 -"Content-Type: text/plain; charset=UTF-8\n" 161 -"Content-Transfer-Encoding: 8bit\n" 162 -"Plural-Forms: nplurals=2; plural=n != 1;>\n" 163 - 164 -msgid "plural_1" 165 -msgid_plural "plural_2" 166 -msgstr[0] "translated plural_1" 167 -msgstr[1] "translated plural_2" 168 - 169 -msgid "singular" 170 -msgstr "translated singular" 171 -`) 172 - 173 -func makeMockTranslations(c *C, localeDir string) { 174 - fullLocaleDir := filepath.Join(localeDir, "en_DK", "LC_MESSAGES") 175 - err := os.MkdirAll(fullLocaleDir, 0755) 176 - c.Assert(err, IsNil) 177 - 178 - po := filepath.Join(fullLocaleDir, "snappy-test.po") 179 - mo := filepath.Join(fullLocaleDir, "snappy-test.mo") 180 - err = ioutil.WriteFile(po, mockLocalePo, 0644) 181 - c.Assert(err, IsNil) 182 - 183 - cmd := exec.Command("msgfmt", po, "--output-file", mo) 184 - cmd.Stdout = os.Stdout 185 - cmd.Stderr = os.Stderr 186 - err = cmd.Run() 187 - c.Assert(err, IsNil) 188 -} 189 - 190 -type i18nTestSuite struct { 191 - origLang string 192 - origLcMessages string 193 -} 194 +type i18nTestSuite struct{} 195 196 var _ = Suite(&i18nTestSuite{}) 197 198 -func (s *i18nTestSuite) SetUpTest(c *C) { 199 - // this dir contains a special hand-crafted en_DK/snappy-test.mo 200 - // file 201 - localeDir := c.MkDir() 202 - makeMockTranslations(c, localeDir) 203 - 204 - // we use a custom test mo file 205 - TEXTDOMAIN = "snappy-test" 206 - 207 - s.origLang = os.Getenv("LANG") 208 - s.origLcMessages = os.Getenv("LC_MESSAGES") 209 - 210 - bindTextDomain("snappy-test", localeDir) 211 - os.Setenv("LANG", "en_DK.UTF-8") 212 - setLocale("") 213 -} 214 - 215 -func (s *i18nTestSuite) TearDownTest(c *C) { 216 - os.Setenv("LANG", s.origLang) 217 - os.Setenv("LC_MESSAGES", s.origLcMessages) 218 -} 219 - 220 func (s *i18nTestSuite) TestTranslatedSingular(c *C) { 221 // no G() to avoid adding the test string to snappy-pot 222 var Gtest = G 223 - c.Assert(Gtest("singular"), Equals, "translated singular") 224 + c.Assert(Gtest("singular"), Equals, "singular") 225 } 226 227 func (s *i18nTestSuite) TestTranslatesPlural(c *C) { 228 // no NG() to avoid adding the test string to snappy-pot 229 var NGtest = NG 230 - c.Assert(NGtest("plural_1", "plural_2", 1), Equals, "translated plural_1") 231 -} 232 - 233 -func (s *i18nTestSuite) TestTranslatedMissingLangNoCrash(c *C) { 234 - setLocale("invalid") 235 - 236 - // no G() to avoid adding the test string to snappy-pot 237 - var Gtest = G 238 - c.Assert(Gtest("singular"), Equals, "singular") 239 -} 240 - 241 -func (s *i18nTestSuite) TestInvalidTextDomainDir(c *C) { 242 - bindTextDomain("snappy-test", "/random/not/existing/dir") 243 - setLocale("invalid") 244 - 245 - // no G() to avoid adding the test string to snappy-pot 246 - var Gtest = G 247 - c.Assert(Gtest("singular"), Equals, "singular") 248 -} 249 - 250 -func (s *i18nTestSuite) TestLangpackResolverFromLangpack(c *C) { 251 - root := c.MkDir() 252 - localeDir := filepath.Join(root, "/usr/share/locale") 253 - err := os.MkdirAll(localeDir, 0755) 254 - c.Assert(err, IsNil) 255 - 256 - d := filepath.Join(root, "/usr/share/locale-langpack") 257 - makeMockTranslations(c, d) 258 - bindTextDomain("snappy-test", localeDir) 259 - setLocale("") 260 - 261 - // no G() to avoid adding the test string to snappy-pot 262 - var Gtest = G 263 - c.Assert(Gtest("singular"), Equals, "translated singular", Commentf("test with %q failed", d)) 264 -} 265 - 266 -func (s *i18nTestSuite) TestLangpackResolverFromCore(c *C) { 267 - origSnapMountDir := dirs.SnapMountDir 268 - defer func() { dirs.SnapMountDir = origSnapMountDir }() 269 - dirs.SnapMountDir = c.MkDir() 270 - 271 - d := filepath.Join(dirs.SnapMountDir, "/core/current/usr/share/locale") 272 - makeMockTranslations(c, d) 273 - bindTextDomain("snappy-test", "/usr/share/locale") 274 - setLocale("") 275 - 276 - // no G() to avoid adding the test string to snappy-pot 277 - var Gtest = G 278 - c.Assert(Gtest("singular"), Equals, "translated singular", Commentf("test with %q failed", d)) 279 + c.Assert(NGtest("plural_1", "plural_2", 0), Equals, "plural_2") 280 + c.Assert(NGtest("plural_1", "plural_2", 1), Equals, "plural_1") 281 + c.Assert(NGtest("plural_1", "plural_2", 2), Equals, "plural_2") 282 } 283 -- 284 2.17.1 285