2021-02-08 09:47:14 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-08-05 19:41:54 +08:00
|
|
|
|
|
|
|
"gitee.com/go-package/carbon/lang"
|
2021-02-08 09:47:14 +08:00
|
|
|
)
|
|
|
|
|
2021-02-18 14:32:31 +08:00
|
|
|
var (
|
|
|
|
defaultLocale = "en"
|
|
|
|
)
|
|
|
|
|
2021-08-16 09:44:30 +08:00
|
|
|
// Language define 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-02-08 09:47:14 +08:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:58:43 +08:00
|
|
|
// NewLanguage return 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-02-23 09:32:55 +08:00
|
|
|
locale: defaultLocale,
|
2021-02-08 09:47:14 +08:00
|
|
|
resources: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:58:43 +08:00
|
|
|
// SetLocale sets language locale.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 设置区域
|
2021-08-05 19:41:54 +08:00
|
|
|
func (l *Language) SetLocale(locale string) error {
|
|
|
|
if len(l.resources) != 0 {
|
2021-02-18 14:32:31 +08:00
|
|
|
return nil
|
2021-02-08 09:47:14 +08:00
|
|
|
}
|
2021-08-10 10:58:43 +08:00
|
|
|
l.locale = locale
|
2021-08-05 19:41:54 +08:00
|
|
|
resources, err := lang.LoadLocale(locale)
|
2021-02-08 09:47:14 +08:00
|
|
|
if err != nil {
|
2021-08-05 19:41:54 +08:00
|
|
|
return err
|
2021-02-08 09:47:14 +08:00
|
|
|
}
|
2021-08-05 19:41:54 +08:00
|
|
|
l.resources = resources
|
2021-02-18 14:32:31 +08:00
|
|
|
return nil
|
|
|
|
}
|
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-08-05 19:41:54 +08:00
|
|
|
func (l *Language) SetResources(resources map[string]string) {
|
|
|
|
if len(l.resources) == 0 {
|
|
|
|
l.resources = resources
|
2021-02-23 09:32:55 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, v := range resources {
|
2021-08-05 19:41:54 +08:00
|
|
|
if _, ok := l.resources[k]; ok {
|
|
|
|
l.resources[k] = v
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:58:43 +08:00
|
|
|
// translate translates by unit string.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 翻译转换
|
2021-08-05 19:41:54 +08:00
|
|
|
func (l *Language) translate(unit string, number int64) string {
|
|
|
|
if len(l.resources) == 0 {
|
|
|
|
l.SetLocale(defaultLocale)
|
2021-08-02 10:25:27 +08:00
|
|
|
}
|
2021-08-05 19:41:54 +08:00
|
|
|
slice := strings.Split(l.resources[unit], "|")
|
2021-02-23 09:32:55 +08:00
|
|
|
if len(slice) == 1 {
|
2021-08-02 10:25:27 +08:00
|
|
|
return strings.Replace(slice[0], "%d", strconv.FormatInt(number, 10), 1)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
2021-08-02 10:25:27 +08:00
|
|
|
if number > 1 {
|
|
|
|
return strings.Replace(slice[1], "%d", strconv.FormatInt(number, 10), 1)
|
2021-02-08 09:47:14 +08:00
|
|
|
}
|
2021-02-23 09:32:55 +08:00
|
|
|
return slice[0]
|
2021-02-08 09:47:14 +08:00
|
|
|
}
|