Skip to content

Commit 6a56a87

Browse files
committed
go embed fs with path
1 parent c7c2135 commit 6a56a87

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module github.com/leonelquinteros/gotext
22

3-
// go: no requirements found in Gopkg.lock
4-
53
require golang.org/x/tools v0.1.12
64

7-
go 1.13
5+
go 1.16

locale.go

+27-20
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,29 @@ multiple languages at the same time by working with this object.
2121
2222
Example:
2323
24-
import (
25-
"encoding/gob"
26-
"bytes"
27-
"fmt"
28-
"github.com/leonelquinteros/gotext"
29-
)
24+
import (
25+
"encoding/gob"
26+
"bytes"
27+
"fmt"
28+
"github.com/leonelquinteros/gotext"
29+
)
3030
31-
func main() {
32-
// Create Locale with library path and language code
33-
l := gotext.NewLocale("/path/to/i18n/dir", "en_US")
31+
func main() {
32+
// Create Locale with library path and language code
33+
l := gotext.NewLocale("/path/to/i18n/dir", "en_US")
3434
35-
// Load domain '/path/to/i18n/dir/en_US/LC_MESSAGES/default.{po,mo}'
36-
l.AddDomain("default")
35+
// Load domain '/path/to/i18n/dir/en_US/LC_MESSAGES/default.{po,mo}'
36+
l.AddDomain("default")
3737
38-
// Translate text from default domain
39-
fmt.Println(l.Get("Translate this"))
38+
// Translate text from default domain
39+
fmt.Println(l.Get("Translate this"))
4040
41-
// Load different domain ('/path/to/i18n/dir/en_US/LC_MESSAGES/extras.{po,mo}')
42-
l.AddDomain("extras")
43-
44-
// Translate text from domain
45-
fmt.Println(l.GetD("extras", "Translate this"))
46-
}
41+
// Load different domain ('/path/to/i18n/dir/en_US/LC_MESSAGES/extras.{po,mo}')
42+
l.AddDomain("extras")
4743
44+
// Translate text from domain
45+
fmt.Println(l.GetD("extras", "Translate this"))
46+
}
4847
*/
4948
type Locale struct {
5049
// Path to locale files.
@@ -83,6 +82,14 @@ func NewLocaleFS(l string, filesystem fs.FS) *Locale {
8382
return loc
8483
}
8584

85+
// NewLocaleFSWithPath returns a Locale working with a fs.FS on a p path folder.
86+
func NewLocaleFSWithPath(l string, filesystem fs.FS, p string) *Locale {
87+
loc := NewLocale("", l)
88+
loc.fs = filesystem
89+
loc.path = p
90+
return loc
91+
}
92+
8693
func (l *Locale) findExt(dom, ext string) string {
8794
filename := path.Join(l.path, l.lang, "LC_MESSAGES", dom+"."+ext)
8895
if l.fileExists(filename) {
@@ -333,7 +340,7 @@ func (l *Locale) GetNDC(dom, str, plural string, n int, ctx string, vars ...inte
333340
return Printf(plural, vars...)
334341
}
335342

336-
//GetTranslations returns a copy of all translations in all domains of this locale. It does not support contexts.
343+
// GetTranslations returns a copy of all translations in all domains of this locale. It does not support contexts.
337344
func (l *Locale) GetTranslations() map[string]*Translation {
338345
all := make(map[string]*Translation)
339346

locale_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package gotext
77

88
import (
9+
"embed"
910
"os"
1011
"path"
1112
"testing"
@@ -703,6 +704,9 @@ func TestLocaleBinaryEncoding(t *testing.T) {
703704
}
704705
}
705706

707+
//go:embed fixtures
708+
var localeFS embed.FS
709+
706710
func TestLocale_GetTranslations(t *testing.T) {
707711
var locales []*Locale
708712
{ // test os
@@ -711,6 +715,9 @@ func TestLocale_GetTranslations(t *testing.T) {
711715
{ // test fs
712716
locales = append(locales, NewLocaleFS("en_US", os.DirFS("fixtures")))
713717
}
718+
{ // test embed
719+
locales = append(locales, NewLocaleFSWithPath("en_US", localeFS, "fixtures"))
720+
}
714721

715722
for _, l := range locales {
716723
l.AddDomain("default")

0 commit comments

Comments
 (0)