-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutilLocale.go
66 lines (62 loc) · 1.92 KB
/
utilLocale.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2023 gpress Authors.
//
// This file is part of gpress.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"errors"
"io"
"os"
"strings"
)
// localeMap 用于记录翻译的Map
var localeMap = make(map[string]string)
var gpressLocate = "zh-CN"
// initLocale 要在config初始化之后,需要获取config中的语言配置
func initLocale() {
gpressLocate = config.Locale
defaultErr := errors.New(gpressLocate + ".json " + funcT("Load failed, using default") + " zh-CN.json")
// 打开文件
jsonFile, err := os.Open(datadir + "/locales/" + gpressLocate + ".json")
if err != nil {
FuncLogError(nil, defaultErr)
jsonFile, err = os.Open(datadir + "/locales/zh-CN.json")
if err != nil {
FuncLogError(nil, defaultErr)
return
}
gpressLocate = "zh-CN"
}
// 关闭文件
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
FuncLogError(nil, defaultErr)
return
}
localeMapTemp := make(map[string]string)
// Decode从输入流读取下一个json编码值并保存在v指向的值里
err = json.Unmarshal([]byte(byteValue), &localeMapTemp)
if err != nil {
FuncLogError(nil, defaultErr)
return
}
//不区分大小写
for key, value := range localeMapTemp {
localeMap[strings.ToLower(key)] = value
}
localeMapTemp = nil
}