本文整理匯總了Golang中github.com/nicksnyder/go-i18n/i18n.MustLoadTranslationFile函數的典型用法代碼示例。如果您正苦於以下問題:Golang MustLoadTranslationFile函數的具體用法?Golang MustLoadTranslationFile怎麽用?Golang MustLoadTranslationFile使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了MustLoadTranslationFile函數的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: MustLoad
func MustLoad() {
// .env file
err := godotenv.Load()
if err != nil {
log.Fatalf("Unable to load environment settings: %s", err)
}
// translations
i18n.MustLoadTranslationFile("i18n/en-us.all.json")
i18n.MustLoadTranslationFile("i18n/pt-br.all.json")
}
示例2: loadFromAsset
func loadFromAsset(packageName, assetPath, locale, language string) error {
assetName := locale + ".all.json"
assetKey := filepath.Join(assetPath, language, packageName, assetName)
byteArray, err := resources.Asset(assetKey)
if err != nil {
return err
}
if len(byteArray) == 0 {
return errors.New(fmt.Sprintf("Could not load i18n asset: %v", assetKey))
}
tmpDir, err := ioutil.TempDir("", "cloudfoundry_cli_i18n_res")
if err != nil {
return err
}
defer func() {
os.RemoveAll(tmpDir)
}()
fileName, err := saveLanguageFileToDisk(tmpDir, assetName, byteArray)
if err != nil {
return err
}
go_i18n.MustLoadTranslationFile(fileName)
os.RemoveAll(fileName)
return nil
}
示例3: main
func main() {
// Configure the i18n library to use the preferred language set in the CENOLANG environement variable
setLanguage := os.Getenv("CENOLANG")
if setLanguage == "" {
os.Setenv("CENOLANG", "en-us")
setLanguage = "en-us"
}
i18n.MustLoadTranslationFile("./translations/" + setLanguage + ".all.json")
T, _ := i18n.Tfunc(setLanguage, "en-us")
// Read an existing configuration file or have the user supply settings
if conf, err := ReadConfigFile(CONFIG_FILE); err != nil {
fmt.Println(T("no_config_cli", map[string]interface{}{"Location": CONFIG_FILE}))
Configuration = GetConfigFromUser()
} else {
Configuration = conf
}
// Create an HTTP proxy server
http.HandleFunc("/lookup", directHandler)
http.HandleFunc("/", proxyHandler)
fmt.Println(T("listening_msg_cli", map[string]interface{}{"Port": Configuration.PortNumber}))
err := http.ListenAndServe(Configuration.PortNumber, nil)
if err != nil {
panic(err)
}
}
示例4: Example
func Example() {
i18n.MustLoadTranslationFile("../goi18n/testdata/expected/en-us.all.json")
T, _ := i18n.Tfunc("en-US")
bobMap := map[string]interface{}{"Person": "Bob"}
bobStruct := struct{ Person string }{Person: "Bob"}
fmt.Println(T("program_greeting"))
fmt.Println(T("person_greeting", bobMap))
fmt.Println(T("person_greeting", bobStruct))
fmt.Println(T("your_unread_email_count", 0))
fmt.Println(T("your_unread_email_count", 1))
fmt.Println(T("your_unread_email_count", 2))
fmt.Println(T("my_height_in_meters", "1.7"))
fmt.Println(T("person_unread_email_count", 0, bobMap))
fmt.Println(T("person_unread_email_count", 1, bobMap))
fmt.Println(T("person_unread_email_count", 2, bobMap))
fmt.Println(T("person_unread_email_count", 0, bobStruct))
fmt.Println(T("person_unread_email_count", 1, bobStruct))
fmt.Println(T("person_unread_email_count", 2, bobStruct))
fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{
"Person": "Bob",
"Timeframe": T("d_days", 0),
}))
fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{
"Person": "Bob",
"Timeframe": T("d_days", 1),
}))
fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{
"Person": "Bob",
"Timeframe": T("d_days", 2),
}))
// Output:
// Hello world
// Hello Bob
// Hello Bob
// You have 0 unread emails.
// You have 1 unread email.
// You have 2 unread emails.
// I am 1.7 meters tall.
// Bob has 0 unread emails.
// Bob has 1 unread email.
// Bob has 2 unread emails.
// Bob has 0 unread emails.
// Bob has 1 unread email.
// Bob has 2 unread emails.
// Bob has 3 unread emails in the past 0 days.
// Bob has 3 unread emails in the past 1 day.
// Bob has 3 unread emails in the past 2 days.
}
示例5: InitTranslationsWithDir
func InitTranslationsWithDir(dir string) {
i18nDirectory := FindDir(dir)
files, _ := ioutil.ReadDir(i18nDirectory)
for _, f := range files {
if filepath.Ext(f.Name()) == ".json" {
filename := f.Name()
locales[strings.Split(filename, ".")[0]] = i18nDirectory + filename
i18n.MustLoadTranslationFile(i18nDirectory + filename)
}
}
}
示例6: LoadLangs
func LoadLangs() {
var langs []M.Lang
l := &Lang{}
err := DM.FindAll(&M.Lang{}, &langs, M.Sf{"Code"}, M.Where{}, M.NewParams(M.Params{Sort: "Code ASC"}))
if err != nil {
beego.Error(err)
return
}
for _, v := range langs {
i18n.MustLoadTranslationFile(l.langFileName("lang::folder", v.Code))
}
}
示例7: Init
// Init configures the localizaton page. Pass the valid path(s) to i18n
// JSON files as defined by the `github.com/nicksnyder/go-i18n` package.
func Init(files []string, defaultLocale string) (err error) {
if len(files) == 0 {
return ErrNoTranslationFiles
}
for _, path := range files {
i18n.MustLoadTranslationFile(path)
}
T = NewTranslationFunc(defaultLocale)
return nil
}
示例8: sysInit
func sysInit() {
<-(gottp.SysInitChan) //Buffered Channel to receive the server upstart boolean
config.InitTracer()
log.Println("Initialized GoTracer")
db.Conn = db.GetConn(
config.Settings.Khabar.DBName,
config.Settings.Khabar.DBAddress,
config.Settings.Khabar.DBUsername,
config.Settings.Khabar.DBPassword,
)
log.Println("Database Connected :" + config.Settings.Khabar.DBName + " " +
"at address:" + config.Settings.Khabar.DBAddress)
transDir := config.Settings.Khabar.TranslationDirectory
if len(transDir) == 0 {
cwd := os.Getenv("PWD")
transDir = cwd + "/translations"
config.Settings.Khabar.TranslationDirectory = cwd
}
log.Println("Directory for translation :" + transDir)
filepath.Walk(transDir, func(path string, _ os.FileInfo, err error) error {
fileExt := filepath.Ext(path)
if fileExt == ".json" && err == nil {
log.Println("Loading translation file:" + path)
i18n.MustLoadTranslationFile(path)
} else {
log.Print("Skipping translation file:" + path + " " +
"File Extension:" + fileExt + " ")
if err != nil {
log.Print("Error:" + err.Error())
}
}
return nil
})
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
log.Println("Translations have been parsed.")
}
示例9: loadTranslationFiles
// loadTranslationFiles loads the found translation files into the i18n
// messaging system for use by the application
func loadTranslationFiles(directory string) {
// Read the directory
fileInfos, err := ioutil.ReadDir(directory)
if err != nil {
tracelog.CompletedError(err, "localize", "loadTranslationFiles")
return
}
// Look for JSON files
for _, fileInfo := range fileInfos {
if path.Ext(fileInfo.Name()) != ".json" {
continue
}
fileName := fmt.Sprintf("%s/%s", directory, fileInfo.Name())
tracelog.Info("localize", "loadTranslationFiles", "Loading %s", fileName)
i18n.MustLoadTranslationFile(fileName)
}
}
示例10: loadFromAsset
func loadFromAsset(locale string) error {
assetName := locale + ".all.json"
assetKey := filepath.Join(GetResourcesPath(), assetName)
byteArray, err := resources.Asset(assetKey)
if err != nil {
return err
}
if len(byteArray) == 0 {
return errors.New(fmt.Sprintf("Could not load i18n asset: %v", assetKey))
}
_, err = os.Stat(os.TempDir())
if err != nil {
if !os.IsExist(err) {
return errors.New("Please make sure Temp dir exist - " + os.TempDir())
} else {
return err
}
}
tmpDir, err := ioutil.TempDir("", "cloudfoundry_cli_i18n_res")
if err != nil {
return err
}
defer func() {
os.RemoveAll(tmpDir)
}()
fileName, err := saveLanguageFileToDisk(tmpDir, assetName, byteArray)
if err != nil {
return err
}
go_i18n.MustLoadTranslationFile(fileName)
os.RemoveAll(fileName)
return nil
}
示例11: Example_template
func Example_template() {
i18n.MustLoadTranslationFile("../goi18n/testdata/expected/en-us.all.json")
T, _ := i18n.Tfunc("en-US")
tmpl.Funcs(map[string]interface{}{
"T": T,
})
tmpl.Execute(os.Stdout, map[string]interface{}{
"Person": "Bob",
"Timeframe": T("d_days", 1),
})
tmpl.Execute(os.Stdout, struct {
Person string
Timeframe string
}{
Person: "Bob",
Timeframe: T("d_days", 1),
})
// Output:
// Hello world
// Hello Bob
// You have 0 unread emails.
// You have 1 unread email.
// You have 2 unread emails.
// Bob has 0 unread emails.
// Bob has 1 unread email.
// Bob has 2 unread emails.
//
// Hello world
// Hello Bob
// You have 0 unread emails.
// You have 1 unread email.
// You have 2 unread emails.
// Bob has 0 unread emails.
// Bob has 1 unread email.
// Bob has 2 unread emails.
}
示例12: TestMain
func TestMain(m *testing.M) {
c, err := config.NewConfig("ini", "../conf/app.conf")
if err != nil {
log.Fatal(err)
}
TestConfig, err := conf.Initialize("test", c)
if err != nil {
log.Fatal(err)
}
if err := DbOpen(TestConfig.Db); err != nil {
log.Fatal(err)
}
defer DbClose()
PrepareTables(&User{}, &Cand{}, &Rat{}, &Crit{}, &Dep{}, &Comment{}, &Role{}, &Contact{}, &Mail{})
err = ClearTestData(&User{}, &Crit{}, &Rat{}, &Dep{})
if err != nil {
log.Fatal(err)
}
err = InsertTestData("../migrations/test_data/data.sql")
if err != nil {
log.Fatal(err)
}
i18n.MustLoadTranslationFile("../langs/en-US.all.json")
T, _ = i18n.Tfunc("en-US", "en-US", "en-US")
exitCode := m.Run()
os.Exit(exitCode)
}
示例13: initI18N
func initI18N() {
i18n.MustLoadTranslationFile("service/userService/locale/en-us.all.json")
i18n.MustLoadTranslationFile("service/userService/locale/ko-kr.all.json")
}
示例14: Initializei18n
func Initializei18n() {
i18nFile := "i18n/" + *lang + ".all.json"
log.Printf("Loading i18n from [%s]", i18nFile)
i18n.MustLoadTranslationFile(i18nFile)
}