@soluling/vue-i18n 1.0.0 → 1.1.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/README.md CHANGED
@@ -55,6 +55,11 @@ const published = await getOtaLanguages('<project GUID>', { fileFilter: fileName
55
55
 
56
56
  `loadOtaMessages(projectGuid, language, options)` resolves to the published messages of a language, or null, for code that gives them to Vue I18n itself.
57
57
 
58
+ ## Several versions of the application
59
+
60
+ When the developers change the text of a key but keep the key, such as *Save* to *Save and close*, Soluling Cloud's translation is for the new text, and a version of the application built before the change still has *Save*. `installOta` compares the original texts: it uses a published translation only when its original text is the same as in the application's messages of the original language (`originalLanguage`, by default vue-i18n's `fallbackLocale`), and the application's own message for the others. A fix made in Soluling Cloud reaches all the versions whose text it is.
61
+
62
+ It needs nothing from you. Soluling Cloud publishes the originals when it builds the project; until then the published translations are used as they are. Set `guard: false` to use them always. `loadOtaMessages` compares only when you give it the `original` messages.
58
63
  ## Options
59
64
 
60
65
  The last parameter of `installOta` and `loadOtaMessages`:
@@ -63,6 +68,8 @@ The last parameter of `installOta` and `loadOtaMessages`:
63
68
  | --- | --- | --- |
64
69
  | `fileFilter` | the `.json` files | Which published files are the application's: `(fileName) => boolean` |
65
70
  | `timeout` | `3000` | Milliseconds to wait for the first download |
71
+ | `guard` | `true` | Use a published translation only when its original text is the same as in this version of the application |
72
+ | `originalLanguage` | vue-i18n's `fallbackLocale` | The language of the application's original messages |
66
73
  | `checkInterval` | a day | Milliseconds between the background checks for newer files; 0 checks on every load |
67
74
  | `cache` | `true` | Save the files in the browser (localStorage) |
68
75
  | `cdnUrl` | `https://www.soluling.com/artifacts` | The published files on Soluling's CDN, where the manifest is read first |
package/dist/index.d.ts CHANGED
@@ -2,11 +2,18 @@ import { OtaOptions } from '@soluling/ota';
2
2
  export { getOtaLanguages, SOLULING_API_URL, SOLULING_CDN_URL } from '@soluling/ota';
3
3
  export type { OtaOptions } from '@soluling/ota';
4
4
  export type Messages = Record<string, unknown>;
5
- export type OtaMessagesOptions = OtaOptions;
5
+ export interface OtaMessagesOptions extends OtaOptions {
6
+ original?: Messages | null;
7
+ originalLanguage?: string;
8
+ guard?: boolean;
9
+ }
6
10
  interface Composer {
7
11
  locale: string | {
8
12
  value: string;
9
13
  };
14
+ fallbackLocale?: unknown;
15
+ getLocaleMessage?(locale: string): Messages;
16
+ setLocaleMessage?(locale: string, message: Messages): void;
10
17
  mergeLocaleMessage(locale: string, message: Messages): void;
11
18
  }
12
19
  interface I18n {
package/dist/index.js CHANGED
@@ -15,7 +15,7 @@
15
15
  // The manifest, the browser cache, and the checks are in @soluling/ota, the over-the-air core of
16
16
  // Soluling's JavaScript libraries.
17
17
  import { watch } from 'vue';
18
- import { loadOtaFile, withParents } from '@soluling/ota';
18
+ import { guardMessages, loadOtaFile, parseJson, withParents } from '@soluling/ota';
19
19
  // The published languages, for the application's language list, so that an application needs only
20
20
  // this package
21
21
  export { getOtaLanguages, SOLULING_API_URL, SOLULING_CDN_URL } from '@soluling/ota';
@@ -23,14 +23,42 @@ function getLocale(composer) {
23
23
  let locale = composer.locale;
24
24
  return (typeof locale === 'string') ? locale : locale.value;
25
25
  }
26
- function parseMessages(data) {
27
- try {
28
- let json = JSON.parse(data);
29
- return (json && (typeof json === 'object') && !Array.isArray(json)) ? json : null;
30
- }
31
- catch {
32
- return null;
26
+ function isObject(value) {
27
+ return !!value && (typeof value === 'object') && !Array.isArray(value);
28
+ }
29
+ // The first language of vue-i18n's fallbackLocale: 'en', ['en'], { default: ['en'] }, or a ref of one
30
+ function getFallbackLanguage(value) {
31
+ if (typeof value === 'string')
32
+ return value;
33
+ if (Array.isArray(value))
34
+ return (typeof value[0] === 'string') ? value[0] : undefined;
35
+ if (isObject(value))
36
+ return ('value' in value) ? getFallbackLanguage(value.value) : getFallbackLanguage(value.default);
37
+ return undefined;
38
+ }
39
+ // A copy of the plain objects of messages; other values, such as compiled messages, are shared
40
+ function copyMessages(messages) {
41
+ let result = {};
42
+ for (let [key, value] of Object.entries(messages))
43
+ result[key] = isObject(value) && (Object.getPrototypeOf(value) === Object.prototype) ? copyMessages(value) : value;
44
+ return result;
45
+ }
46
+ // The messages of over merged over those of base: an object of both is merged, so that a key that
47
+ // over doesn't have keeps base's message
48
+ function mergeMessages(base, over) {
49
+ let result = { ...base };
50
+ for (let [key, value] of Object.entries(over)) {
51
+ let current = result[key];
52
+ result[key] = (isObject(current) && isObject(value)) ? mergeMessages(current, value) : value;
33
53
  }
54
+ return result;
55
+ }
56
+ // The published messages of a file, guarded with the application's original messages when given
57
+ function getMessages(file, original) {
58
+ let published = file ? parseJson(file.data) : null;
59
+ if (!published || !original)
60
+ return published;
61
+ return guardMessages(published, parseJson(file.original), original);
34
62
  }
35
63
  /**
36
64
  * The messages of the language published in Soluling Cloud. After the first visit, the messages
@@ -46,11 +74,14 @@ export async function loadOtaMessages(projectGuid, language, options) {
46
74
  let guid = (projectGuid || '').trim();
47
75
  if (!guid || !language)
48
76
  return null;
77
+ let original = ((options?.guard !== false) && options?.original) || null;
49
78
  let file = await loadOtaFile(guid, withParents([language]), {
50
79
  fileFilter: fileName => fileName.toLowerCase().endsWith('.json'),
51
80
  ...(options || {}),
81
+ originals: !!original,
52
82
  });
53
- return file ? parseMessages(file.data) : null;
83
+ // With the original messages: a key whose original text changed is left out
84
+ return getMessages(file, original);
54
85
  }
55
86
  /**
56
87
  * Merges the messages published in Soluling Cloud into the messages of vue-i18n: those of the
@@ -64,10 +95,28 @@ export async function loadOtaMessages(projectGuid, language, options) {
64
95
  */
65
96
  export function installOta(i18n, projectGuid, options) {
66
97
  let composer = i18n.global;
98
+ let canSet = !!composer.getLocaleMessage && !!composer.setLocaleMessage;
99
+ // The application's original messages, copied before anything is merged: the messages of the
100
+ // original language, which has no published file
101
+ let originalLanguage = options?.originalLanguage || getFallbackLanguage(composer.fallbackLocale) || 'en';
102
+ let originalMessages = canSet ? composer.getLocaleMessage(originalLanguage) : null;
103
+ let original = (options?.guard !== false) && isObject(originalMessages) && (Object.keys(originalMessages).length > 0) ?
104
+ copyMessages(originalMessages) : null;
105
+ // The application's own messages of each language, copied before the published ones are merged
106
+ // into them, so that a newer published file replaces the older one: a key that the guard leaves out
107
+ // now must not keep the message that an older file had
108
+ let own = {};
67
109
  async function load(language, refresh = false) {
68
- let messages = await loadOtaMessages(projectGuid, language, { ...(options || {}), refresh });
69
- if (messages)
110
+ let messages = await loadOtaMessages(projectGuid, language, { ...(options || {}), original, refresh });
111
+ if (!messages)
112
+ return;
113
+ if (!canSet) {
70
114
  composer.mergeLocaleMessage(language, messages);
115
+ return;
116
+ }
117
+ if (!(language in own))
118
+ own[language] = copyMessages(composer.getLocaleMessage(language) || {});
119
+ composer.setLocaleMessage(language, mergeMessages(own[language], messages));
71
120
  }
72
121
  let ready = load(getLocale(composer));
73
122
  // A locale set without load, such as by the application's own code, loads the messages too
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soluling/vue-i18n",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Over-the-air translations from Soluling Cloud for vue-i18n: merges the messages published in Soluling Cloud into vue-i18n (Vue 3)",
5
5
  "license": "MIT",
6
6
  "author": "Soluling",
@@ -44,7 +44,7 @@
44
44
  "release": "npm publish --access public"
45
45
  },
46
46
  "dependencies": {
47
- "@soluling/ota": "^1.0.0"
47
+ "@soluling/ota": "^1.3.0"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "vue": ">=3.2"