Improve sync method. Improve docs.
This commit is contained in:
33
README.md
33
README.md
@@ -8,6 +8,7 @@ GNU gettext utilities for Go.
|
|||||||
- 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.
|
||||||
- Supports pluralization rules.
|
- Supports pluralization rules.
|
||||||
- Ready to use inside Go templates.
|
- Ready to use inside Go templates.
|
||||||
|
- Safe for concurrent use accross multiple goroutines.
|
||||||
- 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 formed isn't available.
|
||||||
- Unit tests available
|
- Unit tests available
|
||||||
|
|
||||||
@@ -34,6 +35,38 @@ Refer to the Godoc package documentation at (https://godoc.org/github.com/leonel
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# About translation function names
|
||||||
|
|
||||||
|
The standard GNU gettext defines helper functions that maps to the gettext() function and it's widely adopted by most implementations.
|
||||||
|
|
||||||
|
The basic translation function is usually _() in the form:
|
||||||
|
|
||||||
|
```
|
||||||
|
_("Translate this")
|
||||||
|
```
|
||||||
|
|
||||||
|
In Go, this can't be implemented by a reusable package as the function name has to start with a capital letter in order to be exported.
|
||||||
|
|
||||||
|
Each implementation of this package can declare this helper functions inside their own packages if this function naming are desired/needed:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/leonelquinteros/gotext"
|
||||||
|
|
||||||
|
func _(str string, vars ...interface{}) string {
|
||||||
|
return gotext.Get(str, vars...)
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
This is a normal Go compiler behaviour.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Usage examples
|
# Usage examples
|
||||||
|
|
||||||
## Using package for single language/domain settings
|
## Using package for single language/domain settings
|
||||||
|
|||||||
9
po.go
9
po.go
@@ -75,11 +75,10 @@ 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) {
|
||||||
po.Lock()
|
|
||||||
defer po.Unlock()
|
|
||||||
|
|
||||||
if po.translations == nil {
|
if po.translations == nil {
|
||||||
|
po.Lock()
|
||||||
po.translations = make(map[string]*translation)
|
po.translations = make(map[string]*translation)
|
||||||
|
po.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
lines := strings.Split(str, "\n")
|
lines := strings.Split(str, "\n")
|
||||||
@@ -103,7 +102,9 @@ func (po *Po) Parse(str string) {
|
|||||||
// 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.
|
||||||
|
po.Lock()
|
||||||
po.translations[tr.id] = tr
|
po.translations[tr.id] = tr
|
||||||
|
po.Unlock()
|
||||||
|
|
||||||
// Flush buffer
|
// Flush buffer
|
||||||
tr = newTranslation()
|
tr = newTranslation()
|
||||||
@@ -156,7 +157,9 @@ func (po *Po) Parse(str string) {
|
|||||||
|
|
||||||
// Save last translation buffer.
|
// Save last translation buffer.
|
||||||
if tr.id != "" {
|
if tr.id != "" {
|
||||||
|
po.Lock()
|
||||||
po.translations[tr.id] = tr
|
po.translations[tr.id] = tr
|
||||||
|
po.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user