diff --git a/gettext/po/util.go b/gettext/po/util.go index 5254483..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,11 @@ func encodePoString(text string) string { buf.WriteRune(r) } } - buf.WriteString(`\n"` + "\n") + if lenLines > 1 && i != lenLines-1 { + buf.WriteString(`\n"` + "\n") + } else { + buf.WriteString(`"` + "\n") + } } return buf.String() } 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"