@soluling/vue-i18n 0.0.0-stage → 1.0.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 +83 -2
- package/dist/index.d.ts +41 -0
- package/dist/index.js +80 -0
- package/package.json +55 -4
package/README.md
CHANGED
|
@@ -1,3 +1,84 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @soluling/vue-i18n
|
|
2
2
|
|
|
3
|
-
|
|
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
|
+
## Options
|
|
59
|
+
|
|
60
|
+
The last parameter of `installOta` and `loadOtaMessages`:
|
|
61
|
+
|
|
62
|
+
| Option | Default | |
|
|
63
|
+
| --- | --- | --- |
|
|
64
|
+
| `fileFilter` | the `.json` files | Which published files are the application's: `(fileName) => boolean` |
|
|
65
|
+
| `timeout` | `3000` | Milliseconds to wait for the first download |
|
|
66
|
+
| `checkInterval` | a day | Milliseconds between the background checks for newer files; 0 checks on every load |
|
|
67
|
+
| `cache` | `true` | Save the files in the browser (localStorage) |
|
|
68
|
+
| `cdnUrl` | `https://www.soluling.com/artifacts` | The published files on Soluling's CDN, where the manifest is read first |
|
|
69
|
+
| `apiUrl` | `https://www.soluling.com/api` | Soluling's API, for the manifest when the CDN doesn't have it |
|
|
70
|
+
|
|
71
|
+
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.
|
|
72
|
+
|
|
73
|
+
With i18next-vue, use [@soluling/i18next](https://www.npmjs.com/package/@soluling/i18next) instead.
|
|
74
|
+
|
|
75
|
+
See the [Vue I18n OTA sample](https://github.com/soluling/I18N/tree/master/Samples/Vue/Driving/vue-i18n-ota).
|
|
76
|
+
|
|
77
|
+
## Build and test
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
npm install
|
|
81
|
+
npm test
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The tests run a stand-in for Soluling's CDN and API, so they need no network, and run Vue I18n with the package.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
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 type OtaMessagesOptions = OtaOptions;
|
|
6
|
+
interface Composer {
|
|
7
|
+
locale: string | {
|
|
8
|
+
value: string;
|
|
9
|
+
};
|
|
10
|
+
mergeLocaleMessage(locale: string, message: Messages): void;
|
|
11
|
+
}
|
|
12
|
+
interface I18n {
|
|
13
|
+
global: Composer;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The messages of the language published in Soluling Cloud. After the first visit, the messages
|
|
17
|
+
* saved in the browser come at once, and a newer file is checked for in the background once a day
|
|
18
|
+
* and used on the next load. Resolves to null when nothing is published for the language, the
|
|
19
|
+
* first download fails or takes longer than timeout, or projectGuid is empty.
|
|
20
|
+
*
|
|
21
|
+
* @param projectGuid The GUID of the Soluling Cloud project, from its Manage page
|
|
22
|
+
* @param language The language, such as fi. A language with a region, such as fi-FI, uses the
|
|
23
|
+
* file of fi when it has none of its own.
|
|
24
|
+
*/
|
|
25
|
+
export declare function loadOtaMessages(projectGuid: string, language: string, options?: OtaMessagesOptions | null): Promise<Messages | null>;
|
|
26
|
+
export interface OtaController {
|
|
27
|
+
ready: Promise<void>;
|
|
28
|
+
load(language: string): Promise<void>;
|
|
29
|
+
update(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Merges the messages published in Soluling Cloud into the messages of vue-i18n: those of the
|
|
33
|
+
* current language at once, and those of a new language when the locale changes. The published
|
|
34
|
+
* messages replace the application's own messages of the language; a key that the published file
|
|
35
|
+
* doesn't have keeps the application's own message. Without a project GUID, nothing is loaded and
|
|
36
|
+
* the application's own messages are used.
|
|
37
|
+
*
|
|
38
|
+
* @param i18n The instance that createI18n returns
|
|
39
|
+
* @param projectGuid The GUID of the Soluling Cloud project, from its Manage page
|
|
40
|
+
*/
|
|
41
|
+
export declare function installOta(i18n: I18n, projectGuid: string, options?: OtaMessagesOptions | null): OtaController;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
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 { loadOtaFile, 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 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;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The messages of the language published in Soluling Cloud. After the first visit, the messages
|
|
37
|
+
* saved in the browser come at once, and a newer file is checked for in the background once a day
|
|
38
|
+
* and used on the next load. Resolves to null when nothing is published for the language, the
|
|
39
|
+
* first download fails or takes longer than timeout, or projectGuid is empty.
|
|
40
|
+
*
|
|
41
|
+
* @param projectGuid The GUID of the Soluling Cloud project, from its Manage page
|
|
42
|
+
* @param language The language, such as fi. A language with a region, such as fi-FI, uses the
|
|
43
|
+
* file of fi when it has none of its own.
|
|
44
|
+
*/
|
|
45
|
+
export async function loadOtaMessages(projectGuid, language, options) {
|
|
46
|
+
let guid = (projectGuid || '').trim();
|
|
47
|
+
if (!guid || !language)
|
|
48
|
+
return null;
|
|
49
|
+
let file = await loadOtaFile(guid, withParents([language]), {
|
|
50
|
+
fileFilter: fileName => fileName.toLowerCase().endsWith('.json'),
|
|
51
|
+
...(options || {}),
|
|
52
|
+
});
|
|
53
|
+
return file ? parseMessages(file.data) : null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Merges the messages published in Soluling Cloud into the messages of vue-i18n: those of the
|
|
57
|
+
* current language at once, and those of a new language when the locale changes. The published
|
|
58
|
+
* messages replace the application's own messages of the language; a key that the published file
|
|
59
|
+
* doesn't have keeps the application's own message. Without a project GUID, nothing is loaded and
|
|
60
|
+
* the application's own messages are used.
|
|
61
|
+
*
|
|
62
|
+
* @param i18n The instance that createI18n returns
|
|
63
|
+
* @param projectGuid The GUID of the Soluling Cloud project, from its Manage page
|
|
64
|
+
*/
|
|
65
|
+
export function installOta(i18n, projectGuid, options) {
|
|
66
|
+
let composer = i18n.global;
|
|
67
|
+
async function load(language, refresh = false) {
|
|
68
|
+
let messages = await loadOtaMessages(projectGuid, language, { ...(options || {}), refresh });
|
|
69
|
+
if (messages)
|
|
70
|
+
composer.mergeLocaleMessage(language, messages);
|
|
71
|
+
}
|
|
72
|
+
let ready = load(getLocale(composer));
|
|
73
|
+
// A locale set without load, such as by the application's own code, loads the messages too
|
|
74
|
+
watch(() => getLocale(composer), language => { load(language).catch(() => undefined); });
|
|
75
|
+
return {
|
|
76
|
+
ready,
|
|
77
|
+
load: language => load(language),
|
|
78
|
+
update: () => load(getLocale(composer), true),
|
|
79
|
+
};
|
|
80
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soluling/vue-i18n",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.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.0.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
|
+
}
|