Merge branch 'fishbrain-master'
This commit is contained in:
@@ -32,8 +32,8 @@ func TestGettersSetters(t *testing.T) {
|
|||||||
func TestPackageFunctions(t *testing.T) {
|
func TestPackageFunctions(t *testing.T) {
|
||||||
// Set PO content
|
// Set PO content
|
||||||
str := `
|
str := `
|
||||||
msgid ""
|
# msgid ""
|
||||||
msgstr ""
|
# msgstr ""
|
||||||
# Initial comment
|
# Initial comment
|
||||||
# Headers below
|
# Headers below
|
||||||
"Language: en\n"
|
"Language: en\n"
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import (
|
|||||||
func TestLocale(t *testing.T) {
|
func TestLocale(t *testing.T) {
|
||||||
// Set PO content
|
// Set PO content
|
||||||
str := `
|
str := `
|
||||||
msgid ""
|
# msgid ""
|
||||||
msgstr ""
|
# msgstr ""
|
||||||
# Initial comment
|
# Initial comment
|
||||||
# Headers below
|
# Headers below
|
||||||
"Language: en\n"
|
"Language: en\n"
|
||||||
|
|||||||
44
po.go
44
po.go
@@ -92,6 +92,16 @@ type Po struct {
|
|||||||
ctxBuffer string
|
ctxBuffer string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type parseState int
|
||||||
|
|
||||||
|
const (
|
||||||
|
head parseState = iota
|
||||||
|
msgCtxt
|
||||||
|
msgID
|
||||||
|
msgIDPlural
|
||||||
|
msgStr
|
||||||
|
)
|
||||||
|
|
||||||
// ParseFile tries to read the file by its provided path (f) and parse its content as a .po file.
|
// ParseFile tries to read the file by its provided path (f) and parse its content as a .po file.
|
||||||
func (po *Po) ParseFile(f string) {
|
func (po *Po) ParseFile(f string) {
|
||||||
// Check if file exists
|
// Check if file exists
|
||||||
@@ -133,6 +143,7 @@ func (po *Po) Parse(str string) {
|
|||||||
po.trBuffer = newTranslation()
|
po.trBuffer = newTranslation()
|
||||||
po.ctxBuffer = ""
|
po.ctxBuffer = ""
|
||||||
|
|
||||||
|
state := head
|
||||||
for _, l := range lines {
|
for _, l := range lines {
|
||||||
// Trim spaces
|
// Trim spaces
|
||||||
l = strings.TrimSpace(l)
|
l = strings.TrimSpace(l)
|
||||||
@@ -145,30 +156,34 @@ func (po *Po) Parse(str string) {
|
|||||||
// Buffer context and continue
|
// Buffer context and continue
|
||||||
if strings.HasPrefix(l, "msgctxt") {
|
if strings.HasPrefix(l, "msgctxt") {
|
||||||
po.parseContext(l)
|
po.parseContext(l)
|
||||||
|
state = msgCtxt
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buffer msgid and continue
|
// Buffer msgid and continue
|
||||||
if strings.HasPrefix(l, "msgid") && !strings.HasPrefix(l, "msgid_plural") {
|
if strings.HasPrefix(l, "msgid") && !strings.HasPrefix(l, "msgid_plural") {
|
||||||
po.parseID(l)
|
po.parseID(l)
|
||||||
|
state = msgID
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for plural form
|
// Check for plural form
|
||||||
if strings.HasPrefix(l, "msgid_plural") {
|
if strings.HasPrefix(l, "msgid_plural") {
|
||||||
po.parsePluralID(l)
|
po.parsePluralID(l)
|
||||||
|
state = msgIDPlural
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save translation
|
// Save translation
|
||||||
if strings.HasPrefix(l, "msgstr") {
|
if strings.HasPrefix(l, "msgstr") {
|
||||||
po.parseMessage(l)
|
po.parseMessage(l)
|
||||||
|
state = msgStr
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multi line strings and headers
|
// Multi line strings and headers
|
||||||
if strings.HasPrefix(l, "\"") && strings.HasSuffix(l, "\"") {
|
if strings.HasPrefix(l, "\"") && strings.HasSuffix(l, "\"") {
|
||||||
po.parseString(l)
|
state = po.parseString(l, state)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -259,23 +274,36 @@ func (po *Po) parseMessage(l string) {
|
|||||||
|
|
||||||
// parseString takes a well formatted string without prefix
|
// parseString takes a well formatted string without prefix
|
||||||
// and creates headers or attach multi-line strings when corresponding
|
// and creates headers or attach multi-line strings when corresponding
|
||||||
func (po *Po) parseString(l string) {
|
func (po *Po) parseString(l string, state parseState) parseState {
|
||||||
|
switch state {
|
||||||
|
case msgStr:
|
||||||
// Check for multiline from previously set msgid
|
// Check for multiline from previously set msgid
|
||||||
if po.trBuffer.id != "" {
|
if po.trBuffer.id != "" {
|
||||||
// Append to last translation found
|
// Append to last translation found
|
||||||
uq, _ := strconv.Unquote(l)
|
uq, _ := strconv.Unquote(l)
|
||||||
po.trBuffer.trs[len(po.trBuffer.trs)-1] += uq
|
po.trBuffer.trs[len(po.trBuffer.trs)-1] += uq
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
case msgID:
|
||||||
|
// Multiline msgid - Append to current id
|
||||||
|
uq, _ := strconv.Unquote(l)
|
||||||
|
po.trBuffer.id += uq
|
||||||
|
case msgIDPlural:
|
||||||
|
// Multiline msgid - Append to current id
|
||||||
|
uq, _ := strconv.Unquote(l)
|
||||||
|
po.trBuffer.pluralID += uq
|
||||||
|
case msgCtxt:
|
||||||
|
// Multiline context - Append to current context
|
||||||
|
ctxt, _ := strconv.Unquote(l)
|
||||||
|
po.ctxBuffer += ctxt
|
||||||
|
default:
|
||||||
// Otherwise is a header
|
// Otherwise is a header
|
||||||
h, err := strconv.Unquote(strings.TrimSpace(l))
|
h, _ := strconv.Unquote(strings.TrimSpace(l))
|
||||||
if err != nil {
|
po.RawHeaders += h
|
||||||
return
|
return head
|
||||||
}
|
}
|
||||||
|
|
||||||
po.RawHeaders += h
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
// isValidLine checks for line prefixes to detect valid syntax.
|
// isValidLine checks for line prefixes to detect valid syntax.
|
||||||
|
|||||||
30
po_test.go
30
po_test.go
@@ -9,8 +9,6 @@ import (
|
|||||||
func TestPo(t *testing.T) {
|
func TestPo(t *testing.T) {
|
||||||
// Set PO content
|
// Set PO content
|
||||||
str := `
|
str := `
|
||||||
msgid ""
|
|
||||||
msgstr ""
|
|
||||||
# Initial comment
|
# Initial comment
|
||||||
# Headers below
|
# Headers below
|
||||||
"Language: en\n"
|
"Language: en\n"
|
||||||
@@ -26,6 +24,19 @@ msgstr "Translated text"
|
|||||||
msgid "Another string"
|
msgid "Another string"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
# Multi-line msgid
|
||||||
|
msgid "multi"
|
||||||
|
"line"
|
||||||
|
"id"
|
||||||
|
msgstr "id with multiline content"
|
||||||
|
|
||||||
|
# Multi-line msgid_plural
|
||||||
|
msgid "multi"
|
||||||
|
"line"
|
||||||
|
"plural"
|
||||||
|
"id"
|
||||||
|
msgstr "plural id with multiline content"
|
||||||
|
|
||||||
#Multi-line string
|
#Multi-line string
|
||||||
msgid "Multi-line"
|
msgid "Multi-line"
|
||||||
msgstr "Multi "
|
msgstr "Multi "
|
||||||
@@ -97,6 +108,18 @@ msgstr "More translation"
|
|||||||
t.Errorf("Expected 'This one is the singular: Variable' but got '%s'", tr)
|
t.Errorf("Expected 'This one is the singular: Variable' but got '%s'", tr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test multi-line id
|
||||||
|
tr = po.Get("multilineid")
|
||||||
|
if tr != "id with multiline content" {
|
||||||
|
t.Errorf("Expected 'id with multiline content' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test multi-line plural id
|
||||||
|
tr = po.Get("multilinepluralid")
|
||||||
|
if tr != "plural id with multiline content" {
|
||||||
|
t.Errorf("Expected 'plural id with multiline content' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
// Test multi-line
|
// Test multi-line
|
||||||
tr = po.Get("Multi-line")
|
tr = po.Get("Multi-line")
|
||||||
if tr != "Multi line" {
|
if tr != "Multi line" {
|
||||||
@@ -149,14 +172,11 @@ msgstr "More translation"
|
|||||||
if tr != "More translation" {
|
if tr != "More translation" {
|
||||||
t.Errorf("Expected 'More translation' but got '%s'", tr)
|
t.Errorf("Expected 'More translation' but got '%s'", tr)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPoHeaders(t *testing.T) {
|
func TestPoHeaders(t *testing.T) {
|
||||||
// Set PO content
|
// Set PO content
|
||||||
str := `
|
str := `
|
||||||
msgid ""
|
|
||||||
msgstr ""
|
|
||||||
# Initial comment
|
# Initial comment
|
||||||
# Headers below
|
# Headers below
|
||||||
"Language: en\n"
|
"Language: en\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user