fix path on different OS
This commit is contained in:
40
gotext.go
40
gotext.go
@@ -1,24 +1,24 @@
|
|||||||
/*
|
/*
|
||||||
* Package gotext implements GNU gettext utilities.
|
Package gotext implements GNU gettext utilities.
|
||||||
*
|
|
||||||
* For quick/simple translations you can use the package level functions directly.
|
For quick/simple translations you can use the package level functions directly.
|
||||||
*
|
|
||||||
* import (
|
import (
|
||||||
* "fmt"
|
"fmt"
|
||||||
* "git.deineagentur.com/DeineAgenturUG/gotext"
|
"git.deineagentur.com/DeineAgenturUG/gotext"
|
||||||
* )
|
)
|
||||||
*
|
|
||||||
* func main() {
|
func main() {
|
||||||
* // Configure package
|
// Configure package
|
||||||
* gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name")
|
gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name")
|
||||||
*
|
|
||||||
* // Translate text from default domain
|
// Translate text from default domain
|
||||||
* fmt.Println(gotext.Get("My text on 'domain-name' domain"))
|
fmt.Println(gotext.Get("My text on 'domain-name' domain"))
|
||||||
*
|
|
||||||
* // Translate text from a different domain without reconfigure
|
// Translate text from a different domain without reconfigure
|
||||||
* fmt.Println(gotext.GetD("domain2", "Another text on a different domain"))
|
fmt.Println(gotext.GetD("domain2", "Another text on a different domain"))
|
||||||
* }
|
}
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
package gotext
|
package gotext
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package gotext
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -99,14 +98,14 @@ msgstr "Another text on another domain"
|
|||||||
`
|
`
|
||||||
|
|
||||||
// Create Locales directory on default location
|
// Create Locales directory on default location
|
||||||
dirname := path.Join("/tmp", "en_US")
|
dirname := filepath.Join("/tmp", "en_US")
|
||||||
err := os.MkdirAll(dirname, os.ModePerm)
|
err := os.MkdirAll(dirname, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't create test directory: %s", err.Error())
|
t.Fatalf("Can't create test directory: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write PO content to default domain file
|
// Write PO content to default domain file
|
||||||
filename := path.Join(dirname, "default.po")
|
filename := filepath.Join(dirname, "default.po")
|
||||||
|
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -118,7 +117,7 @@ msgstr "Another text on another domain"
|
|||||||
t.Fatalf("Can't write to test file: %s", err.Error())
|
t.Fatalf("Can't write to test file: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
anotherFilename := path.Join(dirname, "another.po")
|
anotherFilename := filepath.Join(dirname, "another.po")
|
||||||
|
|
||||||
af, err := os.Create(anotherFilename)
|
af, err := os.Create(anotherFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -198,14 +197,14 @@ msgstr[1] ""
|
|||||||
`
|
`
|
||||||
|
|
||||||
// Create Locales directory on default location
|
// Create Locales directory on default location
|
||||||
dirname := path.Join("/tmp", "en_US")
|
dirname := filepath.Join("/tmp", "en_US")
|
||||||
err := os.MkdirAll(dirname, os.ModePerm)
|
err := os.MkdirAll(dirname, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't create test directory: %s", err.Error())
|
t.Fatalf("Can't create test directory: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write PO content to default domain file
|
// Write PO content to default domain file
|
||||||
filename := path.Join(dirname, "default.po")
|
filename := filepath.Join(dirname, "default.po")
|
||||||
|
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -318,19 +317,19 @@ msgstr[1] "Custom ctx translations"
|
|||||||
`
|
`
|
||||||
|
|
||||||
// Create Locales directory and files on temp location
|
// Create Locales directory and files on temp location
|
||||||
dirname := path.Join("/tmp", "en_US")
|
dirname := filepath.Join("/tmp", "en_US")
|
||||||
err := os.MkdirAll(dirname, os.ModePerm)
|
err := os.MkdirAll(dirname, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't create test directory: %s", err.Error())
|
t.Fatalf("Can't create test directory: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
fDefault, err := os.Create(path.Join(dirname, "default.po"))
|
fDefault, err := os.Create(filepath.Join(dirname, "default.po"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't create test file: %s", err.Error())
|
t.Fatalf("Can't create test file: %s", err.Error())
|
||||||
}
|
}
|
||||||
defer fDefault.Close()
|
defer fDefault.Close()
|
||||||
|
|
||||||
fCustom, err := os.Create(path.Join(dirname, "custom.po"))
|
fCustom, err := os.Create(filepath.Join(dirname, "custom.po"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't create test file: %s", err.Error())
|
t.Fatalf("Can't create test file: %s", err.Error())
|
||||||
}
|
}
|
||||||
@@ -408,14 +407,14 @@ msgstr "Some random Translation in a context"
|
|||||||
`
|
`
|
||||||
|
|
||||||
// Create Locales directory on default location
|
// Create Locales directory on default location
|
||||||
dirname := path.Join("/tmp", "en_US")
|
dirname := filepath.Join("/tmp", "en_US")
|
||||||
err := os.MkdirAll(dirname, os.ModePerm)
|
err := os.MkdirAll(dirname, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't create test directory: %s", err.Error())
|
t.Fatalf("Can't create test directory: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write PO content to default domain file
|
// Write PO content to default domain file
|
||||||
filename := path.Join("/tmp", GetDomain()+".po")
|
filename := filepath.Join("/tmp", GetDomain()+".po")
|
||||||
|
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -437,7 +436,7 @@ msgstr "Some random Translation in a context"
|
|||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
GetLibrary()
|
GetLibrary()
|
||||||
SetLibrary(path.Join("/tmp", "gotextlib"))
|
SetLibrary(filepath.Join("/tmp", "gotextlib"))
|
||||||
GetDomain()
|
GetDomain()
|
||||||
SetDomain("default")
|
SetDomain("default")
|
||||||
GetLanguage()
|
GetLanguage()
|
||||||
|
|||||||
13
locale.go
13
locale.go
@@ -9,11 +9,11 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/*
|
||||||
Locale wraps the entire i18n collection for a single language (locale)
|
Locale wraps the entire i18n collection for a single language (locale)
|
||||||
It's used by the package functions, but it can also be used independently to handle
|
It's used by the package functions, but it can also be used independently to handle
|
||||||
multiple languages at the same time by working with this object.
|
multiple languages at the same time by working with this object.
|
||||||
@@ -44,7 +44,6 @@ Example:
|
|||||||
fmt.Println(l.GetD("extras", "Translate this"))
|
fmt.Println(l.GetD("extras", "Translate this"))
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type Locale struct {
|
type Locale struct {
|
||||||
// Path to locale files.
|
// Path to locale files.
|
||||||
path string
|
path string
|
||||||
@@ -73,25 +72,25 @@ func NewLocale(p, l string) *Locale {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Locale) findExt(dom, ext string) string {
|
func (l *Locale) findExt(dom, ext string) string {
|
||||||
filename := path.Join(l.path, l.lang, "LC_MESSAGES", dom+"."+ext)
|
filename := filepath.Join(l.path, l.lang, "LC_MESSAGES", dom+"."+ext)
|
||||||
if _, err := os.Stat(filename); err == nil {
|
if _, err := os.Stat(filename); err == nil {
|
||||||
return filename
|
return filename
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(l.lang) > 2 {
|
if len(l.lang) > 2 {
|
||||||
filename = path.Join(l.path, l.lang[:2], "LC_MESSAGES", dom+"."+ext)
|
filename = filepath.Join(l.path, l.lang[:2], "LC_MESSAGES", dom+"."+ext)
|
||||||
if _, err := os.Stat(filename); err == nil {
|
if _, err := os.Stat(filename); err == nil {
|
||||||
return filename
|
return filename
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
filename = path.Join(l.path, l.lang, dom+"."+ext)
|
filename = filepath.Join(l.path, l.lang, dom+"."+ext)
|
||||||
if _, err := os.Stat(filename); err == nil {
|
if _, err := os.Stat(filename); err == nil {
|
||||||
return filename
|
return filename
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(l.lang) > 2 {
|
if len(l.lang) > 2 {
|
||||||
filename = path.Join(l.path, l.lang[:2], dom+"."+ext)
|
filename = filepath.Join(l.path, l.lang[:2], dom+"."+ext)
|
||||||
if _, err := os.Stat(filename); err == nil {
|
if _, err := os.Stat(filename); err == nil {
|
||||||
return filename
|
return filename
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ package gotext
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -62,14 +62,14 @@ msgstr "More Translation"
|
|||||||
`
|
`
|
||||||
|
|
||||||
// Create Locales directory with simplified language code
|
// Create Locales directory with simplified language code
|
||||||
dirname := path.Join("/tmp", "en", "LC_MESSAGES")
|
dirname := filepath.Join("/tmp", "en", "LC_MESSAGES")
|
||||||
err := os.MkdirAll(dirname, os.ModePerm)
|
err := os.MkdirAll(dirname, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't create test directory: %s", err.Error())
|
t.Fatalf("Can't create test directory: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write PO content to file
|
// Write PO content to file
|
||||||
filename := path.Join(dirname, "my_domain.po")
|
filename := filepath.Join(dirname, "my_domain.po")
|
||||||
|
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -192,14 +192,14 @@ msgstr "More Translation"
|
|||||||
`
|
`
|
||||||
|
|
||||||
// Create Locales directory with simplified language code
|
// Create Locales directory with simplified language code
|
||||||
dirname := path.Join("/tmp", "en", "LC_MESSAGES")
|
dirname := filepath.Join("/tmp", "en", "LC_MESSAGES")
|
||||||
err := os.MkdirAll(dirname, os.ModePerm)
|
err := os.MkdirAll(dirname, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't create test directory: %s", err.Error())
|
t.Fatalf("Can't create test directory: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write PO content to file
|
// Write PO content to file
|
||||||
filename := path.Join(dirname, "my_domain.po")
|
filename := filepath.Join(dirname, "my_domain.po")
|
||||||
|
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -425,14 +425,14 @@ msgstr[2] "And this is the second plural form: %s"
|
|||||||
`
|
`
|
||||||
|
|
||||||
// Create Locales directory with simplified language code
|
// Create Locales directory with simplified language code
|
||||||
dirname := path.Join("/tmp", "es")
|
dirname := filepath.Join("/tmp", "es")
|
||||||
err := os.MkdirAll(dirname, os.ModePerm)
|
err := os.MkdirAll(dirname, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Can't create test directory: %s", err.Error())
|
t.Fatalf("Can't create test directory: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write PO content to file
|
// Write PO content to file
|
||||||
filename := path.Join(dirname, "race.po")
|
filename := filepath.Join(dirname, "race.po")
|
||||||
|
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user