From 8eb140f8b5faa80dd78f5dca460b34c7b06475de Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Thu, 4 Jan 2024 15:18:57 +0800 Subject: [PATCH] lib: refine library to text --- i18n/library.go | 25 ------------------------- i18n/library_test.go | 35 ----------------------------------- i18n/text.go | 11 +++++++++++ i18n/text_test.go | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 60 deletions(-) delete mode 100644 i18n/library.go delete mode 100644 i18n/library_test.go diff --git a/i18n/library.go b/i18n/library.go deleted file mode 100644 index 6acb5d62d..000000000 --- a/i18n/library.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2009-present, Alibaba Cloud All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package i18n - -func T(en string, zh string) *Text { - t := &Text{ - dic: make(map[string]string), - } - t.dic["en"] = en - if zh != "" { - t.dic["zh"] = zh - } - return t -} diff --git a/i18n/library_test.go b/i18n/library_test.go deleted file mode 100644 index 9f75e1cf8..000000000 --- a/i18n/library_test.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2009-present, Alibaba Cloud All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package i18n - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestLibrary(t *testing.T) { - //Test T(en string, zh string)*Text - text := T("hello", "你好") - assert.Equal(t, "hello", text.dic["en"]) - assert.Equal(t, "你好", text.dic["zh"]) - - text = T("", "你好") - assert.Equal(t, "", text.dic["en"]) - assert.Equal(t, "你好", text.dic["zh"]) - - text = T("hello", "") - assert.Equal(t, "hello", text.dic["en"]) - assert.Equal(t, "", text.dic["zh"]) -} diff --git a/i18n/text.go b/i18n/text.go index 40a952df7..faa25b82d 100644 --- a/i18n/text.go +++ b/i18n/text.go @@ -13,6 +13,17 @@ // limitations under the License. package i18n +func T(en string, zh string) *Text { + t := &Text{ + dic: make(map[string]string), + } + t.dic["en"] = en + if zh != "" { + t.dic["zh"] = zh + } + return t +} + type Text struct { dic map[string]string } diff --git a/i18n/text_test.go b/i18n/text_test.go index 9f80f56de..540aff0cc 100644 --- a/i18n/text_test.go +++ b/i18n/text_test.go @@ -33,3 +33,18 @@ func TestText(t *testing.T) { language = "zh" assert.Equal(t, "你好", tx.Text()) } + +func TestLibrary(t *testing.T) { + //Test T(en string, zh string)*Text + text := T("hello", "你好") + assert.Equal(t, "hello", text.dic["en"]) + assert.Equal(t, "你好", text.dic["zh"]) + + text = T("", "你好") + assert.Equal(t, "", text.dic["en"]) + assert.Equal(t, "你好", text.dic["zh"]) + + text = T("hello", "") + assert.Equal(t, "hello", text.dic["en"]) + assert.Equal(t, "", text.dic["zh"]) +}