2021-02-08 09:47:14 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
2021-09-06 11:12:54 +08:00
|
|
|
"embed"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-02-08 09:47:14 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-08-22 12:57:53 +08:00
|
|
|
"sync"
|
2021-02-08 09:47:14 +08:00
|
|
|
)
|
|
|
|
|
2021-09-06 11:12:54 +08:00
|
|
|
//go:embed lang
|
|
|
|
var fs embed.FS
|
|
|
|
|
2021-02-18 14:32:31 +08:00
|
|
|
var (
|
2022-10-29 14:32:23 +08:00
|
|
|
// default directory
|
|
|
|
// 默认目录
|
|
|
|
defaultDir = "lang/"
|
2021-09-06 11:12:54 +08:00
|
|
|
|
2022-10-29 14:32:23 +08:00
|
|
|
// default locale
|
|
|
|
// 默认区域
|
2021-02-18 14:32:31 +08:00
|
|
|
defaultLocale = "en"
|
2021-09-06 11:12:54 +08:00
|
|
|
|
2022-10-29 14:32:23 +08:00
|
|
|
// invalid locale error
|
|
|
|
// 无效的区域错误
|
2021-09-06 11:12:54 +08:00
|
|
|
invalidLocaleError = func(locale string) error {
|
|
|
|
return fmt.Errorf("invalid locale file %q, please make sure the json file exists and is valid", locale)
|
|
|
|
}
|
2021-02-18 14:32:31 +08:00
|
|
|
)
|
|
|
|
|
2021-09-08 13:40:41 +08:00
|
|
|
// Language defines a Language struct.
|
2021-08-10 10:58:43 +08:00
|
|
|
// 定义 Language 结构体
|
2021-02-08 09:47:14 +08:00
|
|
|
type Language struct {
|
2021-08-02 10:25:27 +08:00
|
|
|
dir string
|
|
|
|
locale string
|
|
|
|
resources map[string]string
|
2021-09-06 11:12:54 +08:00
|
|
|
Error error
|
2023-08-22 12:57:53 +08:00
|
|
|
rw *sync.RWMutex
|
2021-02-08 09:47:14 +08:00
|
|
|
}
|
|
|
|
|
2021-09-08 13:40:41 +08:00
|
|
|
// NewLanguage returns a new Language instance.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 初始化 Language 结构体
|
2021-02-08 09:47:14 +08:00
|
|
|
func NewLanguage() *Language {
|
|
|
|
return &Language{
|
2021-09-06 11:12:54 +08:00
|
|
|
dir: defaultDir,
|
2021-02-23 09:32:55 +08:00
|
|
|
locale: defaultLocale,
|
2021-02-08 09:47:14 +08:00
|
|
|
resources: make(map[string]string),
|
2023-08-22 12:57:53 +08:00
|
|
|
rw: new(sync.RWMutex),
|
2021-02-08 09:47:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:58:43 +08:00
|
|
|
// SetLocale sets language locale.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 设置区域
|
2021-09-06 11:12:54 +08:00
|
|
|
func (lang *Language) SetLocale(locale string) {
|
2023-08-22 12:57:53 +08:00
|
|
|
lang.rw.Lock()
|
|
|
|
defer lang.rw.Unlock()
|
|
|
|
|
2021-09-06 11:12:54 +08:00
|
|
|
if len(lang.resources) != 0 {
|
|
|
|
return
|
2021-02-08 09:47:14 +08:00
|
|
|
}
|
2021-09-06 11:12:54 +08:00
|
|
|
lang.locale = locale
|
2022-10-29 14:32:23 +08:00
|
|
|
fileName := lang.dir + locale + ".json"
|
2021-09-06 11:12:54 +08:00
|
|
|
bytes, err := fs.ReadFile(fileName)
|
2021-02-08 09:47:14 +08:00
|
|
|
if err != nil {
|
2021-09-06 11:12:54 +08:00
|
|
|
lang.Error = invalidLocaleError(fileName)
|
2021-09-10 11:44:39 +08:00
|
|
|
return
|
2021-09-06 11:12:54 +08:00
|
|
|
}
|
2023-08-22 12:57:53 +08:00
|
|
|
_ = json.Unmarshal(bytes, &lang.resources)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
2021-02-08 09:47:14 +08:00
|
|
|
|
2021-08-10 10:58:43 +08:00
|
|
|
// SetResources sets language resources.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 设置资源
|
2021-09-06 11:12:54 +08:00
|
|
|
func (lang *Language) SetResources(resources map[string]string) {
|
2023-08-22 12:57:53 +08:00
|
|
|
lang.rw.Lock()
|
|
|
|
defer lang.rw.Unlock()
|
|
|
|
|
2021-09-06 11:12:54 +08:00
|
|
|
if len(lang.resources) == 0 {
|
|
|
|
lang.resources = resources
|
2021-02-23 09:32:55 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, v := range resources {
|
2021-09-06 11:12:54 +08:00
|
|
|
if _, ok := lang.resources[k]; ok {
|
|
|
|
lang.resources[k] = v
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 14:32:23 +08:00
|
|
|
// returns a translated string.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 翻译转换
|
2021-09-10 11:55:20 +08:00
|
|
|
func (lang *Language) translate(unit string, value int64) string {
|
2021-09-06 11:12:54 +08:00
|
|
|
if len(lang.resources) == 0 {
|
|
|
|
lang.SetLocale(defaultLocale)
|
2021-08-02 10:25:27 +08:00
|
|
|
}
|
2021-09-06 11:12:54 +08:00
|
|
|
slice := strings.Split(lang.resources[unit], "|")
|
2021-09-10 11:55:20 +08:00
|
|
|
number := getAbsValue(value)
|
2021-02-23 09:32:55 +08:00
|
|
|
if len(slice) == 1 {
|
2021-09-10 11:55:20 +08:00
|
|
|
return strings.Replace(slice[0], "%d", strconv.FormatInt(value, 10), 1)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
2021-08-23 11:13:22 +08:00
|
|
|
if int64(len(slice)) <= number {
|
2021-09-10 11:55:20 +08:00
|
|
|
return strings.Replace(slice[len(slice)-1], "%d", strconv.FormatInt(value, 10), 1)
|
2021-09-06 11:12:54 +08:00
|
|
|
}
|
2021-09-10 11:55:20 +08:00
|
|
|
if !strings.Contains(slice[number-1], "%d") && value < 0 {
|
2021-09-06 11:12:54 +08:00
|
|
|
return "-" + slice[number-1]
|
2021-02-08 09:47:14 +08:00
|
|
|
}
|
2021-09-10 11:55:20 +08:00
|
|
|
return strings.Replace(slice[number-1], "%d", strconv.FormatInt(value, 10), 1)
|
2021-02-08 09:47:14 +08:00
|
|
|
}
|