use go:embed directive to include lang files

This commit is contained in:
gouguoyin 2021-09-06 11:12:54 +08:00
parent 3bc4624c5f
commit e65bec090b
2 changed files with 52 additions and 35 deletions

View File

@ -1,14 +1,28 @@
package carbon
import (
"embed"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"gitee.com/golang-package/carbon/lang"
)
//go:embed lang
var fs embed.FS
var (
// defaultDir default directory
defaultDir = "lang"
// defaultLocale default locale
defaultLocale = "en"
// invalidLocaleError returns an invalid locale error.
invalidLocaleError = func(locale string) error {
return fmt.Errorf("invalid locale file %q, please make sure the json file exists and is valid", locale)
}
)
// Language define a Language struct.
@ -17,12 +31,14 @@ type Language struct {
dir string
locale string
resources map[string]string
Error error
}
// NewLanguage return a new Language instance.
// 初始化 Language 结构体
func NewLanguage() *Language {
return &Language{
dir: defaultDir,
locale: defaultLocale,
resources: make(map[string]string),
}
@ -30,45 +46,51 @@ func NewLanguage() *Language {
// SetLocale sets language locale.
// 设置区域
func (l *Language) SetLocale(locale string) error {
if len(l.resources) != 0 {
return nil
func (lang *Language) SetLocale(locale string) {
if len(lang.resources) != 0 {
return
}
l.locale = locale
resources, err := lang.LoadLocale(locale)
lang.locale = locale
fileName := lang.dir + string(os.PathSeparator) + locale + ".json"
bytes, err := fs.ReadFile(fileName)
if err != nil {
return err
lang.Error = invalidLocaleError(fileName)
}
if json.Unmarshal(bytes, &lang.resources) != nil {
lang.Error = invalidLocaleError(fileName)
}
l.resources = resources
return nil
}
// SetResources sets language resources.
// 设置资源
func (l *Language) SetResources(resources map[string]string) {
if len(l.resources) == 0 {
l.resources = resources
func (lang *Language) SetResources(resources map[string]string) {
if len(lang.resources) == 0 {
lang.resources = resources
return
}
for k, v := range resources {
if _, ok := l.resources[k]; ok {
l.resources[k] = v
if _, ok := lang.resources[k]; ok {
lang.resources[k] = v
}
}
}
// translate translates by unit string.
// 翻译转换
func (l *Language) translate(unit string, number int64) string {
if len(l.resources) == 0 {
l.SetLocale(defaultLocale)
func (lang *Language) translate(unit string, vaule int64) string {
if len(lang.resources) == 0 {
lang.SetLocale(defaultLocale)
}
slice := strings.Split(l.resources[unit], "|")
slice := strings.Split(lang.resources[unit], "|")
number := getAbsValue(vaule)
if len(slice) == 1 {
return strings.Replace(slice[0], "%d", strconv.FormatInt(number, 10), 1)
return strings.Replace(slice[0], "%d", strconv.FormatInt(vaule, 10), 1)
}
if int64(len(slice)) <= number {
return strings.Replace(slice[len(slice)-1], "%d", strconv.FormatInt(number, 10), 1)
return strings.Replace(slice[len(slice)-1], "%d", strconv.FormatInt(vaule, 10), 1)
}
return strings.Replace(slice[number-1], "%d", strconv.FormatInt(number, 10), 1)
if !strings.Contains(slice[number-1], "%d") && vaule < 0 {
return "-" + slice[number-1]
}
return strings.Replace(slice[number-1], "%d", strconv.FormatInt(vaule, 10), 1)
}

View File

@ -1,7 +1,6 @@
package carbon
import (
"fmt"
"strconv"
"testing"
@ -12,25 +11,21 @@ func TestLanguage_SetLocale(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected error // 期望值
input1 Carbon // 输入值
input2 string // 输入值
expected string // 期望值
}{
{"en", nil},
{"zh-CN", nil},
{Now(), "en", "1 day after"},
{Tomorrow(), "zh-CN", "1 天后"},
}
for index, test := range tests {
assert.ErrorIs(test.expected, NewLanguage().SetLocale(test.input), "Current test index is "+strconv.Itoa(index))
lang := NewLanguage()
lang.SetLocale(test.input2)
assert.Equal(test.expected, (test.input1).AddDays(1).SetLanguage(lang).DiffForHumans(test.input1), "Current test index is "+strconv.Itoa(index))
}
}
func TestLangError_SetLocale(t *testing.T) {
locale, lang := "xxx", NewLanguage()
expected := fmt.Errorf("invalid locale %q, please see the directory %q for all valid locales", locale, "./lang/")
actual := lang.SetLocale(locale)
assert.Equal(t, expected, actual, "It should catch an exception in SetLocale()")
}
func TestLanguage_SetResources1(t *testing.T) {
assert := assert.New(t)