diff --git a/lang/loader.go b/lang/loader.go index 785b24b..a1489e9 100644 --- a/lang/loader.go +++ b/lang/loader.go @@ -2,12 +2,24 @@ package lang import ( "errors" + "go/build" "io/ioutil" + "os" + "path/filepath" ) // LoadLocaleText will load the translations from the locale json files according to the locale func LoadLocaleText(l string) ([]byte, error) { lText, err := ioutil.ReadFile("./lang/" + l + ".json") + if os.IsNotExist(err) { + gopath := os.Getenv("GOPATH") + if gopath == "" { + gopath = build.Default.GOPATH + } + lPath := filepath.Join(gopath, "src", "github.com", "uniplaces", "carbon", "lang", l + ".json") + lText, err = ioutil.ReadFile(lPath) + } + if err != nil { return nil, errors.New("not able to read the lang file:" + err.Error()) } diff --git a/translator_test.go b/translator_test.go index 7f9308c..8e2f6f5 100644 --- a/translator_test.go +++ b/translator_test.go @@ -1,6 +1,7 @@ package carbon import ( + "strings" "testing" "time" @@ -18,3 +19,10 @@ func TestTrans(t *testing.T) { c.SetLocale("pt") assert.Equal(t, "pt", c.GetLocale()) } + +func TestLoadNonExistentResource(t *testing.T) { + tr := NewTranslator() + err := tr.loadResource("iv") + assert.NotNil(t, err) + assert.True(t, strings.Contains(err.Error(), "github.com")) +}