license & added middlewarefunc

This commit is contained in:
Darko Luketic 2016-09-06 16:18:05 +02:00
parent 82744058ff
commit 16b8f5de0b
2 changed files with 26 additions and 1 deletions

View File

@ -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.

25
i18n.go
View File

@ -73,3 +73,28 @@ func (i *I18nMiddleware) Middleware(next http.Handler) http.Handler {
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))
})
}