index.js
5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {
getI18nPropertiesApi
} from "@/api";
import {
createI18n
} from "vue-i18n";
// 获取当前语言环境
function getLanguage() {
const storageLang = uni.getStorageSync("lang");
if (storageLang) {
return storageLang;
}
const systemLang = uni.getSystemInfoSync().language.toLowerCase();
const languageMap = {
ja: "ja_JP",
"ja-jp": "ja_JP",
en: "en_US",
"en-us": "en_US",
"en-gb": "en_US",
"en-au": "en_US",
// "zh-tw": "zh_TW",
// "zh-hk": "zh_TW",
// "zh-mo": "zh_TW",
// "zh-hant": "zh_TW",
// "zh-cn": "zh_CN",
// "zh-sg": "zh_CN",
// "zh-hans": "zh_CN",
// zh: "zh_CN",
};
return languageMap[systemLang] || "en_US";
}
export const i18n = createI18n({
legacy: false,
locale: getLanguage(),
fallbackLocale: "en_US",
messages: {},
missingWarn: false,
fallbackWarn: false,
});
export const changeLanguage = async (lang) => {
try {
uni.showLoading({
title: i18n.global.t("navData.load"),
mask: true,
});
uni.setStorageSync("lang", lang);
await fetchPropertiesFile(lang);
} catch (e) {
console.log(e);
} finally {
uni.hideLoading();
}
};
// 更新TabBar文本
export const updateTabBarText = () => {
const tabbarNameList = [
"app.tabbar.home",
"YAP", // 不需要翻译
"app.tabbar.workbench",
"app.tabbar.message",
"app.tabbar.my",
];
tabbarNameList.forEach((item, index) => {
try {
uni.setTabBarItem({
index,
text: item === "YAP" ? item : i18n.global.t(item),
});
} catch (err) {
console.warn("设置TabBar项目失败:", err);
}
});
};
// 解析properties文件
const parseProperties = (text, currentLang) => {
const jsonObj = {};
const lines = text.split("\n");
lines.forEach((line) => {
if (line && !line.startsWith("#")) {
const [key, value] = line.split("=");
if (key) {
let chineseStr = value || "";
if (currentLang.indexOf("zh") !== -1) {
chineseStr = chineseStr.replace(
/\\u([\dA-Fa-f]{4})/g,
(match, p1) => {
return String.fromCharCode(parseInt(p1, 16));
}
);
}
chineseStr = chineseStr.replace(/\\/g, "");
jsonObj[key.trim()] = chineseStr;
}
}
});
return jsonObj;
};
export const fetchPropertiesFile = async (lang) => {
try {
const res = await getI18nPropertiesApi(lang);
const resObj = parseProperties(res, lang);
i18n.global.setLocaleMessage(lang, resObj);
i18n.global.locale.value = lang;
uni.setStorageSync("i18n", JSON.stringify(resObj));
} catch (e) {
// 首次加载失败,使用部分默认语言 保证首页正常加载
const resObj = {
'app.login.welcome': 'Welcome',
'rules.loginName': 'Please enter user name',
'rules.password': 'Please enter password',
'login.rememberPw': 'Remember Password',
'login.forgotPassword': 'Forgot Password',
'app.login.readedAndAgree': 'Read and agree',
'app.login.privacyAgreement': 'Privacy Agreement',
'table.language': 'Language',
'button.nextStep': 'Next Step',
'message.confirm': 'Confirm',
'app.login.networkError': 'Network timeout',
'message.resetPassEnterUserName': 'Enter Your Username and We Will Help You Reset Your Password.',
'app.login.verifyError': 'Verification failed',
}
i18n.global.setLocaleMessage('en_US', resObj);
i18n.global.locale.value = 'en_US';
uni.setStorageSync("lang", 'en_US');
uni.getNetworkType({
success: function(res) {
// if (res.networkType !== 'none') {
// } else {
// // openAppSettings()
// }
uni.showToast({
title: 'Network Error',
icon: "none",
});
}
})
}
};
// function openAppSettings() {
// // #ifdef APP-PLUS
// console.log(plus.os.name);
// if (plus.os.name === 'iOS') {
// const UIApplication = plus.ios.import('UIApplication');
// const NSURL = plus.ios.import('NSURL');
// const application = UIApplication.sharedApplication();
// const url = NSURL.URLWithString('app-settings:');
// if (application.respondsToSelector('openURL:')) {
// application.openURL(url);
// }
// plus.ios.deleteObject(url);
// plus.ios.deleteObject(NSURL);
// plus.ios.deleteObject(UIApplication);
// }
// // #endif
// }
export const initI18n = async (type = "loadPage") => {
try {
const curLang = getLanguage();
const storageLang = uni.getStorageSync("lang");
if (!storageLang) {
uni.setStorageSync("lang", curLang);
}
const i18nData = uni.getStorageSync("i18n");
if (type === "main" || !i18nData) {
await fetchPropertiesFile(curLang, type);
} else {
i18n.global.setLocaleMessage(curLang, JSON.parse(i18nData));
i18n.global.locale.value = curLang;
}
} catch (e) {
console.log(e);
} finally {
// #ifdef APP
// 1s的延时保证没有页面跳转动画
setTimeout(() => {
plus.navigator.closeSplashscreen();
}, 1000);
// #endif
}
};
export const t = i18n.global.t;