Fetch the repository succeeded.
This action will force synchronization from dromara/carbon, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
package carbon
import (
"embed"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
)
//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 defines a Language struct.
// 定义 Language 结构体
type Language struct {
dir string
locale string
resources map[string]string
Error error
}
// NewLanguage returns a new Language instance.
// 初始化 Language 结构体
func NewLanguage() *Language {
return &Language{
dir: defaultDir,
locale: defaultLocale,
resources: make(map[string]string),
}
}
// SetLocale sets language locale.
// 设置区域
func (lang *Language) SetLocale(locale string) {
if len(lang.resources) != 0 {
return
}
lang.locale = locale
fileName := lang.dir + string(os.PathSeparator) + locale + ".json"
bytes, err := fs.ReadFile(fileName)
if err != nil {
lang.Error = invalidLocaleError(fileName)
return
}
if json.Unmarshal(bytes, &lang.resources) != nil {
lang.Error = invalidLocaleError(fileName)
}
}
// SetResources sets language 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 := lang.resources[k]; ok {
lang.resources[k] = v
}
}
}
// translate returns a translated string.
// 翻译转换
func (lang *Language) translate(unit string, value int64) string {
if len(lang.resources) == 0 {
lang.SetLocale(defaultLocale)
}
slice := strings.Split(lang.resources[unit], "|")
number := getAbsValue(value)
if len(slice) == 1 {
return strings.Replace(slice[0], "%d", strconv.FormatInt(value, 10), 1)
}
if int64(len(slice)) <= number {
return strings.Replace(slice[len(slice)-1], "%d", strconv.FormatInt(value, 10), 1)
}
if !strings.Contains(slice[number-1], "%d") && value < 0 {
return "-" + slice[number-1]
}
return strings.Replace(slice[number-1], "%d", strconv.FormatInt(value, 10), 1)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。