Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature additional methods #63

Merged
merged 18 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 96 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,40 @@ func main() {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(ctx, "welcome"))
})

router.GET("/:name", func(ctx *gin.Context) {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(
ctx,
&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": ctx.Param("name"),
},
}))
router.GET("/messageId/:name", func(context *gin.Context) {
context.String(http.StatusOK, MustGetMessage(context, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": context.Param("name"),
},
}))
})

router.GET("/messageType/:name", func(context *gin.Context) {
context.String(http.StatusOK, MustGetMessage(context, &i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "welcomeWithName",
},
TemplateData: map[string]string{
"name": context.Param("name"),
},
}))
})

router.GET("/exist/:lang", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "%v", ginI18n.HasLang(ctx, ctx.Param("lang")))
})

// get the default and current language
router.GET("/lang/default", func(context *gin.Context) {
context.String(http.StatusOK, "%s", GetDefaultLanguage(context).String())
})

// get the current language
router.GET("/lang/current", func(context *gin.Context) {
context.String(http.StatusOK, "%s", GetCurrentLanguage(context).String())
})

if err := router.Run(":8080"); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -97,17 +120,40 @@ func main() {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(ctx, "welcome"))
})

router.GET("/:name", func(ctx *gin.Context) {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(
ctx,
&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": ctx.Param("name"),
},
}))
router.GET("/messageId/:name", func(context *gin.Context) {
context.String(http.StatusOK, MustGetMessage(context, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": context.Param("name"),
},
}))
})

router.GET("/messageType/:name", func(context *gin.Context) {
context.String(http.StatusOK, MustGetMessage(context, &i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "welcomeWithName",
},
TemplateData: map[string]string{
"name": context.Param("name"),
},
}))
})

router.GET("/exist/:lang", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "%v", ginI18n.HasLang(ctx, ctx.Param("lang")))
})

// get the default and current language
router.GET("/lang/default", func(context *gin.Context) {
context.String(http.StatusOK, "%s", GetDefaultLanguage(context).String())
})

// get the current language
router.GET("/lang/current", func(context *gin.Context) {
context.String(http.StatusOK, "%s", GetCurrentLanguage(context).String())
})

if err := router.Run(":8080"); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -150,17 +196,40 @@ func main() {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(ctx, "welcome"))
})

router.GET("/:name", func(ctx *gin.Context) {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(
ctx,
&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": ctx.Param("name"),
},
}))
router.GET("/messageId/:name", func(context *gin.Context) {
context.String(http.StatusOK, MustGetMessage(context, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": context.Param("name"),
},
}))
})

router.GET("/messageType/:name", func(context *gin.Context) {
context.String(http.StatusOK, MustGetMessage(context, &i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "welcomeWithName",
},
TemplateData: map[string]string{
"name": context.Param("name"),
},
}))
})

router.GET("/exist/:lang", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "%v", ginI18n.HasLang(ctx, ctx.Param("lang")))
})

// get the default and current language
router.GET("/lang/default", func(context *gin.Context) {
context.String(http.StatusOK, "%s", GetDefaultLanguage(context).String())
})

// get the current language
router.GET("/lang/current", func(context *gin.Context) {
context.String(http.StatusOK, "%s", GetCurrentLanguage(context).String())
})

if err := router.Run(":8080"); err != nil {
log.Fatal(err)
}
Expand Down
64 changes: 63 additions & 1 deletion ginI18n.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ginI18nImpl is an implementation of the GinI18n interface, providing
// Package i18n ginI18nImpl is an implementation of the GinI18n interface, providing
// localization support for Gin applications. It uses the go-i18n library
// to manage and retrieve localized messages.
//
Expand All @@ -11,6 +11,9 @@
// Methods:
// - GetMessage: Retrieves a localized message based on the provided context and parameter.
// - MustGetMessage: Retrieves a localized message and returns an empty string if retrieval fails.
// - HasLang: Checks if a specific language is supported.
// - GetCurrentLanguage: Retrieves the current language based on the Gin context..
// - GetDefaultLanguage: Retrieves the default language
// - SetBundle: Sets the i18n.Bundle configuration.
// - SetGetLngHandler: Sets the handler function to retrieve the language tag from the Gin context.
// - loadMessageFiles: Loads all localization files into the bundle.
Expand Down Expand Up @@ -42,6 +45,55 @@ type ginI18nImpl struct {
getLngHandler GetLngHandler
}

