wide/i18n/locales.go

99 lines
2.0 KiB
Go
Raw Normal View History

2015-01-18 08:59:10 +03:00
// Copyright (c) 2014-2015, b3log.org
2014-11-26 06:24:24 +03:00
//
2014-11-12 18:13:14 +03:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-11-26 06:24:24 +03:00
//
2014-11-12 18:13:14 +03:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-11-26 06:24:24 +03:00
//
2014-11-12 18:13:14 +03:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-12-07 06:30:53 +03:00
// Package i18n includes internationalization related manipulations.
2014-08-25 18:17:02 +04:00
package i18n
import (
"encoding/json"
"io/ioutil"
"os"
2014-12-08 12:43:25 +03:00
"sort"
2014-10-28 09:38:27 +03:00
"strings"
2014-09-08 07:37:34 +04:00
2014-12-13 13:47:41 +03:00
"github.com/b3log/wide/log"
2014-08-25 18:17:02 +04:00
)
2014-12-13 13:47:41 +03:00
// Logger.
var logger = log.NewLogger(os.Stdout)
2014-10-29 13:15:18 +03:00
// Locale.
2014-08-25 18:17:02 +04:00
type locale struct {
Name string
Langs map[string]interface{}
TimeZone string
}
2014-10-29 13:15:18 +03:00
// All locales.
2014-08-25 18:17:02 +04:00
var Locales = map[string]locale{}
2014-10-29 13:15:18 +03:00
// Load loads i18n message configurations.
2014-09-02 18:57:30 +04:00
func Load() {
2014-10-28 09:38:27 +03:00
f, _ := os.Open("i18n")
names, _ := f.Readdirnames(-1)
f.Close()
2014-08-25 18:17:02 +04:00
2014-10-28 09:38:27 +03:00
for _, name := range names {
if !strings.HasSuffix(name, ".json") {
continue
}
loc := name[:strings.LastIndex(name, ".")]
load(loc)
}
2014-10-23 17:43:35 +04:00
}
func load(localeStr string) {
bytes, err := ioutil.ReadFile("i18n/" + localeStr + ".json")
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-23 17:43:35 +04:00
os.Exit(-1)
}
2014-08-25 18:17:02 +04:00
2014-10-23 17:43:35 +04:00
l := locale{Name: localeStr}
2014-08-25 18:17:02 +04:00
2014-10-23 17:43:35 +04:00
err = json.Unmarshal(bytes, &l.Langs)
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-08-25 18:17:02 +04:00
os.Exit(-1)
}
2014-10-23 17:43:35 +04:00
Locales[localeStr] = l
}
2014-09-16 07:36:21 +04:00
2015-01-13 08:54:19 +03:00
// Get gets a message with the specified locale and key.
2014-10-23 17:43:35 +04:00
func Get(locale, key string) interface{} {
2014-09-16 07:36:21 +04:00
return Locales[locale].Langs[key]
}
2014-10-29 13:15:18 +03:00
// GetAll gets all messages with the specified locale.
2014-10-23 17:43:35 +04:00
func GetAll(locale string) map[string]interface{} {
2014-08-25 18:17:02 +04:00
return Locales[locale].Langs
}
2014-11-26 06:24:24 +03:00
// GetLocalesNames gets names of all locales. Returns ["zh_CN", "en_US"] for example.
func GetLocalesNames() []string {
ret := []string{}
2014-12-07 06:30:53 +03:00
for name := range Locales {
2014-11-26 06:24:24 +03:00
ret = append(ret, name)
}
2014-12-08 12:43:25 +03:00
sort.Strings(ret)
2014-11-26 06:24:24 +03:00
return ret
}