[Golang] Convert Traditional Chinese PO file to Simplified Chinese
Automatically convert Traditional Chinese (zh_TW) PO file to Simplified Chinese (zh_CN) by OpenCC and OpenCC Go binding.
Install Necessary Tool
$ sudo apt-get install opencc libopencc-dev
$ go get -u github.com/siongui/go-opencc
Source Code
package main
import (
"bufio"
"fmt"
"github.com/siongui/go-opencc"
"os"
"path/filepath"
"strings"
)
var ct2s = opencc.NewConverter("zht2zhs.ini")
func File2lines(filePath string) []string {
f, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer f.Close()
var lines []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
panic(err)
}
return lines
}
func main() {
twPOPath := "locale/zh_TW/LC_MESSAGES/messages.po"
cnPOPath := "locale/zh_CN/LC_MESSAGES/messages.po"
defer ct2s.Close()
os.MkdirAll(filepath.Dir(cnPOPath), 0755)
fo, err := os.Create(cnPOPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
panic(err)
}
defer fo.Close()
for _, line := range File2lines(twPOPath) {
if strings.HasPrefix(line, "msgstr") {
fo.Write([]byte(ct2s.Convert(line)))
} else {
if strings.Contains(line, "zh_TW") {
fo.Write([]byte(strings.Replace(line, "zh_TW", "zh_CN", 1)))
} else {
fo.Write([]byte(line))
}
}
fo.Write([]byte("\n"))
}
}
Tested on: Ubuntu Linux 15.10, Go 1.6, opencc 0.4.3-2build1, GitHub: siongui/go-opencc.
References:
[1] | [Python] Automatically Convert Traditional Chinese PO file to Simplified Chinese |
[2] | create zh_CN PO from zh_TW · siongui/pali@365d46c · GitHub |