// GetDefaultLanguage retrieves the default language tag for the application.
//
// This method returns the default language tag that is used when no specific
// language is specified by the client or in the context.
//
// Parameters:
// - ctx: The Gin context from which to retrieve the message.
//
// Returns:
// - language.Tag: The default language tag.
func (i *ginI18nImpl) GetDefaultLanguage() language.Tag {
return i.defaultLanguage
}

// GetCurrentLanguage retrieves the current language tag from the Gin context.
//
// This method extracts the language tag from the Gin context using the provided
// `getLngHandler` function. It uses this handler to obtain the language tag for
// the current request. If the language is not provided, it returns the default language.
//
// Parameters:
// - ctx: The Gin context from which to retrieve the message.
//
// Returns:
// - language.Tag: The language tag based on the context (either the specified language or the default language).
func (i *ginI18nImpl) GetCurrentLanguage(context *gin.Context) language.Tag {
return language.Make(i.getLngHandler(context, i.defaultLanguage.String()))
}

// HasLang checks whether the specified language is supported by the application.
//
// This method checks if a language tag is available in the localizer map (`localizerByLng`),
// which stores localizers for all supported languages. If the language is supported,
// it returns `true`; otherwise, it returns `false`.
//
// Parameters:
// - ctx: The Gin context from which to retrieve the message.
// - language (string): The language tag (e.g., "en", "zh") to check.
//
// Returns:
// - bool: `true` if the language is supported, otherwise `false`.
func (i *ginI18nImpl) HasLang(language string) bool {
if _, exist := i.localizerByLng[language]; exist {
return true
}

return false
}

// GetMessage retrieves a localized message based on the provided context and parameter.
// If the message cannot be retrieved, it returns an empty string.
//
Expand Down Expand Up @@ -183,6 +235,16 @@ func (i *ginI18nImpl) getLocalizeConfig(param interface{}) (*i18n.LocalizeConfig
MessageID: paramValue,
}
return localizeConfig, nil
case *i18n.Message:
localizeConfig := &i18n.LocalizeConfig{
DefaultMessage: paramValue,
}
return localizeConfig, nil
case i18n.Message:
localizeConfig := &i18n.LocalizeConfig{
DefaultMessage: &paramValue,
}
return localizeConfig, nil
case *i18n.LocalizeConfig:
return paramValue, nil
case i18n.LocalizeConfig:
Expand Down
30 changes: 30 additions & 0 deletions i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package i18n

import (
"github.com/gin-gonic/gin"
"golang.org/x/text/language"
)

// newI18n ...
Expand Down Expand Up @@ -66,3 +67,32 @@ func MustGetMessage(context *gin.Context, param interface{}) string {
atI18n := context.MustGet("i18n").(GinI18n)
return atI18n.MustGetMessage(context, param)
}

// HasLang check all i18n lang exists
// Example:
// HasLang(context, "ZH-cn") // return false or true
func HasLang(context *gin.Context, language string) bool {
atI18n := context.MustGet("i18n").(GinI18n)
return atI18n.HasLang(language)
}

// GetDefaultLanguage get the default language
// Example:
// GetDefaultLanguage(context)
func GetDefaultLanguage(context *gin.Context) language.Tag {
atI18n := context.MustGet("i18n").(GinI18n)
return atI18n.GetDefaultLanguage()
}

// GetCurrentLanguage get the current language
// Example:
// GetCurrentLanguage(context)
func GetCurrentLanguage(context *gin.Context) language.Tag {
atI18n := context.MustGet("i18n").(GinI18n)
return atI18n.GetCurrentLanguage(context)
}

// I18n get GinI18n from gin.Context
func I18n(context *gin.Context) GinI18n {
return context.MustGet("i18n").(GinI18n)
}
Loading
Loading