github.com/gogf/gf/v2@v2.7.4/i18n/gi18n/gi18n_ctx.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 gi18n implements internationalization and localization. 8 package gi18n 9 10 import ( 11 "context" 12 13 "github.com/gogf/gf/v2/os/gctx" 14 ) 15 16 const ( 17 ctxLanguage gctx.StrKey = "I18nLanguage" 18 ) 19 20 // WithLanguage append language setting to the context and returns a new context. 21 func WithLanguage(ctx context.Context, language string) context.Context { 22 if ctx == nil { 23 ctx = context.TODO() 24 } 25 return context.WithValue(ctx, ctxLanguage, language) 26 } 27 28 // LanguageFromCtx retrieves and returns language name from context. 29 // It returns an empty string if it is not set previously. 30 func LanguageFromCtx(ctx context.Context) string { 31 if ctx == nil { 32 return "" 33 } 34 v := ctx.Value(ctxLanguage) 35 if v != nil { 36 return v.(string) 37 } 38 return "" 39 }