-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #373 from Skyenought/feat/i18n
✨ Add fiberi18n middleware
- Loading branch information
Showing
19 changed files
with
789 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name-template: 'Fiberi18n - v$RESOLVED_VERSION' | ||
tag-template: 'fiberi18n/v$RESOLVED_VERSION' | ||
tag-prefix: fiberi18n/v | ||
include-paths: | ||
- fiberi18n | ||
categories: | ||
- title: '🚀 New' | ||
labels: | ||
- '✏️ Feature' | ||
- title: '🧹 Updates' | ||
labels: | ||
- '🧹 Updates' | ||
- '🤖 Dependencies' | ||
- title: '🐛 Fixes' | ||
labels: | ||
- '☢️ Bug' | ||
- title: '📚 Documentation' | ||
labels: | ||
- '📒 Documentation' | ||
change-template: '- $TITLE (#$NUMBER)' | ||
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks. | ||
version-resolver: | ||
major: | ||
labels: | ||
- 'major' | ||
minor: | ||
labels: | ||
- 'minor' | ||
- '✏️ Feature' | ||
patch: | ||
labels: | ||
- 'patch' | ||
- '📒 Documentation' | ||
- '☢️ Bug' | ||
- '🤖 Dependencies' | ||
- '🧹 Updates' | ||
default: patch | ||
template: | | ||
$CHANGES | ||
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...fiberi18n/v$RESOLVED_VERSION | ||
Thank you $CONTRIBUTORS for making this update possible. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Release Drafter Fiberi18n | ||
on: | ||
push: | ||
# branches to consider in the event; optional, defaults to all | ||
branches: | ||
- master | ||
- main | ||
paths: | ||
- 'fiberi18n/**' | ||
jobs: | ||
draft_release_fiberi18n: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 30 | ||
steps: | ||
- uses: release-drafter/release-drafter@v5 | ||
with: | ||
config-name: release-drafter-fiberi18n.yml | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Fiberi18n | ||
|
||
 | ||
