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 REQUEST] Log when I18n Translation Fails? #1593

Closed
AlbinoGeek opened this issue Aug 16, 2020 · 2 comments
Closed

[FEATURE REQUEST] Log when I18n Translation Fails? #1593

AlbinoGeek opened this issue Aug 16, 2020 · 2 comments

Comments

@AlbinoGeek
Copy link

Summary

A call to ctx.Tr() with an invalid or mistyped key might go unnoticed if it does not log the "key not found" somewhere.

There are many reasons this could happen:

  • Perhaps something new was added in one language, but not in another?
  • Perhaps someone simply fat-fingered or typo'd a translation key name.
  • Maybe you were using dynamic key assembly in a template.
  • Maybe the end-user had access to the translation files (a CRM)
@kataras
Copy link
Owner

kataras commented Aug 18, 2020

Hello @AlbinoGeek, I pushed a commit which gives you a lot of info and you are able to both log and inject a value there too. Example:

// Use DefaultMessageFunc to customize the return value of a not found key or lang.
// All language inputs fallback to the default locale if not matched.
// This is why this one accepts both input and matched languages,
// so the caller can be more expressful knowing those.
// Defaults to nil.
app.I18n.DefaultMessageFunc = func(langInput, langMatched, key string, args ...interface{}) string {
msg := fmt.Sprintf("user language input: %s: matched as: %s: not found key: %s: args: %v", langInput, langMatched, key, args)
app.Logger().Warn(msg)
return msg
}
// Load i18n when customizations are set in place.
//
// First parameter: Glob filpath patern,
// Second variadic parameter: Optional language tags, the first one is the default/fallback one.
err := app.I18n.Load("./locales/*/*.ini", "en-US", "el-GR", "zh-CN")
if err != nil {
panic(err)
}
app.Get("/not-matched", func(ctx iris.Context) {
text := ctx.Tr("not_found_key", "some", "values", 42)
ctx.WriteString(text)
// user language input: en-gb: matched as: en-US: not found key: not_found_key: args: [some values 42]
})

@kataras kataras added this to the v12.2.0 milestone Aug 18, 2020
@AlbinoGeek
Copy link
Author

That's awesome, I would say that Resolved this request, as I now have control to log or dynamically generate if required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants