gc_i18n 1.2.12 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/packages/index.js CHANGED
@@ -39,7 +39,7 @@ export default class I18n {
39
39
  login,
40
40
  keyboard
41
41
  } = options;
42
- this.token = token || generateJWT(orgCode);
42
+ this.token = token || generateJWT(orgCode) || store2.get("I18N_TOKEN");
43
43
  this.loadI18n = true;
44
44
  this.appCode = appCode;
45
45
  this.router = router;
@@ -74,7 +74,6 @@ export default class I18n {
74
74
  // 监听浏览器语言变化
75
75
  this.setupLanguageChangeListener();
76
76
  }
77
-
78
77
  // 监听浏览器语言变化
79
78
  setupLanguageChangeListener() {
80
79
  if ("onlanguagechange" in window) {
@@ -204,7 +203,7 @@ export default class I18n {
204
203
  if (routeName) {
205
204
  const routerKey = `${routeName}.${key}`;
206
205
  const routeTranslation = originalT(routerKey);
207
- if (routeTranslation !== routerKey) {
206
+ if (routeTranslation !== routerKey && !_.isEmpty(routeTranslation)) {
208
207
  return routeTranslation;
209
208
  } else {
210
209
  const commonKey = `common.${key}`;
@@ -219,11 +218,21 @@ export default class I18n {
219
218
  return originalT(key, comment);
220
219
  }
221
220
  };
221
+ globalThis.$i18nLogin = async (orgCode) => {
222
+ this.setToken(generateJWT(orgCode));
223
+ this.clearI18n();
224
+ await this.setLanguage(this.locale);
225
+ location.reload();
226
+ };
227
+ globalThis.$i18nLogout = (orgCode) => {
228
+ this.clearI18n();
229
+ location.reload();
230
+ };
222
231
 
223
232
  globalThis.$deepScan = function (val) {
224
233
  return val;
225
234
  };
226
- globalThis.$clearI18n = () => {
235
+ globalThis.$clearI18n = this.clearI18n = () => {
227
236
  const languageStore = store2.namespace(`I18N_${_.toUpper(this.appCode)}`);
228
237
  languageStore.clearAll();
229
238
  };
@@ -236,6 +245,7 @@ export default class I18n {
236
245
  }
237
246
  setToken(token) {
238
247
  this.token = token;
248
+ store2.set("I18N_TOKEN", token);
239
249
  }
240
250
  async getLanguages(isRemote = false) {
241
251
  const langs = store2.get("I18N_LANGUAGES");
@@ -260,7 +270,8 @@ export default class I18n {
260
270
  baseUrl: this.baseUrl,
261
271
  appCode: this.appCode,
262
272
  language: language ? language : this.locale,
263
- token: this.token
273
+ token: this.token,
274
+ routerName: this.name
264
275
  });
265
276
  if (res) {
266
277
  const remoteMessages = convertArrayToObject(res);
@@ -287,9 +298,11 @@ export default class I18n {
287
298
  emitter.on("closeModal", (payload) => {
288
299
  this.configInstance = null;
289
300
  });
301
+
290
302
  // 创建 config 组件的实例
291
303
  return createApp(configVue, {
292
304
  appCode: this.appCode,
305
+ router: this.router,
293
306
  token: this.token,
294
307
  setLanguage: this.setLanguage.bind(this, this.locale),
295
308
  name: name || this.name,
@@ -77,7 +77,8 @@ export const getTranslate = async ({
77
77
  baseUrl,
78
78
  appCode,
79
79
  language = "zh-CN",
80
- token
80
+ token,
81
+ routerName
81
82
  }) => {
82
83
  return new Promise(async (resolve, reject) => {
83
84
  // const appCodeStore = i18nStore.get(appCode);
@@ -107,7 +108,11 @@ export const getTranslate = async ({
107
108
  // 合并增量数据
108
109
  const langData = _.get(lastData, "translatesDTOs");
109
110
  if (!_.isEmpty(res.translatesDTOs)) {
110
- const data = mergeArraysByKey(langData, res.translatesDTOs);
111
+ const data = mergeArraysByKey(
112
+ langData,
113
+ res.translatesDTOs,
114
+ routerName
115
+ );
111
116
  const saveData = {
112
117
  lastPullDate: res.lastPullDate,
113
118
  translatesDTOs: data
@@ -7,8 +7,17 @@ export const convertArrayToObject = (arr) => {
7
7
  return result;
8
8
  };
9
9
 
10
- export const mergeArraysByKey = (arr1, arr2) => {
10
+ export const mergeArraysByKey = (arr1, arr2, routerName) => {
11
11
  arr2.forEach((obj2) => {
12
+ console.log("obj2.key ", obj2.key);
13
+ const type = obj2.key.split(".");
14
+ if (type[0] === "common") {
15
+ const findHome = _.find(arr1, { key: `${routerName}.${type[1]}` });
16
+ // 如果找到 删除
17
+ if (findHome) {
18
+ _.remove(arr1, { key: `${routerName}.${type[1]}` });
19
+ }
20
+ }
12
21
  const foundObj = _.find(arr1, { key: obj2.key });
13
22
  if (foundObj) {
14
23
  _.merge(foundObj, obj2);
package/src/view/Home.vue CHANGED
@@ -6,15 +6,26 @@ const change = async (lang) => {
6
6
  const clear = () => {
7
7
  $clearI18n();
8
8
  };
9
+ const login = () => {
10
+ $i18nLogin("abc");
11
+ };
12
+ const logout = () => {
13
+ $i18nLogout();
14
+ };
15
+ const type = import.meta.env.MODE === "unit" ? "单位级 单位代码: dd" : "模板级";
9
16
  </script>
10
17
 
11
18
  <template>
12
- <h1>首页</h1>
19
+ <h1>当前环境 -- {{ type }}</h1>
20
+
21
+ <div>首页</div>
13
22
  <RouterLink to="/test">测试</RouterLink>
14
23
  <h1>{{ $t("dsgljksdg") }}</h1>
15
24
  <button @click="change('en-US')">en</button>
16
25
  <button @click="change('zh-CN')">zh</button>
17
26
  <div>
18
27
  <button @click="clear">清理</button>
28
+ <button @click="login">登录</button>
29
+ <button @click="logout">登出</button>
19
30
  </div>
20
31
  </template>