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

Can we add the possibility of inline translation using Accept-Language header? #156

Open
1 task done
NicosKaralis opened this issue Nov 28, 2022 · 0 comments
Open
1 task done
Assignees
Labels
type:feature_request Feature request

Comments

@NicosKaralis
Copy link

Is there an existing issue for this?

  • 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature_request Feature request
Projects
None yet
Development

No branches or pull requests

2 participants