feat: keyword-ctx

See toolshed/abra#647
This commit is contained in:
2025-09-28 10:44:16 +02:00
parent 7c1823dacd
commit 8bba24cd1e
2 changed files with 220 additions and 29 deletions

View File

@ -54,6 +54,7 @@ func (s *xgettextTestSuite) SetUpTest(c *C) {
opts.AddCommentsTag = "TRANSLATORS:"
opts.Keyword = "i18n.G"
opts.KeywordPlural = "i18n.NG"
opts.KeywordContext = "i18n.GC"
opts.SortOutput = true
opts.PackageName = "snappy"
opts.MsgIDBugsAddress = "snappy-devel@lists.ubuntu.com"
@ -102,6 +103,29 @@ func main() {
})
}
func (s *xgettextTestSuite) TestProcessFilesSimpleWithContext(c *C) {
fname := makeGoSourceFile(c, []byte(`package main
func main() {
// TRANSLATORS: foo comment
i18n.GC("foo", "foo context")
}
`))
err := processFiles([]string{fname})
c.Assert(err, IsNil)
c.Assert(msgIDs, DeepEquals, map[string][]msgID{
"foo": {
{
comment: "#. TRANSLATORS: foo comment\n",
context: "foo context",
fname: fname,
line: 5,
},
},
})
}
func (s *xgettextTestSuite) TestProcessFilesMultiple(c *C) {
fname := makeGoSourceFile(c, []byte(`package main
@ -132,6 +156,38 @@ func main() {
})
}
func (s *xgettextTestSuite) TestProcessFilesMultipleWithContext(c *C) {
fname := makeGoSourceFile(c, []byte(`package main
func main() {
// TRANSLATORS: foo comment
i18n.GC("foo", "foo context")
// TRANSLATORS: bar comment
i18n.GC("foo", "foo context 2")
}
`))
err := processFiles([]string{fname})
c.Assert(err, IsNil)
c.Assert(msgIDs, DeepEquals, map[string][]msgID{
"foo": {
{
comment: "#. TRANSLATORS: foo comment\n",
context: "foo context",
fname: fname,
line: 5,
},
{
comment: "#. TRANSLATORS: bar comment\n",
context: "foo context 2",
fname: fname,
line: 8,
},
},
})
}
const header = `# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
@ -174,6 +230,31 @@ msgstr ""
c.Assert(out.String(), Equals, expected)
}
func (s *xgettextTestSuite) TestWriteOutputSimpleWithContext(c *C) {
msgIDs = map[string][]msgID{
"foo": {
{
fname: "fname",
line: 2,
comment: "#. foo\n",
context: "foo context",
},
},
}
out := bytes.NewBuffer([]byte(""))
writePotFile(out)
expected := fmt.Sprintf(`%s
#. foo
#: fname:2
msgctx "foo context"
msgid "foo"
msgstr ""
`, header)
c.Assert(out.String(), Equals, expected)
}
func (s *xgettextTestSuite) TestWriteOutputMultiple(c *C) {
msgIDs = map[string][]msgID{
"foo": {
@ -203,6 +284,43 @@ msgstr ""
c.Assert(out.String(), Equals, expected)
}
func (s *xgettextTestSuite) TestWriteOutputMultipleWithContext(c *C) {
msgIDs = map[string][]msgID{
"foo": {
{
fname: "fname",
context: "context1",
line: 2,
comment: "#. comment1\n",
},
{
fname: "fname",
context: "context2",
line: 4,
comment: "#. comment2\n",
},
},
}
out := bytes.NewBuffer([]byte(""))
writePotFile(out)
expected := fmt.Sprintf(`%s
#. comment1
#: fname:2
msgctx "context1"
msgid "foo"
msgstr ""
#. comment2
#: fname:4
msgctx "context2"
msgid "foo"
msgstr ""
`, header)
c.Assert(out.String(), Equals, expected)
}
func (s *xgettextTestSuite) TestWriteOutputNoComment(c *C) {
msgIDs = map[string][]msgID{
"foo": {