github.com/wangyougui/gf/v2@v2.6.5/os/gview/gview_z_unit_i18n_test.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/wangyougui/gf.
     6  
     7  package gview_test
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  
    13  	"github.com/wangyougui/gf/v2/debug/gdebug"
    14  	"github.com/wangyougui/gf/v2/frame/g"
    15  	"github.com/wangyougui/gf/v2/i18n/gi18n"
    16  	"github.com/wangyougui/gf/v2/os/gctx"
    17  	"github.com/wangyougui/gf/v2/os/gfile"
    18  	"github.com/wangyougui/gf/v2/os/gview"
    19  	"github.com/wangyougui/gf/v2/test/gtest"
    20  )
    21  
    22  func Test_I18n(t *testing.T) {
    23  	gtest.C(t, func(t *gtest.T) {
    24  		content := `{{.name}} says "{#hello}{#world}!"`
    25  		expect1 := `john says "你好世界!"`
    26  		expect2 := `john says "こんにちは世界!"`
    27  		expect3 := `john says "{#hello}{#world}!"`
    28  
    29  		g.I18n().SetPath(gtest.DataPath("i18n"))
    30  
    31  		g.I18n().SetLanguage("zh-CN")
    32  		result1, err := g.View().ParseContent(context.TODO(), content, g.Map{
    33  			"name": "john",
    34  		})
    35  		t.AssertNil(err)
    36  		t.Assert(result1, expect1)
    37  
    38  		g.I18n().SetLanguage("ja")
    39  		result2, err := g.View().ParseContent(context.TODO(), content, g.Map{
    40  			"name": "john",
    41  		})
    42  		t.AssertNil(err)
    43  		t.Assert(result2, expect2)
    44  
    45  		g.I18n().SetLanguage("none")
    46  		result3, err := g.View().ParseContent(context.TODO(), content, g.Map{
    47  			"name": "john",
    48  		})
    49  		t.AssertNil(err)
    50  		t.Assert(result3, expect3)
    51  	})
    52  	gtest.C(t, func(t *gtest.T) {
    53  		content := `{{.name}} says "{#hello}{#world}!"`
    54  		expect1 := `john says "你好世界!"`
    55  		expect2 := `john says "こんにちは世界!"`
    56  		expect3 := `john says "{#hello}{#world}!"`
    57  
    58  		g.I18n().SetPath(gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n")
    59  
    60  		result1, err := g.View().ParseContent(context.TODO(), content, g.Map{
    61  			"name":         "john",
    62  			"I18nLanguage": "zh-CN",
    63  		})
    64  		t.AssertNil(err)
    65  		t.Assert(result1, expect1)
    66  
    67  		result2, err := g.View().ParseContent(context.TODO(), content, g.Map{
    68  			"name":         "john",
    69  			"I18nLanguage": "ja",
    70  		})
    71  		t.AssertNil(err)
    72  		t.Assert(result2, expect2)
    73  
    74  		result3, err := g.View().ParseContent(context.TODO(), content, g.Map{
    75  			"name":         "john",
    76  			"I18nLanguage": "none",
    77  		})
    78  		t.AssertNil(err)
    79  		t.Assert(result3, expect3)
    80  	})
    81  	// gi18n manager is nil
    82  	gtest.C(t, func(t *gtest.T) {
    83  		content := `{{.name}} says "{#hello}{#world}!"`
    84  		expect1 := `john says "{#hello}{#world}!"`
    85  
    86  		g.I18n().SetPath(gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n")
    87  
    88  		view := gview.New()
    89  		view.SetI18n(nil)
    90  		result1, err := view.ParseContent(context.TODO(), content, g.Map{
    91  			"name":         "john",
    92  			"I18nLanguage": "zh-CN",
    93  		})
    94  		t.AssertNil(err)
    95  		t.Assert(result1, expect1)
    96  	})
    97  	// SetLanguage in context
    98  	gtest.C(t, func(t *gtest.T) {
    99  		content := `{{.name}} says "{#hello}{#world}!"`
   100  		expect1 := `john says "你好世界!"`
   101  		ctx := gctx.New()
   102  		g.I18n().SetPath(gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n")
   103  		ctx = gi18n.WithLanguage(ctx, "zh-CN")
   104  		t.Log(gi18n.LanguageFromCtx(ctx))
   105  
   106  		view := gview.New()
   107  
   108  		result1, err := view.ParseContent(ctx, content, g.Map{
   109  			"name": "john",
   110  		})
   111  		t.AssertNil(err)
   112  		t.Assert(result1, expect1)
   113  	})
   114  
   115  }