Skip to content

Commit

Permalink
Merge pull request #10 from fasmat/feature/upgrade-mw-to-use-iofs
Browse files Browse the repository at this point in the history
Upgrade i18mw package to use io/fs over packr/packd
  • Loading branch information
paganotoni committed Nov 23, 2021
2 parents abf6494 + 969c01e commit ce3e574
Show file tree
Hide file tree
Showing 30 changed files with 713 additions and 4,206 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
24 changes: 24 additions & 0 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: "Close stale issues and PRs"
on:
schedule:
- cron: "30 1 * * *"

jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v3
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: "This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days."
stale-pr-message: "This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days."
close-issue-message: "This issue was closed because it has been stalled for 5 days with no activity."
close-pr-message: "This PR was closed because it has been stalled for 10 days with no activity."
days-before-issue-stale: 30
days-before-issue-close: 5
days-before-pr-stale: 45
days-before-pr-close: 10
stale-issue-label: "no-issue-activity"
exempt-issue-labels: "help wanted,enhancement"
stale-pr-label: "no-pr-activity"
exempt-pr-labels: "dependencies,awaiting-approval,work-in-progress"
38 changes: 24 additions & 14 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
name: Tests
on: [push]
jobs:
on:
push:
branches:
- main
pull_request:

jobs:
tests-on:
name: ${{matrix.go-version}} ${{matrix.os}}
name: ${{ matrix.os }} - Go v${{ matrix.go-version }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
go-version: [1.12.x, 1.13.x]
os: [macos-latest, windows-latest, ubuntu-latest]
steps:
- name: Checkout Code
uses: actions/checkout@v1
with:
fetch-depth: 1
- name: Test
run: |
go mod tidy -v
go test -race ./...
go-version:
- "1.16.x"
- "1.17.x"
os:
- "macos-latest"
- "windows-latest"
- "ubuntu-latest"

steps:
- uses: actions/checkout@v2
- name: Setup Go ${{ matrix.go }}
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}

- name: Test
run: |
go mod tidy -v
go test -race ./...
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
i18n middleware
===============
# i18n middleware

This Buffalo middleware enables i18n features in your app:

* User language detection from configurable sources
* Translation helper using locales bundles from github.com/nicksnyder/go-i18n
* Localized views

Installation
------------
## Installation

This middleware is setup by default on a new Buffalo app:

**actions/app.go**
### actions/app.go

```go
var app *buffalo.App

Expand All @@ -26,16 +26,17 @@ func App() *buffalo.App {
// [...]

// Setup and use translations:
var err error
if T, err = i18n.New(packr.NewBox("../locales"), "en"); err != nil {
app.Stop(err)
}
app.Use(T.Middleware())
var err error
if T, err = i18n.New(os.DirFS("locales"), "en"); err != nil {
app.Stop(err)
}

app.Use(T.Middleware())
}
return app
}
```

Use `i18n.New` to create a new instance of the translation module, then add the middleware (`T.Middleware()`) to the app to enable its features.

See https://gobuffalo.io/docs/localization for further info about Buffalo translation features and configuration.
See <https://gobuffalo.io/docs/localization> for further info about Buffalo translation features and configuration.
19 changes: 11 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module github.com/gobuffalo/mw-i18n
module github.com/gobuffalo/mw-i18n/v2

go 1.13
go 1.16

require (
github.com/gobuffalo/buffalo v0.16.8
github.com/gobuffalo/httptest v1.5.0
github.com/gobuffalo/packd v1.0.0
github.com/pelletier/go-toml v1.6.0
github.com/stretchr/testify v1.5.1
gopkg.in/yaml.v2 v2.2.8
github.com/gobuffalo/buffalo v0.17.5
github.com/gobuffalo/httptest v1.5.1
github.com/nicksnyder/go-i18n v1.10.0
github.com/stretchr/testify v1.7.0
)

