1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-01 17:27:03 +00:00

fixes, i18n, sitemap generator and new examples

Former-commit-id: 54801dc705ee0fa66232f65063f8a68c9cc31921
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-12-13 23:06:18 +02:00
parent c66f7a6d21
commit 04477c3e41
58 changed files with 1474 additions and 1311 deletions

View File

@@ -17,6 +17,9 @@ type Application interface {
// Logger returns the golog logger instance(pointer) that is being used inside the "app".
Logger() *golog.Logger
// I18nReadOnly returns the i18n's read-only features.
I18nReadOnly() I18nReadOnly
// View executes and write the result of a template file to the writer.
//
// Use context.View to render templates to the client instead.

View File

@@ -67,15 +67,9 @@ type ConfigurationReadOnly interface {
// Defaults to 32MB or 32 << 20 if you prefer.
GetPostMaxMemory() int64
// GetTranslateLanguageContextKey returns the configuration's TranslateFunctionContextKey value,
// used for i18n inside templates.
GetTranslateFunctionContextKey() string
// GetTranslateLangFunctionContextKey returns the configuration's TranslateLangFunctionContextKey value,
// used for i18n inside templates.
GetTranslateLangFunctionContextKey() string
// GetTranslateLanguageContextKey returns the configuration's TranslateLanguageContextKey value,
// used for i18n.
GetTranslateLanguageContextKey() string
// GetTranslateLanguageContextKey returns the configuration's LocaleContextKey value,
// used for i18n. Defaults to "iris.locale".
GetLocaleContextKey() string
// GetViewLayoutContextKey returns the key of the context's user values' key
// which is being used to set the template

View File

@@ -76,7 +76,7 @@ func (u UnmarshalerFunc) Unmarshal(data []byte, v interface{}) error {
return u(data, v)
}
// Context is the midle-man server's "object" for the clients.
// Context is the midle-man server's "object" dealing with incoming requests.
//
// A New context is being acquired from a sync.Pool on each connection.
// The Context is the most important thing on the iris's http flow.
@@ -292,21 +292,9 @@ type Context interface {
// You can use this function to Set and Get local values
// that can be used to share information between handlers and middleware.
Values() *memstore.Store
// Translate is the i18n (localization) middleware's function,
// it calls the Values().Get(ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey())
// to execute the translate function and returns the current localized text value.
//
// Example: https://github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n
Translate(format string, args ...interface{}) string
// TranslateLang is the i18n (localization) middleware's function,
// it calls the Values().Get(ctx.Application().ConfigurationReadOnly().GetTranslateLangFunctionContextKey())
// to execute the translate function and returns the localized text value based on the "lang".
//
// Example: https://github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n
TranslateLang(lang, format string, args ...interface{}) string
// +------------------------------------------------------------+
// | Path, Host, Subdomain, IP, Headers etc... |
// | Path, Host, Subdomain, IP, Headers, Localization etc... |
// +------------------------------------------------------------+
// Method returns the request.Method, the client's http method to the server.
@@ -365,6 +353,14 @@ type Context interface {
// in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
// or by the URL query parameter "referer".
GetReferrer() Referrer
// GetLocale returns the current request's `Locale` found by i18n middleware.
// See `Tr` too.
GetLocale() Locale
// Tr returns a i18n localized message based on format with optional arguments.
// See `GetLocale` too.
// Example: https://github.com/kataras/iris/tree/master/_examples/i18n
Tr(format string, args ...interface{}) string
// +------------------------------------------------------------+
// | Headers helpers |
// +------------------------------------------------------------+
@@ -1503,32 +1499,6 @@ func (ctx *context) Values() *memstore.Store {
return &ctx.values
}
// Translate is the i18n (localization) middleware's function,
// it calls the Values().Get(ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey())
// to execute the translate function and return the current localized text value.
//
// Example: https://github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n
func (ctx *context) Translate(format string, args ...interface{}) string {
if cb, ok := ctx.values.Get(ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey()).(func(string, ...interface{}) string); ok {
return cb(format, args...)
}
return ""
}
// TranslateLang is the i18n (localization) middleware's function,
// it calls the Values().Get(ctx.Application().ConfigurationReadOnly().GetTranslateLangFunctionContextKey())
// to execute the translate function and returns the localized text value based on the "lang".
//
// Example: https://github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n
func (ctx *context) TranslateLang(lang, format string, args ...interface{}) string {
if cb, ok := ctx.values.Get(ctx.Application().ConfigurationReadOnly().GetTranslateLangFunctionContextKey()).(func(string, string, ...interface{}) string); ok {
return cb(lang, format, args...)
}
return ""
}
// +------------------------------------------------------------+
// | Path, Host, Subdomain, IP, Headers etc... |
// +------------------------------------------------------------+
@@ -1797,6 +1767,36 @@ func (ctx *context) GetReferrer() Referrer {
return emptyReferrer
}
// GetLocale returns the current request's `Locale` found by i18n middleware.
// See `Tr` too.
func (ctx *context) GetLocale() Locale {
contextKey := ctx.app.ConfigurationReadOnly().GetLocaleContextKey()
if v := ctx.Values().Get(contextKey); v != nil {
if locale, ok := v.(Locale); ok {
return locale
}
}
if locale := ctx.Application().I18nReadOnly().GetLocale(ctx); locale != nil {
ctx.Values().Set(contextKey, locale)
return locale
}
return nil
}
// Tr returns a i18n localized message based on format with optional arguments.
// See `GetLocale` too.
//
// Example: https://github.com/kataras/iris/tree/master/_examples/i18n
func (ctx *context) Tr(format string, args ...interface{}) string { // other name could be: Localize.
if locale := ctx.GetLocale(); locale != nil { // TODO: here... I need to change the logic, if not found then call the i18n's get locale and set the value in order to be fastest on routes that are not using (no need to reigster a middleware.)
return locale.GetMessage(format, args...)
}
return fmt.Sprintf(format, args...)
}
// +------------------------------------------------------------+
// | Response Headers helpers |
// +------------------------------------------------------------+

28
context/i18n.go Normal file
View File

@@ -0,0 +1,28 @@
package context
import "golang.org/x/text/language"
// I18nReadOnly is the interface which ontains the read-only i18n features.
// Read the "i18n" package fo details.
type I18nReadOnly interface {
Tags() []language.Tag
GetLocale(ctx Context) Locale
Tr(lang string, format string, args ...interface{}) string
}
// Locale is the interface which returns from a `Localizer.GetLocale` metod.
// It serves the transltions based on "key" or format. See `GetMessage`.
type Locale interface {
// Index returns the current locale index from the languages list.
Index() int
// Tag returns the full language Tag attached tothis Locale,
// it should be uniue across different Locales.
Tag() *language.Tag
// Language should return the exact languagecode of this `Locale`
//that the user provided on `New` function.
//
// Same as `Tag().String()` but it's static.
Language() string
// GetMessage should return translated text based n the given "key".
GetMessage(key string, args ...interface{}) string
}

View File

@@ -5,6 +5,7 @@ import (
"path"
"path/filepath"
"strings"
"time"
"github.com/kataras/iris/v12/macro"
)
@@ -56,6 +57,15 @@ type RouteReadOnly interface {
// route, manually or automatic by the framework,
// get the route by `Application#GetRouteByPath(staticSite.RequestPath)`.
StaticSites() []StaticSite
// Sitemap properties: https://www.sitemaps.org/protocol.html
// GetLastMod returns the date of last modification of the file served by this route.
GetLastMod() time.Time
// GetChangeFreq returns the the page frequently is likely to change.
GetChangeFreq() string
// GetPriority returns the priority of this route's URL relative to other URLs on your site.
GetPriority() float32
}
// StaticSite is a structure which is used as field on the `Route`