Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Fix language, use go:embed #76

Merged
merged 3 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.13, 1.14, 1.15]
go-version: [1.16, 1.17, 1.18]

runs-on: ubuntu-latest

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Carbon
======
[![Build Status](https://travis-ci.org/uniplaces/carbon.svg?branch=master)](https://travis-ci.org/uniplaces/carbon)
[![Build Status](https://github.com/uniplaces/carbon/actions/workflows/pull_request.yml/badge.svg)](https://github.com/uniplaces/carbon/actions/workflows/pull_request.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/uniplaces/carbon)](https://goreportcard.com/report/github.com/uniplaces/carbon)
[![codecov](https://codecov.io/gh/uniplaces/carbon/branch/master/graph/badge.svg)](https://codecov.io/gh/uniplaces/carbon)
[![GoDoc](https://godoc.org/github.com/uniplaces/carbon?status.svg)](https://godoc.org/github.com/uniplaces/carbon)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/uniplaces/carbon

go 1.15
go 1.16

require (
github.com/davecgh/go-spew v1.1.0 // indirect
Expand Down
18 changes: 5 additions & 13 deletions lang/loader.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package lang

import (
"embed"
"errors"
"go/build"
"io/ioutil"
"os"
"path/filepath"
)

//go:embed *.json
var f embed.FS

// 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)
}
lText, err := f.ReadFile(l + ".json")

if err != nil {
return nil, errors.New("not able to read the lang file:" + err.Error())
Expand Down
4 changes: 2 additions & 2 deletions translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func (t *Translator) AssertValidLocale(l string) error {

// loadResource loads the translations according to the locale
func (t *Translator) loadResource(l string) error {
ltext, err := lang.LoadLocaleText(l)
lText, err := lang.LoadLocaleText(l)
if err != nil {
return err
}

err = json.Unmarshal(ltext, &t.resources)
err = json.Unmarshal(lText, &t.resources)
if err != nil {
return errors.New("unable to unmarshall locale data : " + err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func TestLoadNonExistentResource(t *testing.T) {
tr := NewTranslator()
err := tr.loadResource("iv")
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "github.com"))
assert.True(t, strings.Contains(err.Error(), "open iv.json: file does not exist"))
}