Skip to content

Commit

Permalink
sort entry
Browse files Browse the repository at this point in the history
  • Loading branch information
youthlin committed Jan 5, 2024
1 parent b7930c5 commit 5fc4bc1
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 2 deletions.
64 changes: 64 additions & 0 deletions testdata/messages.new.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-01-05 16:04:11+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
"X-Created-By: xtpl(https://github.com/pub-go/tpl/tree/main/cmd/xtpl)\n"
"X-xTpl-Path: .\n"
"X-xTpl-Pattern: .*\\.html\n"
"X-xTpl-Keywords: T;N:1,2;N64:1,2;X:1c,2;XN:1c,2,3;XN64:1c,2,3;__;_n:1,2;_x:1c,2;_xn:1c,2,3\n"
"X-xTpl-Output: testdata/messages.pot\n"

#: testdata/index.html:6:24
msgid "Title"
msgstr ""

#: testdata/index.html:9:21
msgid "How are you?"
msgstr ""

#: testdata/index.html:11:23
msgid "And you?"
msgstr ""

#: testdata/index.html:12:20
msgid "Hello, %s"
msgstr ""

#: testdata/index.html:13:32
msgctxt "post new"
msgid "Post"
msgstr ""

#: testdata/index.html:14:29
msgctxt "posts"
msgid "Post"
msgstr ""

#: testdata/index.html:15:20
msgid "One Apple"
msgid_plural "%d Apples"
msgstr[0] ""
msgstr[1] ""

#: testdata/index.html:16:28
msgctxt "ctx"
msgid "One Book"
msgid_plural "%d Books"
msgstr[0] ""
msgstr[1] ""

64 changes: 64 additions & 0 deletions testdata/messages.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-01-05 16:04:11+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
"X-Created-By: xtpl(https://github.com/pub-go/tpl/tree/main/cmd/xtpl)\n"
"X-xTpl-Path: .\n"
"X-xTpl-Pattern: .*\\.html\n"
"X-xTpl-Keywords: T;N:1,2;N64:1,2;X:1c,2;XN:1c,2,3;XN64:1c,2,3;__;_n:1,2;_x:1c,2;_xn:1c,2,3\n"
"X-xTpl-Output: testdata/messages.pot\n"

#: testdata/index.html:11:23
msgid "And you?"
msgstr ""

#: testdata/index.html:12:20
msgid "Hello, %s"
msgstr ""

#: testdata/index.html:9:21
msgid "How are you?"
msgstr ""

#: testdata/index.html:15:20
msgid "One Apple"
msgid_plural "%d Apples"
msgstr[0] ""
msgstr[1] ""

#: testdata/index.html:6:24
msgid "Title"
msgstr ""

#: testdata/index.html:16:28
msgctxt "ctx"
msgid "One Book"
msgid_plural "%d Books"
msgstr[0] ""
msgstr[1] ""

#: testdata/index.html:13:32
msgctxt "post new"
msgid "Post"
msgstr ""

#: testdata/index.html:14:29
msgctxt "posts"
msgid "Post"
msgstr ""

42 changes: 42 additions & 0 deletions translator/entry.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package translator

import (
"fmt"
"strconv"
"strings"
)

// Entry 一个翻译条目
type Entry struct {
MsgCmts []string
Expand All @@ -25,3 +31,39 @@ func (e *Entry) isValid() bool {
// header entry: msgid == ""
return e != nil && (e.MsgStr != "" || len(e.MsgStrN) > 0)
}

// getSortKey 排序
func (e *Entry) getSortKey() string {
if e.isHeader() {
return "" // 排在最前
}
return e.getLineString() + e.Key()
}

// getLineString 如果有行号注释按行号排序 非行号的注释不使用
func (e *Entry) getLineString() string {
var ss []string
for _, cmt := range e.MsgCmts {
// #: testdata/index.html:16:28 testdata/index.html:18
if strings.HasPrefix(cmt, "#:") { // 行号注释前缀
cmt = strings.TrimPrefix(cmt, "#:")
var s []string
s = append(s, "#:")
for _, item := range strings.Split(cmt, " ") { // 多个位置
pair := strings.Split(item, ":") // 文件名:行号[:列号]
result := make([]string, 0, len(pair))
for _, n := range pair {
i, err := strconv.ParseInt(n, 10, 64)
if err != nil {
result = append(result, n)
} else { // 数字添加前导 0,这样字符串比较时 012<123
result = append(result, fmt.Sprintf("%10d", i))
}
}
s = append(s, result...)
}
ss = append(ss, s...)
}
}
return strings.Join(ss, "")
}
2 changes: 1 addition & 1 deletion translator/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (file *File) SortedEntry() (entries []*Entry) {
sort.Slice(entries, func(i, j int) bool {
left := entries[i]
right := entries[j]
return left.Key() < right.Key()
return left.getSortKey() < right.getSortKey()
})
return
}
Expand Down
20 changes: 20 additions & 0 deletions translator/file_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package translator

import (
"os"
"testing"
)

Expand Down Expand Up @@ -231,3 +232,22 @@ func TestFile_N(t *testing.T) {
})
}
}

func TestSortedEntry(t *testing.T) {
content, err := os.ReadFile("../testdata/messages.pot")
if err != nil {
t.Fatalf("read file failed: %v", err)
}
f, err := ReadPot(content)
if err != nil {
t.Fatalf("read .po failed: %v", err)
}
w, err := os.OpenFile("../testdata/messages.new.pot", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0777)
if err != nil {
t.Fatalf("open save file failed: %v", err)
}
err = f.SaveAsPot(w)
if err != nil {
t.Fatalf("save file failed: %v", err)
}
}
10 changes: 9 additions & 1 deletion translator/po.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ var errEmptyPo = fmt.Errorf("empty po file")

// ReadPo read po file
func ReadPo(content []byte) (*File, error) {
return readPo(content, false)
}

func ReadPot(content []byte) (*File, error) {
return readPo(content, true)
}

func readPo(content []byte, pot bool) (*File, error) {
if len(content) == 0 {
return nil, errors.Wrapf(errEmptyPo, "read po file failed")
}
Expand All @@ -25,7 +33,7 @@ func ReadPo(content []byte) (*File, error) {
file := new(File)
for {
entry, err := readEntry(r)
if entry.isValid() {
if pot || entry.isValid() {
file.AddEntry(entry)
}
if errors.Is(err, io.EOF) {
Expand Down

0 comments on commit 5fc4bc1

Please sign in to comment.