From 16b8f5de0bca6c25226164949df17dc7fec6ad58 Mon Sep 17 00:00:00 2001 From: Darko Luketic Date: Tue, 6 Sep 2016 16:18:05 +0200 Subject: [PATCH] license & added middlewarefunc --- LICENSE | 2 +- i18n.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 4e939aa..9377d5c 100644 --- a/LICENSE +++ b/LICENSE @@ -7,6 +7,6 @@ Redistribution and use in source and binary forms, with or without modification, 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -3. Use or distribution of this code is disallowed for the github.com user kataras in any representation. +3. Use or redistribution of this code is disallowed for the github.com user kataras in any representation and use for the iris project or related projects in particular. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/i18n.go b/i18n.go index 4e59dcd..22fa7b4 100644 --- a/i18n.go +++ b/i18n.go @@ -72,4 +72,29 @@ func (i *I18nMiddleware) Middleware(next http.Handler) http.Handler { ctx3 := context.WithValue(ctx2, "i18nTfunc", i.config.bundle.MustTfunc(lang, rlang, i.config.DefaultLanguage)) next.ServeHTTP(w, r.WithContext(ctx3)) }) +} + +func (i *I18nMiddleware) MiddlewareFunc(next http.Handler) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + bycookie := false + lang := r.URL.Query().Get(i.config.URLParam) + rlang := r.Header.Get("Accept-Language") + if lang == "" { + lc ,e := r.Cookie("lang") + if e != nil { + lang = "" + } else { + lang = lc.Value + bycookie = true + } + } + if !bycookie { + http.SetCookie(w, &http.Cookie{HttpOnly:true, Name: "lang", Value:lang}) + } + ctx0 := context.WithValue(r.Context(), "i18nlang", lang) + ctx1 := context.WithValue(ctx0, "i18nrlang", rlang) + ctx2 := context.WithValue(ctx1, "i18ndlang", i.config.DefaultLanguage) + ctx3 := context.WithValue(ctx2, "i18nTfunc", i.config.bundle.MustTfunc(lang, rlang, i.config.DefaultLanguage)) + next.ServeHTTP(w, r.WithContext(ctx3)) + }) } \ No newline at end of file