replace (
github.com/gobuffalo/buffalo v0.17.5 => github.com/fasmat/buffalo v0.16.15-0.20211121174727-77319a4a9d1a
github.com/gobuffalo/pop/v6 v6.0.0 => github.com/fasmat/pop/v6 v6.0.0-20211121174542-8ace23c76ee8
)
1,614 changes: 608 additions & 1,006 deletions go.sum

Large diffs are not rendered by default.

32 changes: 20 additions & 12 deletions i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package i18n

import (
"fmt"
"io/fs"
"path/filepath"
"sort"
"strings"

"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/mw-i18n/internal/go-i18n/i18n"
"github.com/gobuffalo/mw-i18n/internal/go-i18n/i18n/language"
"github.com/gobuffalo/mw-i18n/internal/go-i18n/i18n/translation"
"github.com/gobuffalo/packd"
"github.com/nicksnyder/go-i18n/i18n"
"github.com/nicksnyder/go-i18n/i18n/language"
"github.com/nicksnyder/go-i18n/i18n/translation"
)

// LanguageExtractor can be implemented for custom finding of search
Expand All @@ -24,8 +24,8 @@ type LanguageExtractorOptions map[string]interface{}

// Translator for handling all your i18n needs.
type Translator struct {
// Box - where are the files?
Box packd.Box
// FS that contains the files
FS fs.FS
// DefaultLanguage - default is passed as a parameter on New.
DefaultLanguage string
// HelperName - name of the view helper. default is "t"
Expand All @@ -36,10 +36,18 @@ type Translator struct {
LanguageExtractorOptions LanguageExtractorOptions
}

// Load translations from the t.Box.
// Load translations from the t.FS
func (t *Translator) Load() error {
return t.Box.Walk(func(path string, f packd.File) error {
b, err := t.Box.Find(path)
return fs.WalkDir(t.FS, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
return nil
}

b, err := fs.ReadFile(t.FS, path)
if err != nil {
return fmt.Errorf("unable to read locale file %s: %v", path, err)
}
Expand All @@ -62,12 +70,12 @@ func (t *Translator) AddTranslation(lang *language.Language, translations ...tra
i18n.AddTranslation(lang, translations...)
}

// New Translator. Requires a packr.Box that points to the location
// New Translator. Requires a fs.FS that points to the location
// of the translation files, as well as a default language. This will
// also call t.Load() and load the translations from disk.
func New(box packd.Box, language string) (*Translator, error) {
func New(fsys fs.FS, language string) (*Translator, error) {
t := &Translator{
Box: box,
FS: fsys,
DefaultLanguage: language,
HelperName: "t",
LanguageExtractorOptions: LanguageExtractorOptions{
Expand Down
30 changes: 3 additions & 27 deletions i18n_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package i18n_test

import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/gobuffalo/packd"

"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/httptest"
i18n "github.com/gobuffalo/mw-i18n"
i18n "github.com/gobuffalo/mw-i18n/v2"
"github.com/stretchr/testify/require"
)

Expand All @@ -23,35 +19,15 @@ type User struct {
LastName string
}

// makeBox builds an in-memory box for tests.
// This allows to drop the hard dependency on packr.
func makeBox(boxPath string) packd.Box {
box := packd.NewMemoryBox()
err := filepath.Walk(boxPath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return box.AddBytes(filepath.Base(path), content)
})
if err != nil {
panic(err)
}
return box
}

func app() *buffalo.App {
app := buffalo.New(buffalo.Options{})

r := render.New(render.Options{
TemplatesBox: makeBox("./templates"),
TemplatesFS: os.DirFS("templates"),
})

// Setup and use translations:
t, err := i18n.New(makeBox("./locales"), "en-US")
t, err := i18n.New(os.DirFS("locales"), "en-US")
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 0 additions & 5 deletions internal/go-i18n/CHANGELOG

This file was deleted.

19 changes: 0 additions & 19 deletions internal/go-i18n/LICENSE

This file was deleted.

0 comments on commit ce3e574

Please sign in to comment.