Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea87d40cc2 | ||
|
|
21c6bc86cb | ||
|
|
2c5ca9c0e6 | ||
|
|
5f34149d25 | ||
|
|
50cf0f07b7 | ||
|
|
d6f4cbb2d5 | ||
|
|
6e728a3df5 |
19
README.md
19
README.md
@@ -4,19 +4,20 @@
|
|||||||
|
|
||||||
GNU gettext utilities for Go.
|
GNU gettext utilities for Go.
|
||||||
|
|
||||||
**Version: 0.9.0**
|
Version: [0.9.1](https://github.com/leonelquinteros/gotext/releases/tag/v0.9.1)
|
||||||
|
|
||||||
|
|
||||||
#Features
|
#Features
|
||||||
|
|
||||||
- Implements GNU gettext support in native Go.
|
- Implements GNU gettext support in native Go.
|
||||||
- Safe for concurrent use accross multiple goroutines.
|
- Safe for concurrent use across multiple goroutines.
|
||||||
- It works with UTF-8 encoding as it's the default for Go language.
|
- It works with UTF-8 encoding as it's the default for Go language.
|
||||||
- Unit tests available
|
- Unit tests available.
|
||||||
- Language codes are automatically simplified from the form "en_UK" to "en" if the formed isn't available.
|
- Language codes are automatically simplified from the form "en_UK" to "en" if the first isn't available.
|
||||||
- Ready to use inside Go templates.
|
- Ready to use inside Go templates.
|
||||||
- Support for pluralization rules.
|
- Support for [pluralization rules](https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html).
|
||||||
- Support for variables inside translation strings using Go's [fmt package syntax](https://golang.org/pkg/fmt/)
|
- Support for [message context](https://www.gnu.org/software/gettext/manual/html_node/Contexts.html).
|
||||||
|
- Support for variables inside translation strings using Go's [fmt package syntax](https://golang.org/pkg/fmt/).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -43,7 +44,7 @@ Refer to the Godoc package documentation at (https://godoc.org/github.com/leonel
|
|||||||
|
|
||||||
# Locales directories structure
|
# Locales directories structure
|
||||||
|
|
||||||
The package will asume a directories structure starting with a base path that will be provided to the package configuration
|
The package will assume a directories structure starting with a base path that will be provided to the package configuration
|
||||||
or to object constructors depending on the use, but either will use the same convention to lookup inside the base path.
|
or to object constructors depending on the use, but either will use the same convention to lookup inside the base path.
|
||||||
|
|
||||||
Inside the base directory where will be the language directories named using the language and country 2-letter codes (en_US, es_AR, ...).
|
Inside the base directory where will be the language directories named using the language and country 2-letter codes (en_US, es_AR, ...).
|
||||||
@@ -110,7 +111,7 @@ func _(str string, vars ...interface{}) string {
|
|||||||
This is valid and can be used within a package.
|
This is valid and can be used within a package.
|
||||||
|
|
||||||
In normal conditions the Go compiler will optimize the calls to _() by replacing its content in place of the function call to reduce the function calling overhead.
|
In normal conditions the Go compiler will optimize the calls to _() by replacing its content in place of the function call to reduce the function calling overhead.
|
||||||
This is a normal Go compiler behaviour.
|
This is a normal Go compiler behavior.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -185,7 +186,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
This is also helpful for using inside templates (from the "text/template" package), where you can pass the Locale object to the template.
|
This is also helpful for using inside templates (from the "text/template" package), where you can pass the Locale object to the template.
|
||||||
If you set the Locale object as "Loc" in the template, then the templace code would look like:
|
If you set the Locale object as "Loc" in the template, then the template code would look like:
|
||||||
|
|
||||||
```
|
```
|
||||||
{{ .Loc.Get "Translate this" }}
|
{{ .Loc.Get "Translate this" }}
|
||||||
|
|||||||
29
gotext.go
29
gotext.go
@@ -124,3 +124,32 @@ func GetND(dom, str, plural string, n int, vars ...interface{}) string {
|
|||||||
// Return translation
|
// Return translation
|
||||||
return storage.GetND(dom, str, plural, n, vars...)
|
return storage.GetND(dom, str, plural, n, vars...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetC uses the default domain globally set to return the corresponding translation of the given string in the given context.
|
||||||
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
|
func GetC(str, ctx string, vars ...interface{}) string {
|
||||||
|
return GetDC(domain, str, ctx, vars...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNC retrieves the (N)th plural form translation for the given string in the given context in the "default" domain.
|
||||||
|
// If n == 0, usually the singular form of the string is returned as defined in the PO file.
|
||||||
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
|
func GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
|
||||||
|
return GetNDC("default", str, plural, n, ctx, vars...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDC returns the corresponding translation in the given domain for the given string in the given context.
|
||||||
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
|
func GetDC(dom, str, ctx string, vars ...interface{}) string {
|
||||||
|
return GetNDC(dom, str, str, 0, ctx, vars...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNDC retrieves the (N)th plural form translation in the given domain for a given string.
|
||||||
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
|
func GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) string {
|
||||||
|
// Try to load default package Locale storage
|
||||||
|
loadStorage(false)
|
||||||
|
|
||||||
|
// Return translation
|
||||||
|
return storage.GetNDC(dom, str, plural, n, ctx, vars...)
|
||||||
|
}
|
||||||
|
|||||||
152
gotext_test.go
152
gotext_test.go
@@ -6,6 +6,29 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestGettersSetters(t *testing.T) {
|
||||||
|
SetDomain("test")
|
||||||
|
dom := GetDomain()
|
||||||
|
|
||||||
|
if dom != "test" {
|
||||||
|
t.Errorf("Expected GetDomain to return 'test', but got '%s'", dom)
|
||||||
|
}
|
||||||
|
|
||||||
|
SetLibrary("/tmp/test")
|
||||||
|
lib := GetLibrary()
|
||||||
|
|
||||||
|
if lib != "/tmp/test" {
|
||||||
|
t.Errorf("Expected GetLibrary to return '/tmp/test', but got '%s'", lib)
|
||||||
|
}
|
||||||
|
|
||||||
|
SetLanguage("es")
|
||||||
|
lang := GetLanguage()
|
||||||
|
|
||||||
|
if lang != "es" {
|
||||||
|
t.Errorf("Expected GetLanguage to return 'es', but got '%s'", lang)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPackageFunctions(t *testing.T) {
|
func TestPackageFunctions(t *testing.T) {
|
||||||
// Set PO content
|
// Set PO content
|
||||||
str := `# Some comment
|
str := `# Some comment
|
||||||
@@ -16,6 +39,108 @@ msgstr "Translated text"
|
|||||||
msgid "Another string"
|
msgid "Another string"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
msgid "One with var: %s"
|
||||||
|
msgid_plural "Several with vars: %s"
|
||||||
|
msgstr[0] "This one is the singular: %s"
|
||||||
|
msgstr[1] "This one is the plural: %s"
|
||||||
|
msgstr[2] "And this is the second plural form: %s"
|
||||||
|
|
||||||
|
msgid "This one has invalid syntax translations"
|
||||||
|
msgid_plural "Plural index"
|
||||||
|
msgstr[abc] "Wrong index"
|
||||||
|
msgstr[1 "Forgot to close brackets"
|
||||||
|
msgstr[0] "Badly formatted string'
|
||||||
|
|
||||||
|
msgid "Invalid formatted id[] with no translations
|
||||||
|
|
||||||
|
msgctxt "Ctx"
|
||||||
|
msgid "One with var: %s"
|
||||||
|
msgid_plural "Several with vars: %s"
|
||||||
|
msgstr[0] "This one is the singular in a Ctx context: %s"
|
||||||
|
msgstr[1] "This one is the plural in a Ctx context: %s"
|
||||||
|
|
||||||
|
msgid "Some random"
|
||||||
|
msgstr "Some random translation"
|
||||||
|
|
||||||
|
msgctxt "Ctx"
|
||||||
|
msgid "Some random in a context"
|
||||||
|
msgstr "Some random translation in a context"
|
||||||
|
|
||||||
|
msgid "More"
|
||||||
|
msgstr "More translation"
|
||||||
|
|
||||||
|
`
|
||||||
|
|
||||||
|
// Create Locales directory on default location
|
||||||
|
dirname := path.Clean("/tmp" + string(os.PathSeparator) + "en_US")
|
||||||
|
err := os.MkdirAll(dirname, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't create test directory: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write PO content to default domain file
|
||||||
|
filename := path.Clean(dirname + string(os.PathSeparator) + "default.po")
|
||||||
|
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't create test file: %s", err.Error())
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = f.WriteString(str)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't write to test file: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set package configuration
|
||||||
|
Configure("/tmp", "en_US", "default")
|
||||||
|
|
||||||
|
// Test translations
|
||||||
|
tr := Get("My text")
|
||||||
|
if tr != "Translated text" {
|
||||||
|
t.Errorf("Expected 'Translated text' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
v := "Variable"
|
||||||
|
tr = Get("One with var: %s", v)
|
||||||
|
if tr != "This one is the singular: Variable" {
|
||||||
|
t.Errorf("Expected 'This one is the singular: Variable' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test plural
|
||||||
|
tr = GetN("One with var: %s", "Several with vars: %s", 2, v)
|
||||||
|
if tr != "And this is the second plural form: Variable" {
|
||||||
|
t.Errorf("Expected 'And this is the second plural form: Variable' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test context translations
|
||||||
|
tr = GetC("Some random in a context", "Ctx")
|
||||||
|
if tr != "Some random translation in a context" {
|
||||||
|
t.Errorf("Expected 'Some random translation in a context' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
v = "Variable"
|
||||||
|
tr = GetC("One with var: %s", "Ctx", v)
|
||||||
|
if tr != "This one is the singular in a Ctx context: Variable" {
|
||||||
|
t.Errorf("Expected 'This one is the singular in a Ctx context: Variable' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
tr = GetNC("One with var: %s", "Several with vars: %s", 1, "Ctx", v)
|
||||||
|
if tr != "This one is the plural in a Ctx context: Variable" {
|
||||||
|
t.Errorf("Expected 'This one is the plural in a Ctx context: Variable' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPackageRace(t *testing.T) {
|
||||||
|
// Set PO content
|
||||||
|
str := `# Some comment
|
||||||
|
msgid "My text"
|
||||||
|
msgstr "Translated text"
|
||||||
|
|
||||||
|
# More comments
|
||||||
|
msgid "Another string"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
msgid "One with var: %s"
|
msgid "One with var: %s"
|
||||||
msgid_plural "Several with vars: %s"
|
msgid_plural "Several with vars: %s"
|
||||||
msgstr[0] "This one is the singular: %s"
|
msgstr[0] "This one is the singular: %s"
|
||||||
@@ -45,21 +170,20 @@ msgstr[2] "And this is the second plural form: %s"
|
|||||||
t.Fatalf("Can't write to test file: %s", err.Error())
|
t.Fatalf("Can't write to test file: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Init sync channels
|
||||||
|
c1 := make(chan bool)
|
||||||
|
c2 := make(chan bool)
|
||||||
|
|
||||||
// Test translations
|
// Test translations
|
||||||
tr := Get("My text")
|
go func(done chan bool) {
|
||||||
if tr != "Translated text" {
|
Get("My text")
|
||||||
t.Errorf("Expected 'Translated text' but got '%s'", tr)
|
done <- true
|
||||||
}
|
}(c1)
|
||||||
|
|
||||||
v := "Variable"
|
go func(done chan bool) {
|
||||||
tr = Get("One with var: %s", v)
|
Get("My text")
|
||||||
if tr != "This one is the singular: Variable" {
|
done <- true
|
||||||
t.Errorf("Expected 'This one is the singular: Variable' but got '%s'", tr)
|
}(c2)
|
||||||
}
|
|
||||||
|
|
||||||
// Test plural
|
Get("My text")
|
||||||
tr = GetN("One with var: %s", "Several with vars: %s", 2, v)
|
|
||||||
if tr != "And this is the second plural form: Variable" {
|
|
||||||
t.Errorf("Expected 'And this is the second plural form: Variable' but got '%s'", tr)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
54
locale.go
54
locale.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -42,6 +43,9 @@ type Locale struct {
|
|||||||
|
|
||||||
// List of available domains for this locale.
|
// List of available domains for this locale.
|
||||||
domains map[string]*Po
|
domains map[string]*Po
|
||||||
|
|
||||||
|
// Sync Mutex
|
||||||
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLocale creates and initializes a new Locale object for a given language.
|
// NewLocale creates and initializes a new Locale object for a given language.
|
||||||
@@ -73,6 +77,9 @@ func (l *Locale) AddDomain(dom string) {
|
|||||||
po.ParseFile(filename)
|
po.ParseFile(filename)
|
||||||
|
|
||||||
// Save new domain
|
// Save new domain
|
||||||
|
l.Lock()
|
||||||
|
defer l.Unlock()
|
||||||
|
|
||||||
if l.domains == nil {
|
if l.domains == nil {
|
||||||
l.domains = make(map[string]*Po)
|
l.domains = make(map[string]*Po)
|
||||||
}
|
}
|
||||||
@@ -92,16 +99,20 @@ func (l *Locale) GetN(str, plural string, n int, vars ...interface{}) string {
|
|||||||
return l.GetND("default", str, plural, n, vars...)
|
return l.GetND("default", str, plural, n, vars...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetD returns the corresponding translation in the given domain for a given string.
|
// GetD returns the corresponding translation in the given domain for the given string.
|
||||||
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
func (l *Locale) GetD(dom, str string, vars ...interface{}) string {
|
func (l *Locale) GetD(dom, str string, vars ...interface{}) string {
|
||||||
return l.GetND(dom, str, str, 0, vars...)
|
return l.GetND(dom, str, str, 0, vars...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetND retrieves the (N)th plural form translation in the given domain for a given string.
|
// GetND retrieves the (N)th plural form translation in the given domain for the given string.
|
||||||
// If n == 0, usually the singular form of the string is returned as defined in the PO file.
|
// If n == 0, usually the singular form of the string is returned as defined in the PO file.
|
||||||
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
func (l *Locale) GetND(dom, str, plural string, n int, vars ...interface{}) string {
|
func (l *Locale) GetND(dom, str, plural string, n int, vars ...interface{}) string {
|
||||||
|
// Sync read
|
||||||
|
l.RLock()
|
||||||
|
defer l.RUnlock()
|
||||||
|
|
||||||
if l.domains != nil {
|
if l.domains != nil {
|
||||||
if _, ok := l.domains[dom]; ok {
|
if _, ok := l.domains[dom]; ok {
|
||||||
if l.domains[dom] != nil {
|
if l.domains[dom] != nil {
|
||||||
@@ -113,3 +124,42 @@ func (l *Locale) GetND(dom, str, plural string, n int, vars ...interface{}) stri
|
|||||||
// Return the same we received by default
|
// Return the same we received by default
|
||||||
return fmt.Sprintf(plural, vars...)
|
return fmt.Sprintf(plural, vars...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetC uses a domain "default" to return the corresponding translation of the given string in the given context.
|
||||||
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
|
func (l *Locale) GetC(str, ctx string, vars ...interface{}) string {
|
||||||
|
return l.GetDC("default", str, ctx, vars...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNC retrieves the (N)th plural form translation for the given string in the given context in the "default" domain.
|
||||||
|
// If n == 0, usually the singular form of the string is returned as defined in the PO file.
|
||||||
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
|
func (l *Locale) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
|
||||||
|
return l.GetNDC("default", str, plural, n, ctx, vars...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDC returns the corresponding translation in the given domain for the given string in the given context.
|
||||||
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
|
func (l *Locale) GetDC(dom, str, ctx string, vars ...interface{}) string {
|
||||||
|
return l.GetNDC(dom, str, str, 0, ctx, vars...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNDC retrieves the (N)th plural form translation in the given domain for the given string in the given context.
|
||||||
|
// If n == 0, usually the singular form of the string is returned as defined in the PO file.
|
||||||
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
|
func (l *Locale) GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) string {
|
||||||
|
// Sync read
|
||||||
|
l.RLock()
|
||||||
|
defer l.RUnlock()
|
||||||
|
|
||||||
|
if l.domains != nil {
|
||||||
|
if _, ok := l.domains[dom]; ok {
|
||||||
|
if l.domains[dom] != nil {
|
||||||
|
return l.domains[dom].GetNC(str, plural, n, ctx, vars...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the same we received by default
|
||||||
|
return fmt.Sprintf(plural, vars...)
|
||||||
|
}
|
||||||
|
|||||||
145
locale_test.go
145
locale_test.go
@@ -22,6 +22,30 @@ msgstr[0] "This one is the singular: %s"
|
|||||||
msgstr[1] "This one is the plural: %s"
|
msgstr[1] "This one is the plural: %s"
|
||||||
msgstr[2] "And this is the second plural form: %s"
|
msgstr[2] "And this is the second plural form: %s"
|
||||||
|
|
||||||
|
msgid "This one has invalid syntax translations"
|
||||||
|
msgid_plural "Plural index"
|
||||||
|
msgstr[abc] "Wrong index"
|
||||||
|
msgstr[1 "Forgot to close brackets"
|
||||||
|
msgstr[0] "Badly formatted string'
|
||||||
|
|
||||||
|
msgid "Invalid formatted id[] with no translations
|
||||||
|
|
||||||
|
msgctxt "Ctx"
|
||||||
|
msgid "One with var: %s"
|
||||||
|
msgid_plural "Several with vars: %s"
|
||||||
|
msgstr[0] "This one is the singular in a Ctx context: %s"
|
||||||
|
msgstr[1] "This one is the plural in a Ctx context: %s"
|
||||||
|
|
||||||
|
msgid "Some random"
|
||||||
|
msgstr "Some random translation"
|
||||||
|
|
||||||
|
msgctxt "Ctx"
|
||||||
|
msgid "Some random in a context"
|
||||||
|
msgstr "Some random translation in a context"
|
||||||
|
|
||||||
|
msgid "More"
|
||||||
|
msgstr "More translation"
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
// Create Locales directory with simplified language code
|
// Create Locales directory with simplified language code
|
||||||
@@ -48,6 +72,9 @@ msgstr[2] "And this is the second plural form: %s"
|
|||||||
// Create Locale with full language code
|
// Create Locale with full language code
|
||||||
l := NewLocale("/tmp", "en_US")
|
l := NewLocale("/tmp", "en_US")
|
||||||
|
|
||||||
|
// Force nil domain storage
|
||||||
|
l.domains = nil
|
||||||
|
|
||||||
// Add domain
|
// Add domain
|
||||||
l.AddDomain("my_domain")
|
l.AddDomain("my_domain")
|
||||||
|
|
||||||
@@ -68,4 +95,122 @@ msgstr[2] "And this is the second plural form: %s"
|
|||||||
if tr != "And this is the second plural form: Variable" {
|
if tr != "And this is the second plural form: Variable" {
|
||||||
t.Errorf("Expected 'And this is the second plural form: Variable' but got '%s'", tr)
|
t.Errorf("Expected 'And this is the second plural form: Variable' but got '%s'", tr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test non-existent "deafult" domain responses
|
||||||
|
tr = l.Get("My text")
|
||||||
|
if tr != "My text" {
|
||||||
|
t.Errorf("Expected 'My text' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
tr = l.GetN("One with var: %s", "Several with vars: %s", 2, v)
|
||||||
|
if tr != "Several with vars: Variable" {
|
||||||
|
t.Errorf("Expected 'Several with vars: Variable' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test inexistent translations
|
||||||
|
tr = l.Get("This is a test")
|
||||||
|
if tr != "This is a test" {
|
||||||
|
t.Errorf("Expected 'This is a test' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
tr = l.GetN("This is a test", "This are tests", 1)
|
||||||
|
if tr != "This are tests" {
|
||||||
|
t.Errorf("Expected 'This are tests' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test syntax error parsed translations
|
||||||
|
tr = l.Get("This one has invalid syntax translations")
|
||||||
|
if tr != "This one has invalid syntax translations" {
|
||||||
|
t.Errorf("Expected 'This one has invalid syntax translations' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
tr = l.GetN("This one has invalid syntax translations", "This are tests", 1)
|
||||||
|
if tr != "This are tests" {
|
||||||
|
t.Errorf("Expected 'Plural index' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test context translations
|
||||||
|
v = "Test"
|
||||||
|
tr = l.GetDC("my_domain", "One with var: %s", "Ctx", v)
|
||||||
|
if tr != "This one is the singular in a Ctx context: Test" {
|
||||||
|
t.Errorf("Expected 'This one is the singular in a Ctx context: Test' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test plural
|
||||||
|
tr = l.GetNDC("my_domain", "One with var: %s", "Several with vars: %s", 1, "Ctx", v)
|
||||||
|
if tr != "This one is the plural in a Ctx context: Test" {
|
||||||
|
t.Errorf("Expected 'This one is the plural in a Ctx context: Test' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test last translation
|
||||||
|
tr = l.GetD("my_domain", "More")
|
||||||
|
if tr != "More translation" {
|
||||||
|
t.Errorf("Expected 'More translation' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocaleRace(t *testing.T) {
|
||||||
|
// Set PO content
|
||||||
|
str := `# Some comment
|
||||||
|
msgid "My text"
|
||||||
|
msgstr "Translated text"
|
||||||
|
|
||||||
|
# More comments
|
||||||
|
msgid "Another string"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
msgid "One with var: %s"
|
||||||
|
msgid_plural "Several with vars: %s"
|
||||||
|
msgstr[0] "This one is the singular: %s"
|
||||||
|
msgstr[1] "This one is the plural: %s"
|
||||||
|
msgstr[2] "And this is the second plural form: %s"
|
||||||
|
|
||||||
|
`
|
||||||
|
|
||||||
|
// Create Locales directory with simplified language code
|
||||||
|
dirname := path.Clean("/tmp" + string(os.PathSeparator) + "es")
|
||||||
|
err := os.MkdirAll(dirname, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't create test directory: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write PO content to file
|
||||||
|
filename := path.Clean(dirname + string(os.PathSeparator) + "race.po")
|
||||||
|
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't create test file: %s", err.Error())
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = f.WriteString(str)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can't write to test file: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Locale with full language code
|
||||||
|
l := NewLocale("/tmp", "es")
|
||||||
|
|
||||||
|
// Init sync channels
|
||||||
|
ac := make(chan bool)
|
||||||
|
rc := make(chan bool)
|
||||||
|
|
||||||
|
// Add domain in goroutine
|
||||||
|
go func(l *Locale, done chan bool) {
|
||||||
|
l.AddDomain("race")
|
||||||
|
done <- true
|
||||||
|
}(l, ac)
|
||||||
|
|
||||||
|
// Get translations in goroutine
|
||||||
|
go func(l *Locale, done chan bool) {
|
||||||
|
l.GetD("race", "My text")
|
||||||
|
done <- true
|
||||||
|
}(l, rc)
|
||||||
|
|
||||||
|
// Get translations at top level
|
||||||
|
l.GetD("race", "My text")
|
||||||
|
|
||||||
|
// Wait for goroutines to finish
|
||||||
|
<-ac
|
||||||
|
<-rc
|
||||||
}
|
}
|
||||||
|
|||||||
111
po.go
111
po.go
@@ -66,6 +66,7 @@ Example:
|
|||||||
type Po struct {
|
type Po struct {
|
||||||
// Storage
|
// Storage
|
||||||
translations map[string]*translation
|
translations map[string]*translation
|
||||||
|
contexts map[string]map[string]*translation
|
||||||
|
|
||||||
// Sync Mutex
|
// Sync Mutex
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
@@ -95,16 +96,23 @@ func (po *Po) ParseFile(f string) {
|
|||||||
|
|
||||||
// Parse loads the translations specified in the provided string (str)
|
// Parse loads the translations specified in the provided string (str)
|
||||||
func (po *Po) Parse(str string) {
|
func (po *Po) Parse(str string) {
|
||||||
|
// Init storage
|
||||||
if po.translations == nil {
|
if po.translations == nil {
|
||||||
po.Lock()
|
po.Lock()
|
||||||
po.translations = make(map[string]*translation)
|
po.translations = make(map[string]*translation)
|
||||||
|
po.contexts = make(map[string]map[string]*translation)
|
||||||
po.Unlock()
|
po.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get lines
|
||||||
lines := strings.Split(str, "\n")
|
lines := strings.Split(str, "\n")
|
||||||
|
|
||||||
|
// Translation buffer
|
||||||
tr := newTranslation()
|
tr := newTranslation()
|
||||||
|
|
||||||
|
// Context buffer
|
||||||
|
ctx := ""
|
||||||
|
|
||||||
for _, l := range lines {
|
for _, l := range lines {
|
||||||
// Trim spaces
|
// Trim spaces
|
||||||
l = strings.TrimSpace(l)
|
l = strings.TrimSpace(l)
|
||||||
@@ -115,19 +123,59 @@ func (po *Po) Parse(str string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Skip invalid lines
|
// Skip invalid lines
|
||||||
if !strings.HasPrefix(l, "msgid") && !strings.HasPrefix(l, "msgid_plural") && !strings.HasPrefix(l, "msgstr") {
|
if !strings.HasPrefix(l, "msgctxt") && !strings.HasPrefix(l, "msgid") && !strings.HasPrefix(l, "msgid_plural") && !strings.HasPrefix(l, "msgstr") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Buffer context and continue
|
||||||
|
if strings.HasPrefix(l, "msgctxt") {
|
||||||
|
// Save current translation buffer.
|
||||||
|
po.Lock()
|
||||||
|
// No context
|
||||||
|
if ctx == "" {
|
||||||
|
po.translations[tr.id] = tr
|
||||||
|
} else {
|
||||||
|
// Save context
|
||||||
|
if _, ok := po.contexts[ctx]; !ok {
|
||||||
|
po.contexts[ctx] = make(map[string]*translation)
|
||||||
|
}
|
||||||
|
po.contexts[ctx][tr.id] = tr
|
||||||
|
}
|
||||||
|
po.Unlock()
|
||||||
|
|
||||||
|
// Flush buffer
|
||||||
|
tr = newTranslation()
|
||||||
|
ctx = ""
|
||||||
|
|
||||||
|
// Buffer context
|
||||||
|
ctx, _ = strconv.Unquote(strings.TrimSpace(strings.TrimPrefix(l, "msgctxt")))
|
||||||
|
|
||||||
|
// Loop
|
||||||
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") {
|
||||||
// Save current translation buffer.
|
// Save current translation buffer if not inside a context.
|
||||||
|
if ctx == "" {
|
||||||
po.Lock()
|
po.Lock()
|
||||||
po.translations[tr.id] = tr
|
po.translations[tr.id] = tr
|
||||||
po.Unlock()
|
po.Unlock()
|
||||||
|
|
||||||
// Flush buffer
|
// Flush buffer
|
||||||
tr = newTranslation()
|
tr = newTranslation()
|
||||||
|
ctx = ""
|
||||||
|
} else if ctx != "" && tr.id != "" {
|
||||||
|
// Save current translation buffer inside a context
|
||||||
|
if _, ok := po.contexts[ctx]; !ok {
|
||||||
|
po.contexts[ctx] = make(map[string]*translation)
|
||||||
|
}
|
||||||
|
po.contexts[ctx][tr.id] = tr
|
||||||
|
|
||||||
|
// Flush buffer
|
||||||
|
tr = newTranslation()
|
||||||
|
ctx = ""
|
||||||
|
}
|
||||||
|
|
||||||
// Set id
|
// Set id
|
||||||
tr.id, _ = strconv.Unquote(strings.TrimSpace(strings.TrimPrefix(l, "msgid")))
|
tr.id, _ = strconv.Unquote(strings.TrimSpace(strings.TrimPrefix(l, "msgid")))
|
||||||
@@ -178,7 +226,15 @@ func (po *Po) Parse(str string) {
|
|||||||
// Save last translation buffer.
|
// Save last translation buffer.
|
||||||
if tr.id != "" {
|
if tr.id != "" {
|
||||||
po.Lock()
|
po.Lock()
|
||||||
|
if ctx == "" {
|
||||||
po.translations[tr.id] = tr
|
po.translations[tr.id] = tr
|
||||||
|
} else {
|
||||||
|
// Save context
|
||||||
|
if _, ok := po.contexts[ctx]; !ok {
|
||||||
|
po.contexts[ctx] = make(map[string]*translation)
|
||||||
|
}
|
||||||
|
po.contexts[ctx][tr.id] = tr
|
||||||
|
}
|
||||||
po.Unlock()
|
po.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,6 +242,10 @@ func (po *Po) Parse(str string) {
|
|||||||
// Get retrieves the corresponding translation for the given string.
|
// Get retrieves the corresponding translation for the given string.
|
||||||
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
func (po *Po) Get(str string, vars ...interface{}) string {
|
func (po *Po) Get(str string, vars ...interface{}) string {
|
||||||
|
// Sync read
|
||||||
|
po.RLock()
|
||||||
|
defer po.RUnlock()
|
||||||
|
|
||||||
if po.translations != nil {
|
if po.translations != nil {
|
||||||
if _, ok := po.translations[str]; ok {
|
if _, ok := po.translations[str]; ok {
|
||||||
return fmt.Sprintf(po.translations[str].get(), vars...)
|
return fmt.Sprintf(po.translations[str].get(), vars...)
|
||||||
@@ -200,6 +260,10 @@ func (po *Po) Get(str string, vars ...interface{}) string {
|
|||||||
// If n == 0, usually the singular form of the string is returned as defined in the PO file.
|
// If n == 0, usually the singular form of the string is returned as defined in the PO file.
|
||||||
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
func (po *Po) GetN(str, plural string, n int, vars ...interface{}) string {
|
func (po *Po) GetN(str, plural string, n int, vars ...interface{}) string {
|
||||||
|
// Sync read
|
||||||
|
po.RLock()
|
||||||
|
defer po.RUnlock()
|
||||||
|
|
||||||
if po.translations != nil {
|
if po.translations != nil {
|
||||||
if _, ok := po.translations[str]; ok {
|
if _, ok := po.translations[str]; ok {
|
||||||
return fmt.Sprintf(po.translations[str].getN(n), vars...)
|
return fmt.Sprintf(po.translations[str].getN(n), vars...)
|
||||||
@@ -209,3 +273,46 @@ func (po *Po) GetN(str, plural string, n int, vars ...interface{}) string {
|
|||||||
// Return the plural string we received by default
|
// Return the plural string we received by default
|
||||||
return fmt.Sprintf(plural, vars...)
|
return fmt.Sprintf(plural, vars...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetC retrieves the corresponding translation for a given string in the given context.
|
||||||
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
|
func (po *Po) GetC(str, ctx string, vars ...interface{}) string {
|
||||||
|
// Sync read
|
||||||
|
po.RLock()
|
||||||
|
defer po.RUnlock()
|
||||||
|
|
||||||
|
if po.contexts != nil {
|
||||||
|
if _, ok := po.contexts[ctx]; ok {
|
||||||
|
if po.contexts[ctx] != nil {
|
||||||
|
if _, ok := po.contexts[ctx][str]; ok {
|
||||||
|
return fmt.Sprintf(po.contexts[ctx][str].get(), vars...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the string we received by default
|
||||||
|
return fmt.Sprintf(str, vars...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNC retrieves the (N)th plural form translation for the given string in the given context.
|
||||||
|
// If n == 0, usually the singular form of the string is returned as defined in the PO file.
|
||||||
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
||||||
|
func (po *Po) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
|
||||||
|
// Sync read
|
||||||
|
po.RLock()
|
||||||
|
defer po.RUnlock()
|
||||||
|
|
||||||
|
if po.contexts != nil {
|
||||||
|
if _, ok := po.contexts[ctx]; ok {
|
||||||
|
if po.contexts[ctx] != nil {
|
||||||
|
if _, ok := po.contexts[ctx][str]; ok {
|
||||||
|
return fmt.Sprintf(po.contexts[ctx][str].getN(n), vars...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the plural string we received by default
|
||||||
|
return fmt.Sprintf(plural, vars...)
|
||||||
|
}
|
||||||
|
|||||||
137
po_test.go
137
po_test.go
@@ -22,7 +22,31 @@ msgstr[0] "This one is the singular: %s"
|
|||||||
msgstr[1] "This one is the plural: %s"
|
msgstr[1] "This one is the plural: %s"
|
||||||
msgstr[2] "And this is the second plural form: %s"
|
msgstr[2] "And this is the second plural form: %s"
|
||||||
|
|
||||||
|
msgid "This one has invalid syntax translations"
|
||||||
|
msgid_plural "Plural index"
|
||||||
|
msgstr[abc] "Wrong index"
|
||||||
|
msgstr[1 "Forgot to close brackets"
|
||||||
|
msgstr[0] "Badly formatted string'
|
||||||
|
|
||||||
|
msgid "Invalid formatted id[] with no translations
|
||||||
|
|
||||||
|
msgctxt "Ctx"
|
||||||
|
msgid "One with var: %s"
|
||||||
|
msgid_plural "Several with vars: %s"
|
||||||
|
msgstr[0] "This one is the singular in a Ctx context: %s"
|
||||||
|
msgstr[1] "This one is the plural in a Ctx context: %s"
|
||||||
|
|
||||||
|
msgid "Some random"
|
||||||
|
msgstr "Some random translation"
|
||||||
|
|
||||||
|
msgctxt "Ctx"
|
||||||
|
msgid "Some random in a context"
|
||||||
|
msgstr "Some random translation in a context"
|
||||||
|
|
||||||
|
msgid "More"
|
||||||
|
msgstr "More translation"
|
||||||
`
|
`
|
||||||
|
|
||||||
// Write PO content to file
|
// Write PO content to file
|
||||||
filename := path.Clean(os.TempDir() + string(os.PathSeparator) + "default.po")
|
filename := path.Clean(os.TempDir() + string(os.PathSeparator) + "default.po")
|
||||||
|
|
||||||
@@ -37,8 +61,13 @@ msgstr[2] "And this is the second plural form: %s"
|
|||||||
t.Fatalf("Can't write to test file: %s", err.Error())
|
t.Fatalf("Can't write to test file: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse po file
|
// Create po object
|
||||||
po := new(Po)
|
po := new(Po)
|
||||||
|
|
||||||
|
// Try to parse a directory
|
||||||
|
po.ParseFile(path.Clean(os.TempDir()))
|
||||||
|
|
||||||
|
// Parse file
|
||||||
po.ParseFile(filename)
|
po.ParseFile(filename)
|
||||||
|
|
||||||
// Test translations
|
// Test translations
|
||||||
@@ -58,4 +87,110 @@ msgstr[2] "And this is the second plural form: %s"
|
|||||||
if tr != "And this is the second plural form: Variable" {
|
if tr != "And this is the second plural form: Variable" {
|
||||||
t.Errorf("Expected 'And this is the second plural form: Variable' but got '%s'", tr)
|
t.Errorf("Expected 'And this is the second plural form: Variable' but got '%s'", tr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test inexistent translations
|
||||||
|
tr = po.Get("This is a test")
|
||||||
|
if tr != "This is a test" {
|
||||||
|
t.Errorf("Expected 'This is a test' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
tr = po.GetN("This is a test", "This are tests", 1)
|
||||||
|
if tr != "This are tests" {
|
||||||
|
t.Errorf("Expected 'This are tests' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test syntax error parsed translations
|
||||||
|
tr = po.Get("This one has invalid syntax translations")
|
||||||
|
if tr != "" {
|
||||||
|
t.Errorf("Expected '' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
tr = po.GetN("This one has invalid syntax translations", "This are tests", 1)
|
||||||
|
if tr != "Plural index" {
|
||||||
|
t.Errorf("Expected 'Plural index' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test context translations
|
||||||
|
v = "Test"
|
||||||
|
tr = po.GetC("One with var: %s", "Ctx", v)
|
||||||
|
if tr != "This one is the singular in a Ctx context: Test" {
|
||||||
|
t.Errorf("Expected 'This one is the singular in a Ctx context: Test' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test plural
|
||||||
|
tr = po.GetNC("One with var: %s", "Several with vars: %s", 1, "Ctx", v)
|
||||||
|
if tr != "This one is the plural in a Ctx context: Test" {
|
||||||
|
t.Errorf("Expected 'This one is the plural in a Ctx context: Test' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test last translation
|
||||||
|
tr = po.Get("More")
|
||||||
|
if tr != "More translation" {
|
||||||
|
t.Errorf("Expected 'More translation' but got '%s'", tr)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTranslationObject(t *testing.T) {
|
||||||
|
tr := newTranslation()
|
||||||
|
str := tr.get()
|
||||||
|
|
||||||
|
if str != "" {
|
||||||
|
t.Errorf("Expected '' but got '%s'", str)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set id
|
||||||
|
tr.id = "Text"
|
||||||
|
|
||||||
|
// Get again
|
||||||
|
str = tr.get()
|
||||||
|
|
||||||
|
if str != "Text" {
|
||||||
|
t.Errorf("Expected 'Text' but got '%s'", str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPoRace(t *testing.T) {
|
||||||
|
// Set PO content
|
||||||
|
str := `# Some comment
|
||||||
|
msgid "My text"
|
||||||
|
msgstr "Translated text"
|
||||||
|
|
||||||
|
# More comments
|
||||||
|
msgid "Another string"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
msgid "One with var: %s"
|
||||||
|
msgid_plural "Several with vars: %s"
|
||||||
|
msgstr[0] "This one is the singular: %s"
|
||||||
|
msgstr[1] "This one is the plural: %s"
|
||||||
|
msgstr[2] "And this is the second plural form: %s"
|
||||||
|
|
||||||
|
`
|
||||||
|
|
||||||
|
// Create Po object
|
||||||
|
po := new(Po)
|
||||||
|
|
||||||
|
// Create sync channels
|
||||||
|
pc := make(chan bool)
|
||||||
|
rc := make(chan bool)
|
||||||
|
|
||||||
|
// Parse po content in a goroutine
|
||||||
|
go func(po *Po, done chan bool) {
|
||||||
|
po.Parse(str)
|
||||||
|
done <- true
|
||||||
|
}(po, pc)
|
||||||
|
|
||||||
|
// Read some translation on a goroutine
|
||||||
|
go func(po *Po, done chan bool) {
|
||||||
|
po.Get("My text")
|
||||||
|
done <- true
|
||||||
|
}(po, rc)
|
||||||
|
|
||||||
|
// Read something at top level
|
||||||
|
po.Get("My text")
|
||||||
|
|
||||||
|
// Wait for goroutines to finish
|
||||||
|
<-pc
|
||||||
|
<-rc
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user