github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/translation/i18n/i18n_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package i18n
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func Test_Tr(t *testing.T) {
    15  	testData1 := []byte(`
    16  .dot.name = Dot Name
    17  fmt = %[1]s %[2]s
    18  
    19  [section]
    20  sub = Sub String
    21  mixed = test value; <span style="color: red\; background: none;">more text</span>
    22  `)
    23  
    24  	testData2 := []byte(`
    25  fmt = %[2]s %[1]s
    26  
    27  [section]
    28  sub = Changed Sub String
    29  `)
    30  
    31  	ls := NewLocaleStore()
    32  	assert.NoError(t, ls.AddLocaleByIni("lang1", "Lang1", testData1))
    33  	assert.NoError(t, ls.AddLocaleByIni("lang2", "Lang2", testData2))
    34  	ls.SetDefaultLang("lang1")
    35  
    36  	result := ls.Tr("lang1", "fmt", "a", "b")
    37  	assert.Equal(t, "a b", result)
    38  
    39  	result = ls.Tr("lang2", "fmt", "a", "b")
    40  	assert.Equal(t, "b a", result)
    41  
    42  	result = ls.Tr("lang1", "section.sub")
    43  	assert.Equal(t, "Sub String", result)
    44  
    45  	result = ls.Tr("lang2", "section.sub")
    46  	assert.Equal(t, "Changed Sub String", result)
    47  
    48  	result = ls.Tr("", ".dot.name")
    49  	assert.Equal(t, "Dot Name", result)
    50  
    51  	result = ls.Tr("lang2", "section.mixed")
    52  	assert.Equal(t, `test value; <span style="color: red; background: none;">more text</span>`, result)
    53  
    54  	langs, descs := ls.ListLangNameDesc()
    55  	assert.Equal(t, []string{"lang1", "lang2"}, langs)
    56  	assert.Equal(t, []string{"Lang1", "Lang2"}, descs)
    57  }