iris icon indicating copy to clipboard operation
iris copied to clipboard

Missing RFC2616 "Accept-Language" support by the i18n middleware

Open joernlenoch opened this issue 9 years ago • 4 comments

Hi!

Hope that I'm submitting to the correct repository.

Started used iris quite a while back and wanted to implement the i18n support today. Unfortunately, I had to notice that the middleware does not support the RFC2616 standard for the "Accept-Language" request header and therefore lacks an important feature for modern browsers.

Source https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html (Section 14.4)

Problem Incoming request with the "Accept-Language" header like de,de-DE:q=0.8,en-US;q=0.4 is not properly used for validating the language preferred by the user. The middleware simply forwards the full header which is not recognizable by unkwons i18n toolkit

///  "kataras/iris/middleware/i18n/i18n.go, line 33ff
if langHeader := ctx.RequestHeader("Accept-Language"); i18n.IsExist(langHeader) {
	language = langHeader
}

The following is a possible solution, which still ignores the given property q and does not fix the issue with replacement IETF language tags (e.g. en instead of en-US) - latter should be fixed by unkwon.

// try to get by the request headers
langHeader := ctx.RequestHeader("Accept-Language")
  if len(langHeader) > 0 {
    for _, langEntry := range strings.Split(langHeader, ",") {
      parts := strings.Split(langEntry, ";")
      if i18n.IsExist(parts[0]) {
        language = parts[0]
        break
      }
    }
  }

I can provide a proper merge request if you like.

Kind Regards,

joernlenoch avatar Aug 01 '17 11:08 joernlenoch

@j-lenoch You've right, you can push the solution, it will be approved, nice work!

Note:

latter should be fixed by unkwon.

GO to kataras/iris/vendor/unkwon and apply the changes there too, we can update it internally, I keep these changes on notes, so it will never lose its internal changes even if we upgrade the vendors.

kataras avatar Aug 01 '17 11:08 kataras

Actually this is not as scary as it sounds, I'll push a fix tomorrow if you don't until then

hiveminded avatar Aug 01 '17 19:08 hiveminded

Certainly - ~especially as you are moving iris over to ion.~ And even then, it is just a minor feature that prevents me (and hopefully some others) from developing this addon themselves.

See https://github.com/kataras/iris/pull/697

joernlenoch avatar Aug 01 '17 22:08 joernlenoch

Great job @j-lenoch and for the record, no we are not moving iris over to ion, iris is the only one active repository, read the history file when you'll get some time off.

hiveminded avatar Aug 02 '17 00:08 hiveminded