2018-03-23 20:17:05 +00:00
|
|
|
/*
|
|
|
|
* 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 (
|
2023-03-16 09:53:22 +00:00
|
|
|
"io/fs"
|
2016-06-19 22:36:33 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-06-24 20:04:46 +00:00
|
|
|
/*
|
2018-03-23 20:17:05 +00:00
|
|
|
Po parses the content of any PO file and provides all the Translation functions needed.
|
2016-07-15 22:04:59 +00:00
|
|
|
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:
|
|
|
|
|
2017-09-01 16:28:51 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-22 06:33:57 +00:00
|
|
|
"code.rocket9labs.com/tslocum/gotext"
|
2017-09-01 16:28:51 +00:00
|
|
|
)
|
2016-06-24 20:04:46 +00:00
|
|
|
|
2017-05-29 09:36:18 +00:00
|
|
|
func main() {
|
|
|
|
// Create po object
|
2023-09-26 08:52:49 +00:00
|
|
|
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
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// Get Translation
|
2017-09-01 16:28:51 +00:00
|
|
|
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 {
|
2020-09-10 20:02:24 +00:00
|
|
|
//these three public members are for backwards compatibility. they are just set to the value in the domain
|
2020-09-26 18:16:19 +00:00
|
|
|
Headers HeaderMap
|
2020-09-10 20:02:24 +00:00
|
|
|
Language string
|
2016-07-15 22:04:59 +00:00
|
|
|
PluralForms string
|
|
|
|
|
2020-09-10 20:02:24 +00:00
|
|
|
domain *Domain
|
2023-03-16 09:53:22 +00:00
|
|
|
fs fs.FS
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 09:36:18 +00:00
|
|
|
type parseState int
|
|
|
|
|
|
|
|
const (
|
2018-03-26 18:47:41 +00:00
|
|
|
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
|
2020-09-10 20:02:24 +00:00
|
|
|
func NewPo() *Po {
|
|
|
|
po := new(Po)
|
|
|
|
po.domain = NewDomain()
|
|
|
|
|
|
|
|
return po
|
2018-03-23 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 09:53:22 +00:00
|
|
|
// NewPoFS works like NewPO but adds an optional fs.FS
|
|
|
|
func NewPoFS(filesystem fs.FS) *Po {
|
|
|
|
po := NewPo()
|
|
|
|
po.fs = filesystem
|
|
|
|
return po
|
|
|
|
}
|
|
|
|
|
2020-09-10 20:02:24 +00:00
|
|
|
func (po *Po) GetDomain() *Domain {
|
|
|
|
return po.domain
|
|
|
|
}
|
2016-06-19 22:36:33 +00:00
|
|
|
|
2020-09-26 18:16:19 +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)
|
|
|
|
}
|
2020-09-10 20:02:24 +00:00
|
|
|
func (po *Po) Get(str string, vars ...interface{}) string {
|
|
|
|
return po.domain.Get(str, vars...)
|
|
|
|
}
|
2016-06-19 22:36:33 +00:00
|
|
|
|
2020-09-26 18:16:19 +00:00
|
|
|
func (po *Po) SetN(id, plural string, n int, str string) {
|
|
|
|
po.domain.SetN(id, plural, n, str)
|
|
|
|
}
|
2020-09-10 20:02:24 +00:00
|
|
|
func (po *Po) GetN(str, plural string, n int, vars ...interface{}) string {
|
|
|
|
return po.domain.GetN(str, plural, n, vars...)
|
|
|
|
}
|
|
|
|
|
2020-09-26 18:16:19 +00:00
|
|
|
func (po *Po) SetC(id, ctx, str string) {
|
|
|
|
po.domain.SetC(id, ctx, str)
|
|
|
|
}
|
2020-09-10 20:02:24 +00:00
|
|
|
func (po *Po) GetC(str, ctx string, vars ...interface{}) string {
|
|
|
|
return po.domain.GetC(str, ctx, vars...)
|
|
|
|
}
|
|
|
|
|
2020-09-26 18:16:19 +00:00
|
|
|
func (po *Po) SetNC(id, plural, ctx string, n int, str string) {
|
|
|
|
po.domain.SetNC(id, plural, ctx, n, str)
|
|
|
|
}
|
2020-09-10 20:02:24 +00:00
|
|
|
func (po *Po) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
|
|
|
|
return po.domain.GetNC(str, plural, n, ctx, vars...)
|
|
|
|
}
|
|
|
|
|
2023-04-04 15:22:21 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-09-26 18:16:19 +00:00
|
|
|
func (po *Po) MarshalText() ([]byte, error) {
|
|
|
|
return po.domain.MarshalText()
|
|
|
|
}
|
|
|
|
|
2020-09-10 20:02:24 +00:00
|
|
|
func (po *Po) ParseFile(f string) {
|
2023-03-16 09:53:22 +00:00
|
|
|
data, err := getFileData(f, po.fs)
|
2016-06-19 22:36:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
po.Parse(data)
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 09:46:13 +00:00
|
|
|
// Parse loads the translations specified in the provided byte slice (buf)
|
2018-03-23 20:17:05 +00:00
|
|
|
func (po *Po) Parse(buf []byte) {
|
2020-09-10 20:02:24 +00:00
|
|
|
if po.domain == nil {
|
2020-09-26 18:16:19 +00:00
|
|
|
panic("NewPo() was not used to instantiate this object")
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 20:02:24 +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
|
2018-03-23 20:17:05 +00:00
|
|
|
lines := strings.Split(string(buf), "\n")
|
2016-06-19 22:36:33 +00:00
|
|
|
|
2016-08-08 14:36:05 +00:00
|
|
|
// Init buffer
|
2020-09-10 20:02:24 +00:00
|
|
|
po.domain.trBuffer = NewTranslation()
|
|
|
|
po.domain.ctxBuffer = ""
|
2020-09-26 18:16:19 +00:00
|
|
|
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) {
|
2020-09-26 18:16:19 +00:00
|
|
|
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)
|
2020-09-10 20:02:24 +00:00
|
|
|
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
|
|
|
|
2018-03-23 20:17:05 +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
|
2016-07-15 22:04:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multi line strings and headers
|
|
|
|
if strings.HasPrefix(l, "\"") && strings.HasSuffix(l, "\"") {
|
2017-09-08 21:08:56 +00:00
|
|
|
po.parseString(l, state)
|
2016-07-15 22:04:59 +00:00
|
|
|
continue
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-23 14:41:38 +00:00
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// Save last Translation buffer.
|
2016-08-08 14:36:05 +00:00
|
|
|
po.saveBuffer()
|
|
|
|
|
|
|
|
// Parse headers
|
2020-09-10 20:02:24 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-23 20:17: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() {
|
2017-09-08 21:08:56 +00:00
|
|
|
// With no context...
|
2020-09-10 20:02:24 +00:00
|
|
|
if po.domain.ctxBuffer == "" {
|
|
|
|
po.domain.translations[po.domain.trBuffer.ID] = po.domain.trBuffer
|
2017-09-08 21:08:56 +00:00
|
|
|
} else {
|
|
|
|
// With context...
|
2023-04-04 14:28:57 +00:00
|
|
|
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
|
|
|
}
|
2023-04-04 14:28:57 +00:00
|
|
|
po.domain.contextTranslations[po.domain.ctxBuffer][po.domain.trBuffer.ID] = po.domain.trBuffer
|
2016-08-08 14:36:05 +00:00
|
|
|
|
2017-09-08 21:08:56 +00:00
|
|
|
// Cleanup current context buffer if needed
|
2020-09-10 20:02:24 +00:00
|
|
|
if po.domain.trBuffer.ID != "" {
|
|
|
|
po.domain.ctxBuffer = ""
|
2017-09-08 21:08:56 +00:00
|
|
|
}
|
2016-06-23 14:41:38 +00:00
|
|
|
}
|
2017-09-08 21:08:56 +00:00
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// Flush Translation buffer
|
2020-09-26 18:16:19 +00:00
|
|
|
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-07-15 22:04:59 +00:00
|
|
|
|
2016-08-08 14:36:05 +00:00
|
|
|
// parseContext takes a line starting with "msgctxt",
|
2018-03-23 20:17:05 +00:00
|
|
|
// saves the current Translation buffer and creates a new context.
|
2016-08-08 14:36:05 +00:00
|
|
|
func (po *Po) parseContext(l string) {
|
2018-03-23 20:17:05 +00:00
|
|
|
// Save current Translation buffer.
|
2016-08-08 14:36:05 +00:00
|
|
|
po.saveBuffer()
|
|
|
|
|
|
|
|
// Buffer context
|
2020-09-10 20:02:24 +00:00
|
|
|
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",
|
2018-03-23 20:17:05 +00:00
|
|
|
// saves the current Translation and creates a new msgid buffer.
|
2016-08-08 14:36:05 +00:00
|
|
|
func (po *Po) parseID(l string) {
|
2018-03-23 20:17:05 +00:00
|
|
|
// Save current Translation buffer.
|
2016-08-08 14:36:05 +00:00
|
|
|
po.saveBuffer()
|
|
|
|
|
|
|
|
// Set id
|
2020-09-10 20:02:24 +00:00
|
|
|
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) {
|
2020-09-10 20:02:24 +00:00
|
|
|
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"))
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// Parse Translation string
|
2020-09-10 20:02:24 +00:00
|
|
|
po.domain.trBuffer.Trs[i], _ = strconv.Unquote(strings.TrimSpace(l[idx+1:]))
|
2016-08-08 14:36:05 +00:00
|
|
|
|
|
|
|
// Loop
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// Save single Translation form under 0 index
|
2020-09-10 20:02:24 +00:00
|
|
|
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
|
2017-09-08 21:08:56 +00:00
|
|
|
func (po *Po) parseString(l string, state parseState) {
|
|
|
|
clean, _ := strconv.Unquote(l)
|
|
|
|
|
2017-05-29 09:36:18 +00:00
|
|
|
switch state {
|
|
|
|
case msgStr:
|
2018-03-23 20:17:05 +00:00
|
|
|
// Append to last Translation found
|
2020-09-10 20:02:24 +00:00
|
|
|
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
|
2020-09-10 20:02:24 +00:00
|
|
|
po.domain.trBuffer.ID += clean
|
2017-09-08 21:08:56 +00:00
|
|
|
|
2017-05-29 09:36:18 +00:00
|
|
|
case msgIDPlural:
|
|
|
|
// Multiline msgid - Append to current id
|
2020-09-10 20:02:24 +00:00
|
|
|
po.domain.trBuffer.PluralID += clean
|
2017-09-08 21:08:56 +00:00
|
|
|
|
2017-05-29 09:36:18 +00:00
|
|
|
case msgCtxt:
|
|
|
|
// Multiline context - Append to current context
|
2020-09-10 20:02:24 +00:00
|
|
|
po.domain.ctxBuffer += clean
|
2016-08-08 14:36:05 +00:00
|
|
|
|
2017-09-08 21:08:56 +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 {
|
2017-09-08 21:08:56 +00:00
|
|
|
// Check prefix
|
|
|
|
valid := []string{
|
|
|
|
"\"",
|
|
|
|
"msgctxt",
|
|
|
|
"msgid",
|
|
|
|
"msgid_plural",
|
|
|
|
"msgstr",
|
2016-08-08 14:36:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 21:08:56 +00:00
|
|
|
for _, v := range valid {
|
|
|
|
if strings.HasPrefix(l, v) {
|
|
|
|
return true
|
|
|
|
}
|
2016-08-08 14:36:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 21:08:56 +00:00
|
|
|
return false
|
2016-08-08 14:36:05 +00:00
|
|
|
}
|