i18n.js 1.1 KB
Newer Older
1
import langEn from './en'
2
import zhHans from './zh-Hans'
3 4
const messages = {
	'en': langEn,
5
	'zh-Hans': zhHans
6 7 8
}
let currentLang = uni.getStorageSync('CURRENT_LANG')
if (!currentLang) {
9 10 11 12 13
	if(uni.getLocale){
		console.log('获取应用语言:',uni.getLocale() );
		let language = 'zh-Hans' 
		if(uni.getLocale() != 'zh-Hans'){
			language = 'en'
14
		}
15 16 17 18 19
		uni.setStorageSync('CURRENT_LANG', language)
		currentLang = language
	}else{
		uni.getSystemInfo({
			success: function (res) {
20 21 22 23 24 25 26
				console.log('获取设备信息:',res);
				let language = 'zh-Hans' 
				if(res.language == 'en'){
					language = 'en'
				}
				uni.setStorageSync('CURRENT_LANG', language)
				currentLang = language
27 28 29 30 31 32
			},
			fail: (err) => {
				console.error(err)
			}
		})
	}
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
}
let i18nConfig = {
	locale: currentLang, // set locale
	messages // set locale messages
}

// #ifdef VUE2
import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n(i18nConfig)
// #endif

// #ifdef VUE3
import {createI18n} from 'vue-i18n'
const i18n = createI18n(i18nConfig)
// #endif

export default i18n