From 8cc992da8e523f3ffde2041e414369b59ebc991a Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Thu, 5 Feb 2015 00:08:46 +0800 Subject: [PATCH 1/4] FIxed `encodePoString`. --- gettext/po/util.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/gettext/po/util.go b/gettext/po/util.go index 5254483..2a1425c 100644 --- a/gettext/po/util.go +++ b/gettext/po/util.go @@ -48,6 +48,9 @@ func decodePoString(text string) string { func encodePoString(text string) string { var buf bytes.Buffer lines := strings.Split(text, "\n") + if len(lines) > 1 { + buf.WriteString(`""` + "\n") + } for i := 0; i < len(lines); i++ { if lines[i] == "" { if i != len(lines)-1 { @@ -70,7 +73,11 @@ func encodePoString(text string) string { buf.WriteRune(r) } } - buf.WriteString(`\n"` + "\n") + if i > 0 { + buf.WriteString(`\n"` + "\n") + } else { + buf.WriteString(`"` + "\n") + } } return buf.String() } From a04b346c7fd5aa9b8530ce4310bc1eec221372e4 Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Thu, 5 Feb 2015 00:25:09 +0800 Subject: [PATCH 2/4] Fixed one line. --- gettext/po/util.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/gettext/po/util.go b/gettext/po/util.go index 2a1425c..c3e9df5 100644 --- a/gettext/po/util.go +++ b/gettext/po/util.go @@ -48,9 +48,6 @@ func decodePoString(text string) string { func encodePoString(text string) string { var buf bytes.Buffer lines := strings.Split(text, "\n") - if len(lines) > 1 { - buf.WriteString(`""` + "\n") - } for i := 0; i < len(lines); i++ { if lines[i] == "" { if i != len(lines)-1 { @@ -73,7 +70,7 @@ func encodePoString(text string) string { buf.WriteRune(r) } } - if i > 0 { + if len(lines) > 1 { buf.WriteString(`\n"` + "\n") } else { buf.WriteString(`"` + "\n") From a19a52b17c8b2a84fdd1985b10c1a5b21f4109fd Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Thu, 5 Feb 2015 23:32:52 +0800 Subject: [PATCH 3/4] Add test sample. --- gettext/po/util_test.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/gettext/po/util_test.go b/gettext/po/util_test.go index 5e8331e..d5f29be 100644 --- a/gettext/po/util_test.go +++ b/gettext/po/util_test.go @@ -4,9 +4,7 @@ package po -import ( - "testing" -) +import "testing" func TestDecodePoString(t *testing.T) { if s := decodePoString(poStrEncode); s != poStrDecode { @@ -20,6 +18,16 @@ func TestEncodePoString(t *testing.T) { } } +func TestEncodePoStringMessageN(t *testing.T) { + if s := encodePoString(poStrDecodeMessageSingleLine); s != poStrEncodeMessageSingleLine { + t.Fatalf(`expect = %s; got = %s`, poStrEncodeMessageSingleLine, s) + } + + if multis := encodePoString(poStrDecodeMessageMultiLines); multis != poStrEncodeMessageMultiLines { + t.Fatalf(`expect = %s; got = %s`, poStrEncodeMessageMultiLines, multis) + } +} + const poStrEncode = `# noise 123456789 "Project-Id-Version: Poedit 1.5\n" @@ -66,3 +74,13 @@ Plural-Forms: nplurals=1; plural=0; X-Generator: Poedit 1.5.5 TestPoString: abc123 ` + +const poStrDecodeMessageSingleLine = `多國語系` + +const poStrEncodeMessageSingleLine = `"多國語系"` + "\n" + +const poStrDecodeMessageMultiLines = `多國語系 +是處理許多國家的翻譯` + +const poStrEncodeMessageMultiLines = `"多國語系\n"` + "\n" + + `"是處理許多國家的翻譯"` + "\n" From 5d9af791e6a59e719fbdb71285becc16ed9b5e76 Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Thu, 5 Feb 2015 23:54:31 +0800 Subject: [PATCH 4/4] Fixed the last line end no need string "\n". --- gettext/po/util.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gettext/po/util.go b/gettext/po/util.go index c3e9df5..12fcfb8 100644 --- a/gettext/po/util.go +++ b/gettext/po/util.go @@ -48,9 +48,10 @@ func decodePoString(text string) string { func encodePoString(text string) string { var buf bytes.Buffer lines := strings.Split(text, "\n") - for i := 0; i < len(lines); i++ { + lenLines := len(lines) + for i := 0; i < lenLines; i++ { if lines[i] == "" { - if i != len(lines)-1 { + if i != lenLines-1 { buf.WriteString(`"\n"` + "\n") } continue @@ -70,7 +71,7 @@ func encodePoString(text string) string { buf.WriteRune(r) } } - if len(lines) > 1 { + if lenLines > 1 && i != lenLines-1 { buf.WriteString(`\n"` + "\n") } else { buf.WriteString(`"` + "\n")