github.com/polarismesh/polaris@v1.17.8/apiserver/httpserver/i18n/translate_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package i18n
    19  
    20  import (
    21  	"testing"
    22  
    23  	"golang.org/x/text/language"
    24  
    25  	api "github.com/polarismesh/polaris/common/api/v1"
    26  )
    27  
    28  func init() {
    29  	LoadI18nMessageFile("../../../release/conf/i18n/en.toml")
    30  	LoadI18nMessageFile("../../../release/conf/i18n/zh.toml")
    31  }
    32  
    33  func Test_Translate(t *testing.T) {
    34  	type args struct {
    35  		lang    language.Tag
    36  		errCode uint32
    37  		want    string
    38  	}
    39  
    40  	tests := []args{
    41  		{
    42  			lang:    language.Chinese,
    43  			errCode: api.ExecuteSuccess,
    44  			want:    "执行成功",
    45  		},
    46  		{
    47  			lang:    language.English,
    48  			errCode: api.ExecuteSuccess,
    49  			want:    "execute success",
    50  		},
    51  		{
    52  			lang:    language.English, // 未知errcode, 则不翻译,即为空字符串
    53  			errCode: 0,
    54  			want:    "",
    55  		},
    56  		{
    57  			lang:    language.Japanese, // 未知的语言, 走默认值, 即英语
    58  			errCode: api.ExecuteSuccess,
    59  			want:    "execute success",
    60  		},
    61  	}
    62  	for _, testItem := range tests {
    63  		if msg, _ := Translate(testItem.errCode, testItem.lang.String()); msg != testItem.want {
    64  			t.Errorf("i18.Translate() = %v, want %v", msg, testItem.want)
    65  		}
    66  	}
    67  
    68  }