Skip to content

Commit

Permalink
[RFC] Fix language, use go:embed (#76)
Browse files Browse the repository at this point in the history
* Fix language, use go:embed

* Update readme with new badge

* Set badge name to Build Status
  • Loading branch information
rntdrts authored Apr 16, 2022
1 parent a000e35 commit f18c311
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 19 deletions.
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"))
}

0 comments on commit f18c311

Please sign in to comment.