func main() { orm.Debug = true orm.RunSyncdb("default", false, true) beego.Router("/", &controllers.MainController{}) beego.Router("/login", &controllers.LoginController{}) beego.Router("/category", &controllers.CategoryController{}) beego.Router("/reply", &controllers.ReplyController{}) beego.AutoRouter(&controllers.ReplyController{}) beego.Router("/topic", &controllers.TopicController{}) beego.AutoRouter(&controllers.TopicController{}) os.Mkdir("attachment", os.ModePerm) beego.SetStaticPath("/attachment", "attachment") err := i18n.SetMessage("en-US", "conf/locale_en-US.ini") if err != nil { beego.Error(err) } err = i18n.SetMessage("zh-CN", "conf/locale_zh-CN.ini") if err != nil { beego.Error(err) } err = i18n.SetMessage("zh-HK", "conf/locale_zh-HK.ini") if err != nil { beego.Error(err) } beego.AddFuncMap("i18n", i18n.Tr) beego.Run() }
func main() { i18n.SetMessage("en-US", "conf/locale_en-US.ini") i18n.SetMessage("zh-CN", "conf/locale_zh-CN.ini") beego.AddFuncMap("i18n", i18n.Tr) beego.Run() }
func init() { //conf/app.conf -> os.Getenv("config_path") configPath := os.Getenv("CONFIG_PATH") if len(configPath) != 0 { log.Infof("Config path: %s", configPath) beego.LoadAppConfig("ini", configPath) } beego.AddFuncMap("i18n", i18n.Tr) langs := strings.Split(beego.AppConfig.String("lang::types"), "|") names := strings.Split(beego.AppConfig.String("lang::names"), "|") supportLanguages = make(map[string]langType) langTypes = make([]*langType, 0, len(langs)) for i, lang := range langs { t := langType{ Lang: lang, Name: names[i], } langTypes = append(langTypes, &t) supportLanguages[lang] = t if err := i18n.SetMessage(lang, "static/i18n/"+"locale_"+lang+".ini"); err != nil { log.Errorf("Fail to set message file: %s", err.Error()) } } }
func (this *BaseController) Prepare() { //Accept-Language acceptLanguage := this.Ctx.Request.Header.Get("Accept-Language") if len(acceptLanguage) > 4 { acceptLanguage = acceptLanguage[:5] // Only compare first 5 letters. if i18n.IsExist(acceptLanguage) { Lang = acceptLanguage } } if len(Lang) == 0 { Lang = "en-US" } // Initialized language type list. langs := strings.Split(beego.AppConfig.String("lang::types"), "|") names := strings.Split(beego.AppConfig.String("lang::names"), "|") langTypes := make([]*langType, 0, len(langs)) for i, v := range langs { langTypes = append(langTypes, &langType{ Lang: v, Name: names[i], }) } for _, lang := range langs { beego.Trace("Loading language: " + lang) if err := i18n.SetMessage(lang, "lang/"+"locale_"+lang+".ini"); err != nil { beego.Error("Fail to set message file: " + err.Error()) return } } }
func New(localeDir string, f FindLangFunc) (p *I18nPlug, err error) { /* /etc/xrest/locale/zh-CN.ini /etc/xrest/locale/en-US.ini */ var locales []*i18n.Locale if err = filepath.Walk(localeDir, func(path string, info os.FileInfo, e error) error { if e != nil { return e } ext := filepath.Ext(path) if info.IsDir() || ext != ".ini" { return nil } lang := strings.ToLower(strings.TrimRight(info.Name(), ext)) if smErr := i18n.SetMessage(lang, path); smErr != nil { return smErr } locales = append(locales, &i18n.Locale{Lang: lang}) return nil }); err != nil { return nil, err } return &I18nPlug{ findLang: f, locales: locales, }, nil }
// 设置语言 func SetLang() { // 设定语言类型 langs := "en-US|zh-CN|ja-JP|zh-TW|ko-KR" // 对语言进行循环 for _, lang := range strings.Split(langs, "|") { // 去除首尾空格 lang = strings.TrimSpace(lang) // 设置文件路径 files := []string{"conf/locale_" + lang + ".ini"} if fh, err := os.Open(files[0]); err == nil { // 打开正确则关闭 fh.Close() } else { // 打开失败则报错 files = nil beego.Error(err.Error()) } if err := i18n.SetMessage(lang, "conf/locale_"+lang+".ini", files...); err != nil { // 载入语言文件失败则报错并退出 beego.Error("Fail to set message file: " + err.Error()) os.Exit(2) } } // 列出所有语言文件 Langs = i18n.ListLangs() }
// 初始化多国语言模块 func InitLang() { languages := beego.AppConfig.String("langs") langs := strings.Split(languages, "|") for _, lang := range langs { beego.Trace("Loading language: " + lang) if err := i18n.SetMessage(lang, "conf/locale/"+"locale_"+lang+".ini"); err != nil { beego.Error("Fail to set message file: " + err.Error()) return } } }
func init() { // Initialize language type list. langTypes = strings.Split(beego.AppConfig.String("lang_types"), "|") // Load locale files according to language types. for _, lang := range langTypes { beego.Trace("Loading language: " + lang) if err := i18n.SetMessage(lang, "conf/"+"locale_"+lang+".ini"); err != nil { beego.Error("Fail to set message file:", err) return } } }
func init() { beego.Router("/", &controllers.MainController{}) beego.Router("/topic", &controllers.TopicController{}) beego.AutoRouter(&controllers.TopicController{}) beego.Router("/about", &controllers.AboutController{}) langs := strings.Split(beego.AppConfig.String("types"), "|") for _, lang := range langs { if err := i18n.SetMessage(lang, "conf/"+"locale_"+lang+".ini"); err != nil { beego.Error(err.Error()) } } beego.AddFuncMap("i18n", i18n.Tr) }
func settingLocales() { // load locales with locale_LANG.ini files langs := "en-US|zh-CN" for _, lang := range strings.Split(langs, "|") { lang = strings.TrimSpace(lang) files := []string{"conf/" + "locale_" + lang + ".ini"} if fh, err := os.Open(files[0]); err == nil { fh.Close() } else { files = nil } if err := i18n.SetMessage(lang, "conf/global/"+"locale_"+lang+".ini", files...); err != nil { beego.Error("Fail to set message file: " + err.Error()) os.Exit(2) } } Langs = i18n.ListLangs() }
func initLocale() { langs := []string{"zh-CN", "en-US"} names := []string{"中文", "English"} config.LangTypes = make([]*LangType, 0, len(langs)) for i, v := range langs { config.LangTypes = append(config.LangTypes, &LangType{ Lang: v, Name: names[i], }) } for _, lang := range langs { beego.Trace("Loading language: " + lang) if err := i18n.SetMessage(lang, "conf/"+"index_"+lang+".ini"); err != nil { beego.Error("Fail to set message file: " + err.Error()) return } } }
/** * 设置语言包 */ func (this *BaseController) settingLocales() { // load locales with locale_LANG.ini files this.langTypes = []langType{ langType{Lang: "en-US", Name: "English"}, langType{Lang: "zh-CN", Name: "中文"}, } for _, langT := range this.langTypes { lang := langT.Lang if i18n.IsExist(lang) { continue } if err := i18n.SetMessage(lang, "conf/i18n/"+lang+".ini"); err != nil { beego.Error("Fail to set message file: " + err.Error()) return } } }
func InitLocales() { // Initialized language type list. langs := strings.Split(beego.AppConfig.String("lang::types"), "|") names := strings.Split(beego.AppConfig.String("lang::names"), "|") langTypes = make([]*langType, 0, len(langs)) for i, v := range langs { langTypes = append(langTypes, &langType{ Lang: v, Name: names[i], }) } for _, lang := range langs { beego.Trace("Loading language: " + lang) if err := i18n.SetMessage(lang, "conf/"+"locale_"+lang+".ini"); err != nil { beego.Error("Fail to set message file: " + err.Error()) return } } }
// 注册本地化文件 func initLanguage() error { langs := strings.Split(beego.AppConfig.String("lang::types"), "|") names := strings.Split(beego.AppConfig.String("lang::names"), "|") langTypes = make([]*langType, 0, len(langs)) for i, v := range langs { langTypes = append(langTypes, &langType{ Name: names[i], Lang: v, }) } for _, lang := range langs { err := i18n.SetMessage(lang, "languages/"+lang+".ini") if err != nil { return err } } return nil }
// We have to call a initialize function manully // because we use `bee bale` to pack static resources // and we cannot make sure that which init() execute first. func initialize() { // Load configuration, set app version and log level. utils.LoadConfig("conf/app.ini") // Load locale files. langs := strings.Split(utils.Cfg.MustValue("lang", "types"), "|") // Skip en-US. for i := 1; i < len(langs); i++ { err := i18n.SetMessage(langs[i], "conf/locale_"+langs[i]+".ini") if err != nil { panic("Fail to set message file: " + err.Error()) } } // Trim 4th part. routers.AppVer = strings.Join(strings.Split(APP_VER, ".")[:3], ".") beego.AppName = utils.Cfg.MustValue("beego", "app_name") beego.RunMode = utils.Cfg.MustValue("beego", "run_mode") beego.HttpPort = utils.Cfg.MustInt("beego", "http_port_"+beego.RunMode) routers.IsBeta = utils.Cfg.MustBool("server", "beta") routers.IsProMode = beego.RunMode == "prod" if routers.IsProMode { beego.SetLevel(beego.LevelInfo) beego.Info("Product mode enabled") os.Mkdir("./log", os.ModePerm) beego.BeeLogger.SetLogger("file", `{"filename": "log/log"}`) } // Initialize data. models.InitDb() routers.InitRouter() doc.SetGithubCredentials(utils.Cfg.MustValue("github", "client_id"), utils.Cfg.MustValue("github", "client_secret")) }
func settingLocales() { // autoload locales with locale_LANG.ini files dirs, _ := ioutil.ReadDir("conf") for _, info := range dirs { if !info.IsDir() { name := info.Name() if filepath.HasPrefix(name, "locale_") { if filepath.Ext(name) == ".ini" { lang := name[7 : len(name)-4] if len(lang) > 0 { if err := i18n.SetMessage(lang, "conf/"+name); err != nil { panic("Fail to set message file: " + err.Error()) } continue } } beego.Error("locale ", name, " not loaded") } } } Langs = i18n.ListLangs() }
// We have to call a initialize function manully // because we use `bee bale` to pack static resources // and we cannot make sure that which init() execute first. func initialize() { var err error // Load configuration, set app version and log level. utils.Cfg, err = utils.LoadConfig("conf/app.ini") if err != nil { panic("Fail to load configuration file: " + err.Error()) } err = i18n.SetMessage("conf/message.ini") if err != nil { panic("Fail to set message file: " + err.Error()) } // Trim 4th part. utils.AppVer = strings.Join(strings.Split(APP_VER, ".")[:3], ".") beego.AppName = utils.Cfg.MustValue("beego", "app_name") beego.RunMode = utils.Cfg.MustValue("beego", "run_mode") beego.HttpPort = utils.Cfg.MustInt("beego", "http_port_"+beego.RunMode) utils.AppName = beego.AppName utils.AppUrl = utils.Cfg.MustValue("app", "app_url") utils.AppDescription = utils.Cfg.MustValue("app", "description") utils.AppKeywords = utils.Cfg.MustValue("app", "keywords") utils.AppJsVer = utils.Cfg.MustValue("app", "js_ver") utils.AppCssVer = utils.Cfg.MustValue("app", "css_ver") utils.MailUser = utils.Cfg.MustValue("app", "mail_user") utils.MailFrom = utils.Cfg.MustValue("app", "mail_from") utils.SecretKey = utils.Cfg.MustValue("app", "secret_key") utils.ActiveCodeLives = utils.Cfg.MustInt("app", "acitve_code_live_days") utils.ResetPwdCodeLives = utils.Cfg.MustInt("app", "resetpwd_code_live_days") utils.IsBeta = utils.Cfg.MustBool("server", "beta") utils.IsProMode = beego.RunMode == "pro" if utils.IsProMode { beego.SetLevel(beego.LevelInfo) beego.Info("Product mode enabled") beego.Info(beego.AppName, APP_VER) } orm.Debug, _ = utils.Cfg.Bool("orm", "debug_log") driverName, _ := utils.Cfg.GetValue("orm", "driver_name") dataSource, _ := utils.Cfg.GetValue("orm", "data_source") maxIdle, _ := utils.Cfg.Int("orm", "max_idle_conn") // session settings beego.SessionOn = true beego.SessionProvider = utils.Cfg.MustValue("app", "session_provider") beego.SessionSavePath = utils.Cfg.MustValue("app", "session_path") beego.SessionName = utils.Cfg.MustValue("app", "session_name") // set mailer connect args mailer.MailHost = utils.Cfg.MustValue("mailer", "host") mailer.AuthUser = utils.Cfg.MustValue("mailer", "user") mailer.AuthPass = utils.Cfg.MustValue("mailer", "pass") // set default database orm.RegisterDataBase("default", driverName, dataSource, maxIdle) orm.RunSyncdb("default", false, true) orm.RunCommand() }
func init() { i18n.SetMessage("en-US", "conf/local_en-US.ini") i18n.SetMessage("zh-CN", "conf/local_zh-CN.ini") beego.AddFuncMap("i18n", i18n.Tr) }