code.gitea.io/gitea@v1.19.3/modules/translation/i18n/i18n_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package i18n
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestLocaleStore(t *testing.T) {
    13  	testData1 := []byte(`
    14  .dot.name = Dot Name
    15  fmt = %[1]s %[2]s
    16  
    17  [section]
    18  sub = Sub String
    19  mixed = test value; <span style="color: red\; background: none;">more text</span>
    20  `)
    21  
    22  	testData2 := []byte(`
    23  fmt = %[2]s %[1]s
    24  
    25  [section]
    26  sub = Changed Sub String
    27  `)
    28  
    29  	ls := NewLocaleStore()
    30  	assert.NoError(t, ls.AddLocaleByIni("lang1", "Lang1", testData1, nil))
    31  	assert.NoError(t, ls.AddLocaleByIni("lang2", "Lang2", testData2, nil))
    32  	ls.SetDefaultLang("lang1")
    33  
    34  	result := ls.Tr("lang1", "fmt", "a", "b")
    35  	assert.Equal(t, "a b", result)
    36  
    37  	result = ls.Tr("lang2", "fmt", "a", "b")
    38  	assert.Equal(t, "b a", result)
    39  
    40  	result = ls.Tr("lang1", "section.sub")
    41  	assert.Equal(t, "Sub String", result)
    42  
    43  	result = ls.Tr("lang2", "section.sub")
    44  	assert.Equal(t, "Changed Sub String", result)
    45  
    46  	result = ls.Tr("", ".dot.name")
    47  	assert.Equal(t, "Dot Name", result)
    48  
    49  	result = ls.Tr("lang2", "section.mixed")
    50  	assert.Equal(t, `test value; <span style="color: red; background: none;">more text</span>`, result)
    51  
    52  	langs, descs := ls.ListLangNameDesc()
    53  	assert.Equal(t, []string{"lang1", "lang2"}, langs)
    54  	assert.Equal(t, []string{"Lang1", "Lang2"}, descs)
    55  
    56  	found := ls.Has("lang1", "no-such")
    57  	assert.False(t, found)
    58  	assert.NoError(t, ls.Close())
    59  }
    60  
    61  func TestLocaleStoreMoreSource(t *testing.T) {
    62  	testData1 := []byte(`
    63  a=11
    64  b=12
    65  `)
    66  
    67  	testData2 := []byte(`
    68  b=21
    69  c=22
    70  `)
    71  
    72  	ls := NewLocaleStore()
    73  	assert.NoError(t, ls.AddLocaleByIni("lang1", "Lang1", testData1, testData2))
    74  	assert.Equal(t, "11", ls.Tr("lang1", "a"))
    75  	assert.Equal(t, "21", ls.Tr("lang1", "b"))
    76  	assert.Equal(t, "22", ls.Tr("lang1", "c"))
    77  }