Skip to content

Commit

Permalink
Fix Locale.findPO to support language code simplification on LC_MESSA…
Browse files Browse the repository at this point in the history
…GES dir.
  • Loading branch information
leonelquinteros committed Jul 1, 2016
1 parent e62229f commit ad380b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

GNU gettext utilities for Go.

Version: [1.0.0](https://github.com/leonelquinteros/gotext/releases/tag/v1.0.0)
Version: [1.0.1](https://github.com/leonelquinteros/gotext/releases/tag/v1.0.1)


# Features
Expand All @@ -17,7 +17,7 @@ Version: [1.0.0](https://github.com/leonelquinteros/gotext/releases/tag/v1.0.0)
- Thread-safe: This package is safe for concurrent use across multiple goroutines.
- It works with UTF-8 encoding as it's the default for Go language.
- Unit tests available.
- Language codes are automatically simplified from the form "en_UK" to "en" if the first 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.


Expand Down Expand Up @@ -49,25 +49,29 @@ or to object constructors depending on the use, but either will use the same con

Inside the base directory where will be the language directories named using the language and country 2-letter codes (en_US, es_AR, ...).
All package functions can lookup after the simplified version for each language in case the full code isn't present but the more general language code exists.
So if the language set is "en_UK", but there is no directory named after that code and there is a directory named "en",
So if the language set is `en_UK`, but there is no directory named after that code and there is a directory named `en`,
all package functions will be able to resolve this generalization and provide translations for the more general library.

The language codes are assumed to be [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) codes (2-letter codes).
That said, most functions will work with any coding standard as long the directory name matches the language code set on the configuration.
That said, most functions will work with any coding standard as long the directory name matches the language code set on the configuration.

A normal library directory structure may look like:
Then, there can be a `LC_MESSAGES` containing all PO files or the PO files themselves.
A library directory structure can look like:

```
/path/to/locales
/path/to/locales/en_US
/path/to/locales/en_US/default.po
/path/to/locales/en_US/extras.po
/path/to/locales/en_US/LC_MESSAGES
/path/to/locales/en_US/LC_MESSAGES/default.po
/path/to/locales/en_US/LC_MESSAGES/extras.po
/path/to/locales/en_UK
/path/to/locales/en_UK/default.po
/path/to/locales/en_UK/extras.po
/path/to/locales/en_UK/LC_MESSAGES
/path/to/locales/en_UK/LC_MESSAGES/default.po
/path/to/locales/en_UK/LC_MESSAGES/extras.po
/path/to/locales/en_AU
/path/to/locales/en_AU/default.po
/path/to/locales/en_AU/extras.po
/path/to/locales/en_AU/LC_MESSAGES
/path/to/locales/en_AU/LC_MESSAGES/default.po
/path/to/locales/en_AU/LC_MESSAGES/extras.po
/path/to/locales/es
/path/to/locales/es/default.po
/path/to/locales/es/extras.po
Expand All @@ -85,9 +89,9 @@ And so on...

# 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 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:
The basic translation function is usually `_()` in the form:

```
_("Translate this")
Expand All @@ -110,7 +114,7 @@ func _(str string, vars ...interface{}) string {

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 behavior.


Expand Down
14 changes: 11 additions & 3 deletions locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ Example:
// Create Locale with library path and language code
l := gotext.NewLocale("/path/to/i18n/dir", "en_US")
// Load domain '/path/to/i18n/dir/en_US/default.po'
// Load domain '/path/to/i18n/dir/en_US/LC_MESSAGES/default.po'
l.AddDomain("default")
// Translate text from default domain
println(l.Get("Translate this"))
// Load different domain ('/path/to/i18n/dir/en_US/extras.po')
// Load different domain ('/path/to/i18n/dir/en_US/LC_MESSAGES/extras.po')
l.AddDomain("extras")
// Translate text from domain
Expand Down Expand Up @@ -64,14 +64,22 @@ func (l *Locale) findPO(dom string) string {
return filename
}

if len(l.lang) > 2 {
filename = path.Join(l.path, l.lang[:2], "LC_MESSAGES", dom+".po")
if _, err := os.Stat(filename); err == nil {
return filename
}
}

filename = path.Join(l.path, l.lang, dom+".po")
if _, err := os.Stat(filename); err == nil {
return filename
}

if len(filename) > 2 {
if len(l.lang) > 2 {
filename = path.Join(l.path, l.lang[:2], dom+".po")
}

return filename
}

Expand Down
8 changes: 4 additions & 4 deletions locale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ msgstr "More translation"
`

// Create Locales directory with simplified language code
dirname := path.Clean("/tmp" + string(os.PathSeparator) + "en")
dirname := path.Join("/tmp", "en", "LC_MESSAGES")
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) + "my_domain.po")
filename := path.Join(dirname, "my_domain.po")

f, err := os.Create(filename)
if err != nil {
Expand Down Expand Up @@ -168,14 +168,14 @@ 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")
dirname := path.Join("/tmp", "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")
filename := path.Join(dirname, "race.po")

f, err := os.Create(filename)
if err != nil {
Expand Down

0 comments on commit ad380b8

Please sign in to comment.