gotext/po.go

365 lines
8.5 KiB
Go
Raw Permalink Normal View History

/*
* Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/
2016-06-19 22:36:33 +00:00
package gotext
import (
"io/fs"
2016-06-19 22:36:33 +00:00
"strconv"
"strings"
)
2016-06-24 20:04:46 +00:00
/*
Po parses the content of any PO file and provides all the Translation functions needed.
It's the base object used by all package methods.
And it's safe for concurrent use by multiple goroutines by using the sync package for locking.
2016-06-24 20:04:46 +00:00
Example:
import (
"fmt"
2024-07-22 06:33:57 +00:00
"code.rocket9labs.com/tslocum/gotext"
)
2016-06-24 20:04:46 +00:00
2017-05-29 09:36:18 +00:00
func main() {
// Create po object
po := gotext.NewPo()
2016-06-24 20:04:46 +00:00
2017-05-29 09:36:18 +00:00
// Parse .po file
po.ParseFile("/path/to/po/file/translations.po")
2016-06-24 20:04:46 +00:00
// Get Translation
fmt.Println(po.Get("Translate this"))
2017-05-29 09:36:18 +00:00
}
2016-06-24 20:04:46 +00:00
*/
2016-06-19 22:36:33 +00:00
type Po struct {
//these three public members are for backwards compatibility. they are just set to the value in the domain
Headers HeaderMap
Language string
PluralForms string
domain *Domain
fs fs.FS
2016-06-19 22:36:33 +00:00
}
2017-05-29 09:36:18 +00:00
type parseState int
const (
head parseState = iota
2017-05-29 09:36:18 +00:00
msgCtxt
msgID
msgIDPlural
msgStr
)
2024-07-22 06:30:26 +00:00
// NewPo should always be used to instantiate a new Po object
func NewPo() *Po {
po := new(Po)
po.domain = NewDomain()
return po
}
// NewPoFS works like NewPO but adds an optional fs.FS
func NewPoFS(filesystem fs.FS) *Po {
po := NewPo()
po.fs = filesystem
return po
}
func (po *Po) GetDomain() *Domain {
return po.domain
}
2016-06-19 22:36:33 +00:00
// Convenience interfaces
func (po *Po) DropStaleTranslations() {
po.domain.DropStaleTranslations()
}
func (po *Po) SetRefs(str string, refs []string) {
po.domain.SetRefs(str, refs)
}
func (po *Po) GetRefs(str string) []string {
return po.domain.GetRefs(str)
}
func (po *Po) Set(id, str string) {
po.domain.Set(id, str)
}
func (po *Po) Get(str string, vars ...interface{}) string {
return po.domain.Get(str, vars...)
}
2016-06-19 22:36:33 +00:00
func (po *Po) SetN(id, plural string, n int, str string) {
po.domain.SetN(id, plural, n, str)
}
func (po *Po) GetN(str, plural string, n int, vars ...interface{}) string {
return po.domain.GetN(str, plural, n, vars...)
}
func (po *Po) SetC(id, ctx, str string) {
po.domain.SetC(id, ctx, str)
}
func (po *Po) GetC(str, ctx string, vars ...interface{}) string {
return po.domain.GetC(str, ctx, vars...)
}
func (po *Po) SetNC(id, plural, ctx string, n int, str string) {
po.domain.SetNC(id, plural, ctx, n, str)
}
func (po *Po) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
return po.domain.GetNC(str, plural, n, ctx, vars...)
}
func (po *Po) IsTranslated(str string) bool {
return po.domain.IsTranslated(str)
}
func (po *Po) IsTranslatedN(str string, n int) bool {
return po.domain.IsTranslatedN(str, n)
}
func (po *Po) IsTranslatedC(str, ctx string) bool {
return po.domain.IsTranslatedC(str, ctx)
}
func (po *Po) IsTranslatedNC(str string, n int, ctx string) bool {
return po.domain.IsTranslatedNC(str, n, ctx)
}
func (po *Po) MarshalText() ([]byte, error) {
return po.domain.MarshalText()
}
func (po *Po) ParseFile(f string) {
data, err := getFileData(f, po.fs)
2016-06-19 22:36:33 +00:00
if err != nil {
return
}
po.Parse(data)
2016-06-19 22:36:33 +00:00
}
// Parse loads the translations specified in the provided byte slice (buf)
func (po *Po) Parse(buf []byte) {
if po.domain == nil {
panic("NewPo() was not used to instantiate this object")
2016-06-19 22:36:33 +00:00
}
// Lock while parsing
po.domain.trMutex.Lock()
po.domain.pluralMutex.Lock()
defer po.domain.trMutex.Unlock()
defer po.domain.pluralMutex.Unlock()
2016-06-26 18:43:54 +00:00
// Get lines
lines := strings.Split(string(buf), "\n")
2016-06-19 22:36:33 +00:00
2016-08-08 14:36:05 +00:00
// Init buffer
po.domain.trBuffer = NewTranslation()
po.domain.ctxBuffer = ""
po.domain.refBuffer = ""
2016-06-26 18:43:54 +00:00
2017-05-29 09:36:18 +00:00
state := head
2016-06-19 22:36:33 +00:00
for _, l := range lines {
// Trim spaces
l = strings.TrimSpace(l)
// Skip invalid lines
2016-08-08 14:36:05 +00:00
if !po.isValidLine(l) {
po.parseComment(l, state)
2016-06-19 22:36:33 +00:00
continue
}
2016-06-26 18:43:54 +00:00
// Buffer context and continue
if strings.HasPrefix(l, "msgctxt") {
2016-08-08 14:36:05 +00:00
po.parseContext(l)
2017-05-29 09:36:18 +00:00
state = msgCtxt
2016-06-26 18:43:54 +00:00
continue
}
// Buffer msgid and continue
if strings.HasPrefix(l, "msgid") && !strings.HasPrefix(l, "msgid_plural") {
2016-08-08 14:36:05 +00:00
po.parseID(l)
2017-05-29 09:36:18 +00:00
state = msgID
2016-06-19 22:36:33 +00:00
continue
}
2016-06-23 14:41:38 +00:00
// Check for plural form
if strings.HasPrefix(l, "msgid_plural") {
2016-08-08 14:36:05 +00:00
po.parsePluralID(l)
po.domain.pluralTranslations[po.domain.trBuffer.PluralID] = po.domain.trBuffer
2017-05-29 09:36:18 +00:00
state = msgIDPlural
2016-06-23 14:41:38 +00:00
continue
}
2016-06-19 22:36:33 +00:00
// Save Translation
2016-06-23 14:41:38 +00:00
if strings.HasPrefix(l, "msgstr") {
2016-08-08 14:36:05 +00:00
po.parseMessage(l)
2017-05-29 09:36:18 +00:00
state = msgStr
continue
}
// Multi line strings and headers
if strings.HasPrefix(l, "\"") && strings.HasSuffix(l, "\"") {
po.parseString(l, state)
continue
2016-06-19 22:36:33 +00:00
}
}
2016-06-23 14:41:38 +00:00
// Save last Translation buffer.
2016-08-08 14:36:05 +00:00
po.saveBuffer()
// Parse headers
po.domain.parseHeaders()
// set values on this struct
// this is for backwards compatibility
po.Language = po.domain.Language
po.PluralForms = po.domain.PluralForms
po.Headers = po.domain.Headers
2016-08-08 14:36:05 +00:00
}
// saveBuffer takes the context and Translation buffers
2016-08-08 14:36:05 +00:00
// and saves it on the translations collection
func (po *Po) saveBuffer() {
// With no context...
if po.domain.ctxBuffer == "" {
po.domain.translations[po.domain.trBuffer.ID] = po.domain.trBuffer
} else {
// With context...
if _, ok := po.domain.contextTranslations[po.domain.ctxBuffer]; !ok {
po.domain.contextTranslations[po.domain.ctxBuffer] = make(map[string]*Translation)
2016-06-26 18:43:54 +00:00
}
po.domain.contextTranslations[po.domain.ctxBuffer][po.domain.trBuffer.ID] = po.domain.trBuffer
2016-08-08 14:36:05 +00:00
// Cleanup current context buffer if needed
if po.domain.trBuffer.ID != "" {
po.domain.ctxBuffer = ""
}
2016-06-23 14:41:38 +00:00
}
// Flush Translation buffer
if po.domain.refBuffer == "" {
po.domain.trBuffer = NewTranslation()
} else {
po.domain.trBuffer = NewTranslationWithRefs(strings.Split(po.domain.refBuffer, " "))
}
}
// Either preserves comments before the first "msgid", for later round-trip.
// Or preserves source references for a given translation.
func (po *Po) parseComment(l string, state parseState) {
if len(l) > 0 && l[0] == '#' {
if state == head {
po.domain.headerComments = append(po.domain.headerComments, l)
} else if len(l) > 1 {
switch l[1] {
case ':':
if len(l) > 2 {
po.domain.refBuffer = strings.TrimSpace(l[2:])
}
}
}
}
2016-08-08 14:36:05 +00:00
}
2016-08-08 14:36:05 +00:00
// parseContext takes a line starting with "msgctxt",
// saves the current Translation buffer and creates a new context.
2016-08-08 14:36:05 +00:00
func (po *Po) parseContext(l string) {
// Save current Translation buffer.
2016-08-08 14:36:05 +00:00
po.saveBuffer()
// Buffer context
po.domain.ctxBuffer, _ = strconv.Unquote(strings.TrimSpace(strings.TrimPrefix(l, "msgctxt")))
2016-08-08 14:36:05 +00:00
}
// parseID takes a line starting with "msgid",
// saves the current Translation and creates a new msgid buffer.
2016-08-08 14:36:05 +00:00
func (po *Po) parseID(l string) {
// Save current Translation buffer.
2016-08-08 14:36:05 +00:00
po.saveBuffer()
// Set id
po.domain.trBuffer.ID, _ = strconv.Unquote(strings.TrimSpace(strings.TrimPrefix(l, "msgid")))
2016-08-08 14:36:05 +00:00
}
// parsePluralID saves the plural id buffer from a line starting with "msgid_plural"
func (po *Po) parsePluralID(l string) {
po.domain.trBuffer.PluralID, _ = strconv.Unquote(strings.TrimSpace(strings.TrimPrefix(l, "msgid_plural")))
2016-08-08 14:36:05 +00:00
}
// parseMessage takes a line starting with "msgstr" and saves it into the current buffer.
func (po *Po) parseMessage(l string) {
l = strings.TrimSpace(strings.TrimPrefix(l, "msgstr"))
// Check for indexed Translation forms
2016-08-08 14:36:05 +00:00
if strings.HasPrefix(l, "[") {
idx := strings.Index(l, "]")
if idx == -1 {
// Skip wrong index formatting
return
}
// Parse index
i, err := strconv.Atoi(l[1:idx])
if err != nil {
// Skip wrong index formatting
return
}
// Parse Translation string
po.domain.trBuffer.Trs[i], _ = strconv.Unquote(strings.TrimSpace(l[idx+1:]))
2016-08-08 14:36:05 +00:00
// Loop
return
}
// Save single Translation form under 0 index
po.domain.trBuffer.Trs[0], _ = strconv.Unquote(l)
2016-08-08 14:36:05 +00:00
}
// parseString takes a well formatted string without prefix
// and creates headers or attach multi-line strings when corresponding
func (po *Po) parseString(l string, state parseState) {
clean, _ := strconv.Unquote(l)
2017-05-29 09:36:18 +00:00
switch state {
case msgStr:
// Append to last Translation found
po.domain.trBuffer.Trs[len(po.domain.trBuffer.Trs)-1] += clean
2016-08-08 14:36:05 +00:00
2017-05-29 09:36:18 +00:00
case msgID:
// Multiline msgid - Append to current id
po.domain.trBuffer.ID += clean
2017-05-29 09:36:18 +00:00
case msgIDPlural:
// Multiline msgid - Append to current id
po.domain.trBuffer.PluralID += clean
2017-05-29 09:36:18 +00:00
case msgCtxt:
// Multiline context - Append to current context
po.domain.ctxBuffer += clean
2016-08-08 14:36:05 +00:00
}
2016-08-08 14:36:05 +00:00
}
// isValidLine checks for line prefixes to detect valid syntax.
func (po *Po) isValidLine(l string) bool {
// Check prefix
valid := []string{
"\"",
"msgctxt",
"msgid",
"msgid_plural",
"msgstr",
2016-08-08 14:36:05 +00:00
}
for _, v := range valid {
if strings.HasPrefix(l, v) {
return true
}
2016-08-08 14:36:05 +00:00
}
return false
2016-08-08 14:36:05 +00:00
}