Improve sync method. Improve docs.

This commit is contained in:
Leonel Quinteros
2016-06-23 12:54:38 -03:00
parent 569d905178
commit 64c6ac19d9
2 changed files with 39 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ GNU gettext utilities for Go.
- It works with UTF-8 encoding as it's the default for Go language.
- Supports pluralization rules.
- 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.
- 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
## Using package for single language/domain settings

9
po.go
View File

@@ -75,11 +75,10 @@ func (po *Po) ParseFile(f string) {
// Parse loads the translations specified in the provided string (str)
func (po *Po) Parse(str string) {
po.Lock()
defer po.Unlock()
if po.translations == nil {
po.Lock()
po.translations = make(map[string]*translation)
po.Unlock()
}
lines := strings.Split(str, "\n")
@@ -103,7 +102,9 @@ func (po *Po) Parse(str string) {
// Buffer msgid and continue
if strings.HasPrefix(l, "msgid") && !strings.HasPrefix(l, "msgid_plural") {
// Save current translation buffer.
po.Lock()
po.translations[tr.id] = tr
po.Unlock()
// Flush buffer
tr = newTranslation()
@@ -156,7 +157,9 @@ func (po *Po) Parse(str string) {
// Save last translation buffer.
if tr.id != "" {
po.Lock()
po.translations[tr.id] = tr
po.Unlock()
}
}