i18n: auto detect locale from browser (#253)

* i18n: detect locale from browser

* fix regex for locale matching

* remove debug code
pull/272/head
Equim 2017-10-15 15:12:40 +08:00 committed by Henrique Dias
parent b93aed98b1
commit 4b84492a11
4 changed files with 59 additions and 6 deletions

View File

@ -9,8 +9,35 @@ import zhTW from './zh-tw.yaml'
Vue.use(VueI18n) Vue.use(VueI18n)
let locale = (navigator.language || navigator.browserLangugae).toLowerCase()
switch (true) {
case /^en.*/i.test(locale):
locale = 'en'
break
case /^fr.*/i.test(locale):
locale = 'fr'
break
case /^pt.*/i.test(locale):
locale = 'pr'
break
case /^ja.*/i.test(locale):
locale = 'ja'
break
case /^zh-CN/i.test(locale):
locale = 'zh-cn'
break
case /^zh-TW/i.test(locale):
locale = 'zh-tw'
break
case /^zh.*/i.test(locale):
locale = 'zh-cn'
break
default:
locale = 'en'
}
const i18n = new VueI18n({ const i18n = new VueI18n({
locale: 'en', locale: locale,
fallbackLocale: 'en', fallbackLocale: 'en',
messages: { messages: {
'en': en, 'en': en,

View File

@ -27,8 +27,34 @@ const mutations = {
setLoading: (state, value) => { state.loading = value }, setLoading: (state, value) => { state.loading = value },
setReload: (state, value) => { state.reload = value }, setReload: (state, value) => { state.reload = value },
setUser: (state, value) => { setUser: (state, value) => {
moment.locale(value.locale) let locale = (value.locale || navigator.language || navigator.browserLangugae).toLowerCase()
i18n.locale = value.locale switch (true) {
case /^en.*/i.test(locale):
locale = 'en'
break
case /^fr.*/i.test(locale):
locale = 'fr'
break
case /^pt.*/i.test(locale):
locale = 'pr'
break
case /^ja.*/i.test(locale):
locale = 'ja'
break
case /^zh-CN/i.test(locale):
locale = 'zh-cn'
break
case /^zh-TW/i.test(locale):
locale = 'zh-tw'
break
case /^zh.*/i.test(locale):
locale = 'zh-cn'
break
default:
locale = 'en'
}
moment.locale(locale)
i18n.locale = locale
state.user = value state.user = value
}, },
setCSS: (state, value) => (state.css = value), setCSS: (state, value) => (state.css = value),

View File

@ -64,7 +64,7 @@ func init() {
flag.BoolVar(&allowPublish, "allow-publish", true, "Default allow publish option for new users") flag.BoolVar(&allowPublish, "allow-publish", true, "Default allow publish option for new users")
flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option for new users") flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option for new users")
flag.BoolVar(&noAuth, "no-auth", false, "Disables authentication") flag.BoolVar(&noAuth, "no-auth", false, "Disables authentication")
flag.StringVar(&locale, "locale", "en", "Default locale for new users") flag.StringVar(&locale, "locale", "", "Default locale for new users, set it empty to enable auto detect from browser")
flag.StringVar(&staticg, "staticgen", "", "Static Generator you want to enable") flag.StringVar(&staticg, "staticgen", "", "Static Generator you want to enable")
flag.BoolVarP(&showVer, "version", "v", false, "Show version") flag.BoolVarP(&showVer, "version", "v", false, "Show version")
} }
@ -81,7 +81,7 @@ func setupViper() {
viper.SetDefault("AllowNew", true) viper.SetDefault("AllowNew", true)
viper.SetDefault("AllowPublish", true) viper.SetDefault("AllowPublish", true)
viper.SetDefault("StaticGen", "") viper.SetDefault("StaticGen", "")
viper.SetDefault("Locale", "en") viper.SetDefault("Locale", "")
viper.SetDefault("NoAuth", false) viper.SetDefault("NoAuth", false)
viper.SetDefault("BaseURL", "") viper.SetDefault("BaseURL", "")
viper.SetDefault("PrefixURL", "") viper.SetDefault("PrefixURL", "")

View File

@ -362,7 +362,7 @@ var DefaultUser = User{
Rules: []*Rule{}, Rules: []*Rule{},
CSS: "", CSS: "",
Admin: true, Admin: true,
Locale: "en", Locale: "",
Scope: ".", Scope: ".",
FileSystem: fileutils.Dir("."), FileSystem: fileutils.Dir("."),
ViewMode: "mosaic", ViewMode: "mosaic",