[](https://gofiber.io/discord) | ||
 | ||
 | ||
 | ||
|
||
[go-i18n](https://github.com/nicksnyder/go-i18n) support for Fiber. | ||
|
||
### Install | ||
|
||
This middleware supports Fiber v2. | ||
|
||
``` | ||
go get -u github.com/gofiber/fiber/v2 | ||
go get -u github.com/gofiber/contrib/fiberi18n | ||
``` | ||
|
||
### Signature | ||
|
||
``` | ||
fiberi18n.New(config ...*Config) fiber.Handler | ||
``` | ||
|
||
### Config | ||
|
||
| Property | Type | Description | Default | | ||
| ---------------- | ------------------------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ||
| Next | `func(c *fiber.Ctx) bool` | A function to skip this middleware when returned `true`. | `nil` | | ||
| RootPath | `string` | The i18n template folder path. | `"./example/localize"` | | ||
| AcceptLanguages | `[]language.Tag` | A collection of languages that can be processed. | `[]language.Tag{language.Chinese, language.English}` | | ||
| FormatBundleFile | `string` | The type of the template file. | `"yaml"` | | ||
| DefaultLanguage | `language.Tag` | The default returned language type. | `language.English` | | ||
| Loader | `Loader` | The implementation of the Loader interface, which defines how to read the file. We provide both os.ReadFile and embed.FS.ReadFile. | `LoaderFunc(os.ReadFile)` | | ||
| UnmarshalFunc | `i18n.UnmarshalFunc` | The function used for decoding template files. | `yaml.Unmarshal` | | ||
| LangHandler | `func(ctx *fiber.Ctx, defaultLang string) string` | Used to get the kind of language handled by *fiber.Ctx and defaultLang. | Retrieved from the request header `Accept-Language` or query parameter `lang`. | | ||
|
||
### Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gofiber/contrib/fiberi18n" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/nicksnyder/go-i18n/v2/i18n" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
app.Use( | ||
fiberi18n.New(&fiberi18n.Config{ | ||
RootPath: "./example/localize", | ||
AcceptLanguages: []language.Tag{language.Chinese, language.English}, | ||
DefaultLanguage: language.Chinese, | ||
}), | ||
) | ||
app.Get("/", func(c *fiber.Ctx) error { | ||
return c.SendString(fiberi18n.MustGetMessage("welcome")) | ||
}) | ||
app.Get("/:name", func(ctx *fiber.Ctx) error { | ||
return ctx.SendString(fiberi18n.MustGetMessage(&i18n.LocalizeConfig{ | ||
MessageID: "welcomeWithName", | ||
TemplateData: map[string]string{ | ||
"name": ctx.Params("name"), | ||
}, | ||
})) | ||
}) | ||
app.Listen("127.0.0.1:3000") | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package fiberi18n | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/nicksnyder/go-i18n/v2/i18n" | ||
"golang.org/x/text/language" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type Config struct { | ||
// Next defines a function to skip this middleware when returned true. | ||
// | ||
// Optional. Default: nil | ||
Next func(c *fiber.Ctx) bool | ||
|
||
// RootPath is i18n template folder path | ||
// | ||
// Default: ./example/localize | ||
RootPath string | ||
|
||
// AcceptLanguages is a collection of languages that can be processed | ||
// | ||
// Optional. Default: []language.Tag{language.Chinese, language.English} | ||
AcceptLanguages []language.Tag | ||
|
||
// FormatBundleFile is type of template file. | ||
// | ||
// Optional. Default: "yaml" | ||
FormatBundleFile string | ||
|
||
// DefaultLanguage is the default returned language type | ||
// | ||
// Optional. Default: language.English | ||
DefaultLanguage language.Tag | ||
|
||
// Loader implements the Loader interface, which defines how to read the file. | ||
// We provide both os.ReadFile and embed.FS.ReadFile | ||
// Optional. Default: LoaderFunc(os.ReadFile) | ||
Loader Loader | ||
|
||
// UnmarshalFunc for decoding template files | ||
// | ||
// Optional. Default: yaml.Unmarshal | ||
UnmarshalFunc i18n.UnmarshalFunc | ||
|
||
// LangHandler is used to get the kind of language handled by *fiber.Ctx and defaultLang | ||
// | ||
// Optional. Default: The language type is retrieved from the request header: `Accept-Language` or query param : `lang` | ||
LangHandler func(ctx *fiber.Ctx, defaultLang string) string | ||
|
||
ctx *fiber.Ctx | ||
bundle *i18n.Bundle | ||
localizerMap map[string]*i18n.Localizer | ||
} | ||
|
||
type Loader interface { | ||
LoadMessage(path string) ([]byte, error) | ||
} | ||
|
||
type LoaderFunc func(path string) ([]byte, error) | ||
|
||
func (f LoaderFunc) LoadMessage(path string) ([]byte, error) { | ||
return f(path) | ||
} | ||
|
||
var ConfigDefault = &Config{ | ||
RootPath: "./example/localize", | ||
DefaultLanguage: language.English, | ||
AcceptLanguages: []language.Tag{language.Chinese, language.English}, | ||
FormatBundleFile: "yaml", | ||
UnmarshalFunc: yaml.Unmarshal, | ||
Loader: LoaderFunc(os.ReadFile), | ||
LangHandler: func(c *fiber.Ctx, defaultLang string) string { | ||
if c == nil { | ||
return defaultLang | ||
} | ||
lang := c.Get("Accept-Language") | ||
if lang != "" { | ||
return lang | ||
} | ||
lang = c.Query("lang") | ||
if lang == "" { | ||
return defaultLang | ||
} | ||
return lang | ||
}, | ||
} | ||
|
||
func configDefault(config ...*Config) *Config { | ||
// Return default config if nothing provided | ||
if len(config) == 0 { | ||
return ConfigDefault | ||
} | ||
|
||
// Override default config | ||
cfg := config[0] | ||
|
||
if cfg.Next == nil { | ||
cfg.Next = ConfigDefault.Next | ||
} | ||
|
||
if cfg.RootPath == "" { | ||
cfg.RootPath = ConfigDefault.RootPath | ||
} | ||
|
||
if cfg.DefaultLanguage == language.Und { | ||
cfg.DefaultLanguage = ConfigDefault.DefaultLanguage | ||
} | ||
|
||
if cfg.UnmarshalFunc == nil { | ||
cfg.UnmarshalFunc = ConfigDefault.UnmarshalFunc | ||
} | ||
|
||
if cfg.FormatBundleFile == "" { | ||
cfg.FormatBundleFile = ConfigDefault.FormatBundleFile | ||
} | ||
|
||
if cfg.AcceptLanguages == nil { | ||
cfg.AcceptLanguages = ConfigDefault.AcceptLanguages | ||
} | ||
|
||
if cfg.Loader == nil { | ||
cfg.Loader = ConfigDefault.Loader | ||
} | ||
|
||
if cfg.UnmarshalFunc == nil { | ||
cfg.UnmarshalFunc = ConfigDefault.UnmarshalFunc | ||
} | ||
|
||
if cfg.LangHandler == nil { | ||
cfg.LangHandler = ConfigDefault.LangHandler | ||
} | ||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build go1.16 | ||
|
||
package fiberi18n | ||
|
||
import "embed" | ||
|
||
type EmbedLoader struct { | ||
FS embed.FS | ||
} | ||
|
||
func (e *EmbedLoader) LoadMessage(path string) ([]byte, error) { | ||
return e.FS.ReadFile(path) | ||
} |
Oops, something went wrong.