github.com/gogf/gf@v1.16.9/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 "context" 11 12 const ( 13 ctxLanguage = "I18nLanguage" 14 ) 15 16 // WithLanguage append language setting to the context and returns a new context. 17 func WithLanguage(ctx context.Context, language string) context.Context { 18 if ctx == nil { 19 ctx = context.TODO() 20 } 21 return context.WithValue(ctx, ctxLanguage, language) 22 } 23 24 // LanguageFromCtx retrieves and returns language name from context. 25 // It returns an empty string if it is not set previously. 26 func LanguageFromCtx(ctx context.Context) string { 27 if ctx == nil { 28 return "" 29 } 30 v := ctx.Value(ctxLanguage) 31 if v != nil { 32 return v.(string) 33 } 34 return "" 35 }