github.com/gogf/gf@v1.16.9/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  	"github.com/gogf/gf/i18n/gi18n"
    12  	"github.com/gogf/gf/util/gconv"
    13  )
    14  
    15  const (
    16  	i18nLanguageVariableName = "I18nLanguage"
    17  )
    18  
    19  // i18nTranslate translate the content with i18n feature.
    20  func (view *View) i18nTranslate(ctx context.Context, content string, variables Params) string {
    21  	if view.config.I18nManager != nil {
    22  		// Compatible with old version.
    23  		if language, ok := variables[i18nLanguageVariableName]; ok {
    24  			ctx = gi18n.WithLanguage(ctx, gconv.String(language))
    25  		}
    26  		return view.config.I18nManager.T(ctx, content)
    27  	}
    28  	return content
    29  }
    30  
    31  // setI18nLanguageFromCtx retrieves language name from context and sets it to template variables map.
    32  func (view *View) setI18nLanguageFromCtx(ctx context.Context, variables map[string]interface{}) {
    33  	if language, ok := variables[i18nLanguageVariableName]; !ok {
    34  		if language = gi18n.LanguageFromCtx(ctx); language != "" {
    35  			variables[i18nLanguageVariableName] = language
    36  		}
    37  	}
    38  }