Skip to content

Commit

Permalink
fix: lang path (#510)
Browse files Browse the repository at this point in the history
* fix: lang path issue

* remove

* add golang 1.22
  • Loading branch information
hwbrzzl authored Jun 13, 2024
1 parent c87227d commit c5e2d40
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 97 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ jobs:
test:
strategy:
matrix:
go:
- "1.21"
go: ["1.21", "1.22"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -28,8 +27,7 @@ jobs:
windows:
strategy:
matrix:
go:
- "1.21"
go: ["1.21", "1.22"]
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
Expand Down
87 changes: 1 addition & 86 deletions translation/file_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package translation

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,8 +15,7 @@ import (

type FileLoaderTestSuite struct {
suite.Suite
executable string
json foundation.Json
json foundation.Json
}

func TestFileLoaderTestSuite(t *testing.T) {
Expand All @@ -30,21 +28,8 @@ func TestFileLoaderTestSuite(t *testing.T) {
assert.Nil(t, file.Create(restrictedFilePath, `{"foo": "restricted"}`))
assert.Nil(t, os.Chmod(restrictedFilePath, 0000))

// path2
executable, err := executablePath()
assert.NoError(t, err)
assert.Nil(t, file.Create(filepath.Join(executable, "lang/en/test.json"), `{"foo": "bar", "baz": {"foo": "bar"}}`))
assert.Nil(t, file.Create(filepath.Join(executable, "lang/en/another/test.json"), `{"foo": "backagebar", "baz": "backagesplash"}`))
assert.Nil(t, file.Create(filepath.Join(executable, "lang/another/en/test.json"), `{"foo": "backagebar", "baz": "backagesplash"}`))
assert.Nil(t, file.Create(filepath.Join(executable, "lang/en/invalid/test.json"), `{"foo": "bar",}`))
assert.Nil(t, file.Create(filepath.Join(executable, "lang/cn.json"), `{"foo": "bar", "baz": {"foo": "bar"}}`))
restrictedFilePath = filepath.Join(executable, "lang/en/restricted/test.json")
assert.Nil(t, file.Create(restrictedFilePath, `{"foo": "restricted"}`))
assert.Nil(t, os.Chmod(restrictedFilePath, 0000))

suite.Run(t, &FileLoaderTestSuite{})
assert.Nil(t, file.Remove("lang"))
assert.Nil(t, file.Remove(filepath.Join(executable, "lang")))
}

func (f *FileLoaderTestSuite) SetupTest() {
Expand Down Expand Up @@ -111,73 +96,3 @@ func (f *FileLoaderTestSuite) TestLoadInvalidJSON() {
f.Error(err)
f.Nil(translations)
}

func (f *FileLoaderTestSuite) TestLoadByExecutable() {
paths := []string{filepath.Join(f.executable, "lang")}
loader := NewFileLoader(paths, f.json)
translations, err := loader.Load("en", "test")
f.NoError(err)
f.NotNil(translations)
f.Equal("bar", translations["foo"])
f.Equal("bar", translations["baz"].(map[string]any)["foo"])

translations, err = loader.Load("cn", "*")
f.NoError(err)
f.NotNil(translations)
f.Equal("bar", translations["foo"])
f.Equal("bar", translations["baz"].(map[string]any)["foo"])

paths = []string{filepath.Join(f.executable, "lang", "another"), filepath.Join(f.executable, "lang")}
loader = NewFileLoader(paths, f.json)
translations, err = loader.Load("en", "test")
f.NoError(err)
f.NotNil(translations)
f.Equal("backagebar", translations["foo"])

paths = []string{filepath.Join(f.executable, "lang")}
loader = NewFileLoader(paths, f.json)
translations, err = loader.Load("en", "another/test")
f.NoError(err)
f.NotNil(translations)
f.Equal("backagebar", translations["foo"])

paths = []string{filepath.Join(f.executable, "lang")}
loader = NewFileLoader(paths, f.json)
translations, err = loader.Load("en", "restricted/test")
if env.IsWindows() {
f.NoError(err)
f.NotNil(translations)
f.Equal("restricted", translations["foo"])
} else {
f.Error(err)
f.Nil(translations)
}
}

func (f *FileLoaderTestSuite) TestLoadNonExistentFileByExecutable() {
paths := []string{filepath.Join(f.executable, "lang")}
loader := NewFileLoader(paths, f.json)
translations, err := loader.Load("hi", "test")

f.Error(err)
f.Nil(translations)
f.Equal(ErrFileNotExist, err)
}

func (f *FileLoaderTestSuite) TestLoadInvalidJSONByExecutable() {
paths := []string{filepath.Join(f.executable, "lang")}
loader := NewFileLoader(paths, f.json)
translations, err := loader.Load("en", "invalid/test")

f.Error(err)
f.Nil(translations)
}

func executablePath() (string, error) {
executable, err := os.Executable()
if err != nil {
return "", err
}

return filepath.Dir(executable), nil
}
8 changes: 1 addition & 7 deletions translation/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package translation

import (
"context"
"path/filepath"

"github.com/goravel/framework/contracts/foundation"
)
Expand All @@ -14,16 +13,11 @@ type ServiceProvider struct {

func (translation *ServiceProvider) Register(app foundation.Application) {
app.BindWith(Binding, func(app foundation.Application, parameters map[string]any) (any, error) {
executable, err := app.ExecutablePath()
if err != nil {
return nil, err
}

config := app.MakeConfig()
logger := app.MakeLog()
locale := config.GetString("app.locale")
fallback := config.GetString("app.fallback_locale")
loader := NewFileLoader([]string{filepath.Join(executable, "lang")}, app.GetJson())
loader := NewFileLoader([]string{"lang"}, app.GetJson())
trans := NewTranslator(parameters["ctx"].(context.Context), loader, locale, fallback, logger)

return trans, nil
Expand Down

0 comments on commit c5e2d40

Please sign in to comment.