Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

單行內容不需要 \n 結尾 #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions gettext/po/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand Down
24 changes: 21 additions & 3 deletions gettext/po/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

package po

import (
"testing"
)
import "testing"

func TestDecodePoString(t *testing.T) {
if s := decodePoString(poStrEncode); s != poStrDecode {
Expand All @@ -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"
Expand Down Expand Up @@ -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"