error-pages
error-pages copied to clipboard
Can we add the possibility of inline translation using Accept-Language header?
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the problem to be solved
The current translation (l10n) works fine but it bothers me that the l10n.js is added at the bottom of the page, async and deferred. My connection is not slow and I can see the page blinking
I would love to implement this, but I'm a newbie with go
Suggest a solution
I think a very simple solution would be:
On startup we read the translations, which are already in l10n.js
, and store it in a hash
On errorpage.go
we initialize the props as usual (props.Message = page.Message()
), after this we check the Accept-Header for the language codes, sort by weight and get the first that matches one of the available languages, if the language is not detected or is en
we just skip the translation, if different than en
we call a function that just returns the value for the key message, just as the l10n already does. Then just for caching we add the lang-code to props
Additional context
My pseudo code would be something like this:
LiveTranslation.parse('translations.json')
if page, exists := cfg.Pages[pageCode]; exists {
props.Message = page.Message()
props.Description = page.Description()
} else if c, err := strconv.Atoi(pageCode); err == nil {
if s := fasthttp.StatusMessage(c); s != "Unknown Status Code" { // as a fallback
props.Message = s
}
}
selected_lang = LiveTranslation.parse_and_match( string(ctx.Request.Header.Peek("Accept-Language")) )
if selected_lang != "en" && selected_lang != null {
for name, value in props {
props .name = LiveTranslation.translate(value, selected_lang)
}
props.Lang = selected_lang
}