github.com/gogf/gf/v2@v2.7.4/os/gview/gview_i18n.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gview 8 9 import ( 10 "context" 11 12 "github.com/gogf/gf/v2/i18n/gi18n" 13 "github.com/gogf/gf/v2/util/gconv" 14 ) 15 16 const ( 17 i18nLanguageVariableName = "I18nLanguage" 18 ) 19 20 // i18nTranslate translate the content with i18n feature. 21 func (view *View) i18nTranslate(ctx context.Context, content string, variables Params) string { 22 if view.config.I18nManager != nil { 23 // Compatible with old version. 24 if language, ok := variables[i18nLanguageVariableName]; ok { 25 ctx = gi18n.WithLanguage(ctx, gconv.String(language)) 26 } 27 return view.config.I18nManager.T(ctx, content) 28 } 29 return content 30 } 31 32 // setI18nLanguageFromCtx retrieves language name from context and sets it to template variables map. 33 func (view *View) setI18nLanguageFromCtx(ctx context.Context, variables map[string]interface{}) { 34 if _, ok := variables[i18nLanguageVariableName]; !ok { 35 if language := gi18n.LanguageFromCtx(ctx); language != "" { 36 variables[i18nLanguageVariableName] = language 37 } 38 } 39 }