i18n.js 1.2 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
		uni.setStorageSync('CURRENT_LANG', language)
		currentLang = language
	}else{
		uni.getSystemInfo({
			success: function (res) {
				uni.getSystemInfo({
					success: function (res) {
						console.log('获取设备信息:',res);
						let language = 'zh-Hans' 
						if(res.language != 'zh-Hans'){
							language = 'en'
						}
						uni.setStorageSync('CURRENT_LANG', language)
						currentLang = language
					},
					fail: (err) => {
						console.error(err)
					}
				})
			},
			fail: (err) => {
				console.error(err)
			}
		})
	}
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
}
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