wide/i18n/locales.go

61 lines
1.0 KiB
Go
Raw Normal View History

2014-09-12 13:10:58 +04:00
// 国际化操作.
2014-08-25 18:17:02 +04:00
package i18n
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
2014-09-08 07:37:34 +04:00
"github.com/golang/glog"
2014-08-25 18:17:02 +04:00
)
type locale struct {
Name string
Langs map[string]interface{}
TimeZone string
}
// 所有的 locales.
var Locales = map[string]locale{}
2014-09-12 13:10:58 +04:00
// 加载国际化配置.
2014-09-02 18:57:30 +04:00
func Load() {
2014-08-25 18:17:02 +04:00
// TODO: 加载所有语言配置
bytes, _ := ioutil.ReadFile("i18n/zh_CN.json")
zhCN := locale{Name: "zh_CN"}
// TODO: 时区
err := json.Unmarshal(bytes, &zhCN.Langs)
if err != nil {
glog.Error(err)
os.Exit(-1)
}
Locales["zh_CN"] = zhCN
2014-09-08 07:37:34 +04:00
glog.V(5).Info("Loaded [zh_CN] locale configuration")
2014-08-25 18:17:02 +04:00
}
2014-09-16 07:36:21 +04:00
// 获取请求对应的本地语言配置项.
func Get(r *http.Request, key string) interface{} {
locale := GetLocale(r)
return Locales[locale].Langs[key]
}
2014-09-12 13:10:58 +04:00
// 获取请求对应的本地语言配置.
2014-09-16 07:36:21 +04:00
func GetAll(r *http.Request) map[string]interface{} {
2014-09-01 06:47:59 +04:00
locale := GetLocale(r)
2014-08-25 18:17:02 +04:00
return Locales[locale].Langs
}
2014-09-12 13:10:58 +04:00
// 获取请求对应的 locale.
2014-09-01 06:47:59 +04:00
func GetLocale(r *http.Request) string {
2014-08-25 18:17:02 +04:00
// TODO: 从请求中获取 locale
return "zh_CN"
}