@soluling/vue-i18n 0.0.0-stage → 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
@@ -1,3 +1,91 @@
1
- # Temporary Holding Version
1
+ # @soluling/vue-i18n
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Over-the-air translations from [Soluling Cloud](https://www.soluling.com/Documentation/OTA) for [Vue I18n](https://vue-i18n.intlify.dev) (Vue 3): when you translate in Soluling Cloud and publish the project's files, a translation fix or a new language reaches your users without a new build or deployment.
4
+
5
+ ```
6
+ npm install @soluling/vue-i18n
7
+ ```
8
+
9
+ ```js
10
+ import { createApp } from 'vue';
11
+ import { createI18n } from 'vue-i18n';
12
+ import { installOta } from '@soluling/vue-i18n';
13
+
14
+ const i18n = createI18n({ legacy: false, locale: 'fi', fallbackLocale: 'en', messages });
15
+ const ota = installOta(i18n, '<project GUID>'); // from the Overview tab of the project's Manage page
16
+
17
+ // Mount when the messages of the language are loaded
18
+ ota.ready.finally(() => {
19
+ createApp(App).use(i18n).provide('ota', ota).mount('#app');
20
+ });
21
+ ```
22
+
23
+ `installOta` merges the messages published in Soluling Cloud into Vue I18n: they replace the application's own messages of the language, and a key that the published file doesn't have keeps the application's own message. After the first visit, the messages saved in the browser come at once, and a newer file is checked for in the background once a day and used on the next load. When nothing is published for the language, the first download fails or takes longer than `timeout`, or the project GUID is empty, the application's own messages are used.
24
+
25
+ Make the Soluling Cloud project from the original file, such as `src/locales/en.json`, with *Vue I18n resource file* as the source type, and turn on *Publish artifacts* on the *Integrations* tab of the project's Manage page.
26
+
27
+ ## The controller
28
+
29
+ `installOta` returns:
30
+
31
+ | | |
32
+ | --- | --- |
33
+ | `ready` | Resolves when the messages of the current language are loaded. Mount the application after it, so it doesn't show its own translations first and then the published ones. |
34
+ | `load(language)` | Loads the published messages of a language. Call it before switching, so that a language added only in Soluling Cloud has its messages when it is shown. A locale changed without it is loaded too, a moment later. |
35
+ | `update()` | Checks Soluling Cloud for newer messages of the current language now and uses them at once, such as for a *Check for new translations* command. |
36
+
37
+ ```js
38
+ async function changeLanguage(value) {
39
+ await ota.load(value);
40
+ locale.value = value;
41
+ }
42
+ ```
43
+
44
+ ## Languages added later
45
+
46
+ A language that you add in Soluling Cloud reaches the users too: its published messages are used, although the application has none of its own for it. `getOtaLanguages` resolves to the published languages, such as `['de', 'fi', 'ja']`, so that the application's language list shows them:
47
+
48
+ ```js
49
+ import { getOtaLanguages } from '@soluling/vue-i18n';
50
+
51
+ const published = await getOtaLanguages('<project GUID>', { fileFilter: fileName => fileName.endsWith('.json') });
52
+ ```
53
+
54
+ ## Without the controller
55
+
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
+
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.
63
+ ## Options
64
+
65
+ The last parameter of `installOta` and `loadOtaMessages`:
66
+
67
+ | Option | Default | |
68
+ | --- | --- | --- |
69
+ | `fileFilter` | the `.json` files | Which published files are the application's: `(fileName) => boolean` |
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 |
73
+ | `checkInterval` | a day | Milliseconds between the background checks for newer files; 0 checks on every load |
74
+ | `cache` | `true` | Save the files in the browser (localStorage) |
75
+ | `cdnUrl` | `https://www.soluling.com/artifacts` | The published files on Soluling's CDN, where the manifest is read first |
76
+ | `apiUrl` | `https://www.soluling.com/api` | Soluling's API, for the manifest when the CDN doesn't have it |
77
+
78
+ It needs Vue 3 and Vue I18n 9 or later, in either mode (`legacy: false` or the legacy API). The manifest, the browser cache, and the checks are in [@soluling/ota](https://www.npmjs.com/package/@soluling/ota), the over-the-air core of Soluling's JavaScript libraries, which npm installs with this package. Vue is a peer dependency: the package uses the Vue of the application.
79
+
80
+ With i18next-vue, use [@soluling/i18next](https://www.npmjs.com/package/@soluling/i18next) instead.
81
+
82
+ See the [Vue I18n OTA sample](https://github.com/soluling/I18N/tree/master/Samples/Vue/Driving/vue-i18n-ota).
83
+
84
+ ## Build and test
85
+
86
+ ```
87
+ npm install
88
+ npm test
89
+ ```
90
+
91
+ The tests run a stand-in for Soluling's CDN and API, so they need no network, and run Vue I18n with the package.
@@ -0,0 +1,48 @@
1
+ import { OtaOptions } from '@soluling/ota';
2
+ export { getOtaLanguages, SOLULING_API_URL, SOLULING_CDN_URL } from '@soluling/ota';
3
+ export type { OtaOptions } from '@soluling/ota';
4
+ export type Messages = Record<string, unknown>;
5
+ export interface OtaMessagesOptions extends OtaOptions {
6
+ original?: Messages | null;
7
+ originalLanguage?: string;
8
+ guard?: boolean;
9
+ }
10
+ interface Composer {
11
+ locale: string | {
12
+ value: string;
13
+ };
14
+ fallbackLocale?: unknown;
15
+ getLocaleMessage?(locale: string): Messages;
16
+ setLocaleMessage?(locale: string, message: Messages): void;
17
+ mergeLocaleMessage(locale: string, message: Messages): void;
18
+ }
19
+ interface I18n {
20
+ global: Composer;
21
+ }
22
+ /**
23
+ * The messages of the language published in Soluling Cloud. After the first visit, the messages
24
+ * saved in the browser come at once, and a newer file is checked for in the background once a day
25
+ * and used on the next load. Resolves to null when nothing is published for the language, the
26
+ * first download fails or takes longer than timeout, or projectGuid is empty.
27
+ *
28
+ * @param projectGuid The GUID of the Soluling Cloud project, from its Manage page
29
+ * @param language The language, such as fi. A language with a region, such as fi-FI, uses the
30
+ * file of fi when it has none of its own.
31
+ */
32
+ export declare function loadOtaMessages(projectGuid: string, language: string, options?: OtaMessagesOptions | null): Promise<Messages | null>;
33
+ export interface OtaController {
34
+ ready: Promise<void>;
35
+ load(language: string): Promise<void>;
36
+ update(): Promise<void>;
37
+ }
38
+ /**
39
+ * Merges the messages published in Soluling Cloud into the messages of vue-i18n: those of the
40
+ * current language at once, and those of a new language when the locale changes. The published
41
+ * messages replace the application's own messages of the language; a key that the published file
42
+ * doesn't have keeps the application's own message. Without a project GUID, nothing is loaded and
43
+ * the application's own messages are used.
44
+ *
45
+ * @param i18n The instance that createI18n returns
46
+ * @param projectGuid The GUID of the Soluling Cloud project, from its Manage page
47
+ */
48
+ export declare function installOta(i18n: I18n, projectGuid: string, options?: OtaMessagesOptions | null): OtaController;
package/dist/index.js ADDED
@@ -0,0 +1,129 @@
1
+ // Over-the-air translations from Soluling Cloud for vue-i18n (Vue 3). The messages published in
2
+ // Soluling Cloud are merged into the messages of vue-i18n, so a translation fix or a new language
3
+ // reaches the users without a new build:
4
+ //
5
+ // import { createApp } from 'vue';
6
+ // import { createI18n } from 'vue-i18n';
7
+ // import { installOta } from '@soluling/vue-i18n';
8
+ //
9
+ // const i18n = createI18n({ legacy: false, locale: 'fi', fallbackLocale: 'en', messages });
10
+ // const ota = installOta(i18n, '<project GUID>');
11
+ //
12
+ // await ota.ready;
13
+ // createApp(App).use(i18n).mount('#app');
14
+ //
15
+ // The manifest, the browser cache, and the checks are in @soluling/ota, the over-the-air core of
16
+ // Soluling's JavaScript libraries.
17
+ import { watch } from 'vue';
18
+ import { guardMessages, loadOtaFile, parseJson, withParents } from '@soluling/ota';
19
+ // The published languages, for the application's language list, so that an application needs only
20
+ // this package
21
+ export { getOtaLanguages, SOLULING_API_URL, SOLULING_CDN_URL } from '@soluling/ota';
22
+ function getLocale(composer) {
23
+ let locale = composer.locale;
24
+ return (typeof locale === 'string') ? locale : locale.value;
25
+ }
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;
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);
62
+ }
63
+ /**
64
+ * The messages of the language published in Soluling Cloud. After the first visit, the messages
65
+ * saved in the browser come at once, and a newer file is checked for in the background once a day
66
+ * and used on the next load. Resolves to null when nothing is published for the language, the
67
+ * first download fails or takes longer than timeout, or projectGuid is empty.
68
+ *
69
+ * @param projectGuid The GUID of the Soluling Cloud project, from its Manage page
70
+ * @param language The language, such as fi. A language with a region, such as fi-FI, uses the
71
+ * file of fi when it has none of its own.
72
+ */
73
+ export async function loadOtaMessages(projectGuid, language, options) {
74
+ let guid = (projectGuid || '').trim();
75
+ if (!guid || !language)
76
+ return null;
77
+ let original = ((options?.guard !== false) && options?.original) || null;
78
+ let file = await loadOtaFile(guid, withParents([language]), {
79
+ fileFilter: fileName => fileName.toLowerCase().endsWith('.json'),
80
+ ...(options || {}),
81
+ originals: !!original,
82
+ });
83
+ // With the original messages: a key whose original text changed is left out
84
+ return getMessages(file, original);
85
+ }
86
+ /**
87
+ * Merges the messages published in Soluling Cloud into the messages of vue-i18n: those of the
88
+ * current language at once, and those of a new language when the locale changes. The published
89
+ * messages replace the application's own messages of the language; a key that the published file
90
+ * doesn't have keeps the application's own message. Without a project GUID, nothing is loaded and
91
+ * the application's own messages are used.
92
+ *
93
+ * @param i18n The instance that createI18n returns
94
+ * @param projectGuid The GUID of the Soluling Cloud project, from its Manage page
95
+ */
96
+ export function installOta(i18n, projectGuid, options) {
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 = {};
109
+ async function load(language, refresh = false) {
110
+ let messages = await loadOtaMessages(projectGuid, language, { ...(options || {}), original, refresh });
111
+ if (!messages)
112
+ return;
113
+ if (!canSet) {
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));
120
+ }
121
+ let ready = load(getLocale(composer));
122
+ // A locale set without load, such as by the application's own code, loads the messages too
123
+ watch(() => getLocale(composer), language => { load(language).catch(() => undefined); });
124
+ return {
125
+ ready,
126
+ load: language => load(language),
127
+ update: () => load(getLocale(composer), true),
128
+ };
129
+ }
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@soluling/vue-i18n",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.1.0",
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
+ "license": "MIT",
6
+ "author": "Soluling",
7
+ "homepage": "https://github.com/soluling/I18N/tree/master/Library/VueI18n#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/soluling/I18N"
11
+ },
12
+ "keywords": [
13
+ "vue-i18n",
14
+ "vue",
15
+ "nuxt",
16
+ "internationalization",
17
+ "i18n",
18
+ "localization",
19
+ "l10n",
20
+ "over-the-air",
21
+ "ota"
22
+ ],
23
+ "type": "module",
24
+ "main": "dist/index.js",
25
+ "module": "dist/index.js",
26
+ "types": "dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js",
31
+ "default": "./dist/index.js"
32
+ }
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "README.md"
37
+ ],
38
+ "sideEffects": false,
39
+ "scripts": {
40
+ "build": "tsc -p tsconfig.json",
41
+ "test": "npm run build && node --test \"test/*.test.mjs\"",
42
+ "prepublishOnly": "npm test",
43
+ "check": "npm pack --dry-run",
44
+ "release": "npm publish --access public"
45
+ },
46
+ "dependencies": {
47
+ "@soluling/ota": "^1.3.0"
48
+ },
49
+ "peerDependencies": {
50
+ "vue": ">=3.2"
51
+ },
52
+ "devDependencies": {
53
+ "typescript": "~5.9.3",
54
+ "vue": "^3.5.46",
55
+ "vue-i18n": "^11.4.24"
56
+ }
57
+ }