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 package carbon
import ( import (
"embed"
"encoding/json"
"fmt"
"os"
"strconv" "strconv"
"strings" "strings"
"gitee.com/golang-package/carbon/lang"
) )
//go:embed lang
var fs embed.FS
var ( var (
// defaultDir default directory
defaultDir = "lang"
// defaultLocale default locale
defaultLocale = "en" 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. // Language define a Language struct.
@ -17,12 +31,14 @@ type Language struct {
dir string dir string
locale string locale string
resources map[string]string resources map[string]string
Error error
} }
// NewLanguage return a new Language instance. // NewLanguage return a new Language instance.
// 初始化 Language 结构体 // 初始化 Language 结构体
func NewLanguage() *Language { func NewLanguage() *Language {
return &Language{ return &Language{
dir: defaultDir,
locale: defaultLocale, locale: defaultLocale,
resources: make(map[string]string), resources: make(map[string]string),
} }
@ -30,45 +46,51 @@ func NewLanguage() *Language {
// SetLocale sets language locale. // SetLocale sets language locale.
// 设置区域 // 设置区域
func (l *Language) SetLocale(locale string) error { func (lang *Language) SetLocale(locale string) {
if len(l.resources) != 0 { if len(lang.resources) != 0 {
return nil return
} }
l.locale = locale lang.locale = locale
resources, err := lang.LoadLocale(locale) fileName := lang.dir + string(os.PathSeparator) + locale + ".json"
bytes, err := fs.ReadFile(fileName)
if err != nil { 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. // SetResources sets language resources.
// 设置资源 // 设置资源
func (l *Language) SetResources(resources map[string]string) { func (lang *Language) SetResources(resources map[string]string) {
if len(l.resources) == 0 { if len(lang.resources) == 0 {
l.resources = resources lang.resources = resources
return return
} }
for k, v := range resources { for k, v := range resources {
if _, ok := l.resources[k]; ok { if _, ok := lang.resources[k]; ok {
l.resources[k] = v lang.resources[k] = v
} }
} }
} }
// translate translates by unit string. // translate translates by unit string.
// 翻译转换 // 翻译转换
func (l *Language) translate(unit string, number int64) string { func (lang *Language) translate(unit string, vaule int64) string {
if len(l.resources) == 0 { if len(lang.resources) == 0 {
l.SetLocale(defaultLocale) lang.SetLocale(defaultLocale)
} }
slice := strings.Split(l.resources[unit], "|") slice := strings.Split(lang.resources[unit], "|")
number := getAbsValue(vaule)
if len(slice) == 1 { 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 { 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 package carbon
import ( import (
"fmt"
"strconv" "strconv"
"testing" "testing"
@ -12,25 +11,21 @@ func TestLanguage_SetLocale(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
input string // 输入值 input1 Carbon // 输入值
expected error // 期望值 input2 string // 输入值
expected string // 期望值
}{ }{
{"en", nil}, {Now(), "en", "1 day after"},
{"zh-CN", nil}, {Tomorrow(), "zh-CN", "1 天后"},
} }
for index, test := range tests { 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) { func TestLanguage_SetResources1(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)