@soluling/react-intl 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 +91 -2
- package/dist/index.d.ts +29 -0
- package/dist/index.js +103 -0
- package/package.json +58 -4
package/README.md
CHANGED
|
@@ -1,3 +1,92 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @soluling/react-intl
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Over-the-air translations from [Soluling Cloud](https://www.soluling.com/Documentation/OTA) for [React Intl](https://formatjs.github.io/docs/react-intl) (react-intl, FormatJS): 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/react-intl
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```jsx
|
|
10
|
+
import { IntlProvider } from 'react-intl';
|
|
11
|
+
import { useOtaMessages } from '@soluling/react-intl';
|
|
12
|
+
|
|
13
|
+
import fi from './translations/fi.json';
|
|
14
|
+
import de from './translations/de.json';
|
|
15
|
+
|
|
16
|
+
const messages = { fi, de };
|
|
17
|
+
|
|
18
|
+
function Root({ language }) {
|
|
19
|
+
const { messages: languageMessages, ready } = useOtaMessages('<project GUID>', language, messages[language]);
|
|
20
|
+
|
|
21
|
+
if (!ready)
|
|
22
|
+
return null;
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<IntlProvider locale={language} defaultLocale="en" messages={languageMessages}>
|
|
26
|
+
<App />
|
|
27
|
+
</IntlProvider>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`useOtaMessages` gives the messages of the language: the ones published in Soluling Cloud, over the application's own messages, which keep the ids that the published file doesn't have. After the first visit, the published 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. It loads the messages again when the language changes.
|
|
33
|
+
|
|
34
|
+
`ready` is false until the messages of the language are loaded: render `IntlProvider` when it is true, so the application doesn't show its own translations first and then the published ones.
|
|
35
|
+
|
|
36
|
+
Make the Soluling Cloud project from the extracted `en.json`, with *React-Intl resource file* as the source type, and turn on *Publish artifacts* on the *Integrations* tab of the project's Manage page. Soluling Cloud publishes `fi.json` and the others in React Intl's runtime format.
|
|
37
|
+
|
|
38
|
+
## Check for new translations
|
|
39
|
+
|
|
40
|
+
The hook also returns `update`, which checks Soluling Cloud for newer messages now and uses them at once, such as for a *Check for new translations* command:
|
|
41
|
+
|
|
42
|
+
```jsx
|
|
43
|
+
const { messages, ready, update } = useOtaMessages(guid, language, own);
|
|
44
|
+
|
|
45
|
+
<button onClick={update}>Check for new translations</button>
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Without the hook
|
|
49
|
+
|
|
50
|
+
`loadOtaMessages(projectGuid, language, ownMessages, options)` resolves to the same messages, for code outside a component, or before rendering:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
import { loadOtaMessages } from '@soluling/react-intl';
|
|
54
|
+
|
|
55
|
+
const messages = await loadOtaMessages('<project GUID>', 'fi', own.fi);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Languages added later
|
|
59
|
+
|
|
60
|
+
A language that you add in Soluling Cloud reaches the users too: the published messages are used for it, although the application has no translations 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:
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
import { getOtaLanguages } from '@soluling/react-intl';
|
|
64
|
+
|
|
65
|
+
const published = await getOtaLanguages('<project GUID>', { fileFilter: fileName => fileName.endsWith('.json') });
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Options
|
|
69
|
+
|
|
70
|
+
The last parameter of `useOtaMessages` and `loadOtaMessages`:
|
|
71
|
+
|
|
72
|
+
| Option | Default | |
|
|
73
|
+
| --- | --- | --- |
|
|
74
|
+
| `fileFilter` | the `.json` files | Which published files are the application's: `(fileName) => boolean` |
|
|
75
|
+
| `timeout` | `3000` | Milliseconds to wait for the first download |
|
|
76
|
+
| `checkInterval` | a day | Milliseconds between the background checks for newer files; 0 checks on every load |
|
|
77
|
+
| `cache` | `true` | Save the files in the browser (localStorage) |
|
|
78
|
+
| `cdnUrl` | `https://www.soluling.com/artifacts` | The published files on Soluling's CDN, where the manifest is read first |
|
|
79
|
+
| `apiUrl` | `https://www.soluling.com/api` | Soluling's API, for the manifest when the CDN doesn't have it |
|
|
80
|
+
|
|
81
|
+
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. React is a peer dependency: the package uses the React of the application, version 16.8 or later.
|
|
82
|
+
|
|
83
|
+
See the [React Intl OTA sample](https://github.com/soluling/I18N/tree/master/Samples/React/Driving/react-intl-ota).
|
|
84
|
+
|
|
85
|
+
## Build and test
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
npm install
|
|
89
|
+
npm test
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The tests run a stand-in for Soluling's CDN and API, so they need no network. Some of them render React Intl's `IntlProvider` with the hook in a browser simulated by happy-dom.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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, string>;
|
|
5
|
+
export type OtaMessagesOptions = OtaOptions;
|
|
6
|
+
/**
|
|
7
|
+
* The messages of the language: the ones published in Soluling Cloud, over the application's own
|
|
8
|
+
* messages, which keep the ids that the published file doesn't have. After the first visit, the
|
|
9
|
+
* published messages saved in the browser come at once, and a newer file is checked for in the
|
|
10
|
+
* background once a day and used on the next load. Resolves to the application's own messages
|
|
11
|
+
* when nothing is published for the language, the first download fails or takes longer than
|
|
12
|
+
* timeout, or projectGuid is empty.
|
|
13
|
+
*
|
|
14
|
+
* @param projectGuid The GUID of the Soluling Cloud project, from its Manage page
|
|
15
|
+
* @param language The language of the messages, such as fi. A language with a region, such as
|
|
16
|
+
* fi-FI, uses the file of fi when it has none of its own.
|
|
17
|
+
* @param ownMessages The application's own messages of the language, if any
|
|
18
|
+
*/
|
|
19
|
+
export declare function loadOtaMessages(projectGuid: string, language: string, ownMessages?: Messages | null, options?: OtaMessagesOptions | null): Promise<Messages | undefined>;
|
|
20
|
+
export interface OtaMessages {
|
|
21
|
+
messages: Messages | undefined;
|
|
22
|
+
ready: boolean;
|
|
23
|
+
update: () => Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The messages of the language for React Intl's IntlProvider, with the published ones over the
|
|
27
|
+
* application's own: see loadOtaMessages. Loads them again when the language changes.
|
|
28
|
+
*/
|
|
29
|
+
export declare function useOtaMessages(projectGuid: string, language: string, ownMessages?: Messages | null, options?: OtaMessagesOptions | null): OtaMessages;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Over-the-air translations from Soluling Cloud for React Intl (react-intl, FormatJS). The messages
|
|
2
|
+
// published in Soluling Cloud replace the application's own messages of the language, so a
|
|
3
|
+
// translation fix or a new language reaches the users without a new build:
|
|
4
|
+
//
|
|
5
|
+
// import { IntlProvider } from 'react-intl';
|
|
6
|
+
// import { useOtaMessages } from '@soluling/react-intl';
|
|
7
|
+
//
|
|
8
|
+
// const { messages, ready } = useOtaMessages('<project GUID>', language, ownMessages[language]);
|
|
9
|
+
//
|
|
10
|
+
// return ready && (
|
|
11
|
+
// <IntlProvider locale={language} defaultLocale="en" messages={messages}>
|
|
12
|
+
// <App />
|
|
13
|
+
// </IntlProvider>
|
|
14
|
+
// );
|
|
15
|
+
//
|
|
16
|
+
// The manifest, the browser cache, and the checks are in @soluling/ota, the over-the-air core of
|
|
17
|
+
// Soluling's JavaScript libraries.
|
|
18
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
19
|
+
import { loadOtaFile, withParents } from '@soluling/ota';
|
|
20
|
+
// The published languages, for the application's language list, so that an application needs only
|
|
21
|
+
// this package
|
|
22
|
+
export { getOtaLanguages, SOLULING_API_URL, SOLULING_CDN_URL } from '@soluling/ota';
|
|
23
|
+
// The messages of a published file. A runtime file is { "id": "text" }; a value that is an object,
|
|
24
|
+
// as in an extracted file, gives its defaultMessage or message.
|
|
25
|
+
function parseMessages(data) {
|
|
26
|
+
let json;
|
|
27
|
+
try {
|
|
28
|
+
json = JSON.parse(data);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
if (!json || (typeof json !== 'object') || Array.isArray(json))
|
|
34
|
+
return null;
|
|
35
|
+
let messages = {};
|
|
36
|
+
for (let [id, value] of Object.entries(json)) {
|
|
37
|
+
if (typeof value === 'string')
|
|
38
|
+
messages[id] = value;
|
|
39
|
+
else if (value && (typeof value === 'object')) {
|
|
40
|
+
let item = value;
|
|
41
|
+
let text = (typeof item.defaultMessage === 'string') ? item.defaultMessage : item.message;
|
|
42
|
+
if (typeof text === 'string')
|
|
43
|
+
messages[id] = text;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return messages;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The messages of the language: the ones published in Soluling Cloud, over the application's own
|
|
50
|
+
* messages, which keep the ids that the published file doesn't have. After the first visit, the
|
|
51
|
+
* published messages saved in the browser come at once, and a newer file is checked for in the
|
|
52
|
+
* background once a day and used on the next load. Resolves to the application's own messages
|
|
53
|
+
* when nothing is published for the language, the first download fails or takes longer than
|
|
54
|
+
* timeout, or projectGuid is empty.
|
|
55
|
+
*
|
|
56
|
+
* @param projectGuid The GUID of the Soluling Cloud project, from its Manage page
|
|
57
|
+
* @param language The language of the messages, such as fi. A language with a region, such as
|
|
58
|
+
* fi-FI, uses the file of fi when it has none of its own.
|
|
59
|
+
* @param ownMessages The application's own messages of the language, if any
|
|
60
|
+
*/
|
|
61
|
+
export async function loadOtaMessages(projectGuid, language, ownMessages, options) {
|
|
62
|
+
let own = ownMessages || undefined;
|
|
63
|
+
let guid = (projectGuid || '').trim();
|
|
64
|
+
if (!guid || !language)
|
|
65
|
+
return own;
|
|
66
|
+
let file = await loadOtaFile(guid, withParents([language]), {
|
|
67
|
+
fileFilter: fileName => fileName.toLowerCase().endsWith('.json'),
|
|
68
|
+
...(options || {}),
|
|
69
|
+
});
|
|
70
|
+
let published = file ? parseMessages(file.data) : null;
|
|
71
|
+
if (!published)
|
|
72
|
+
return own;
|
|
73
|
+
return { ...(own || {}), ...published };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The messages of the language for React Intl's IntlProvider, with the published ones over the
|
|
77
|
+
* application's own: see loadOtaMessages. Loads them again when the language changes.
|
|
78
|
+
*/
|
|
79
|
+
export function useOtaMessages(projectGuid, language, ownMessages, options) {
|
|
80
|
+
let [state, setState] = useState(null);
|
|
81
|
+
// The options are read when loading, so that an options object made on each render doesn't
|
|
82
|
+
// load the messages again
|
|
83
|
+
let optionsRef = useRef(options);
|
|
84
|
+
optionsRef.current = options;
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
let active = true;
|
|
87
|
+
loadOtaMessages(projectGuid, language, ownMessages, optionsRef.current).then(messages => {
|
|
88
|
+
if (active)
|
|
89
|
+
setState({ language, messages });
|
|
90
|
+
});
|
|
91
|
+
return () => { active = false; };
|
|
92
|
+
}, [projectGuid, language, ownMessages]);
|
|
93
|
+
let update = useCallback(async () => {
|
|
94
|
+
let messages = await loadOtaMessages(projectGuid, language, ownMessages, { ...(optionsRef.current || {}), refresh: true });
|
|
95
|
+
setState({ language, messages });
|
|
96
|
+
}, [projectGuid, language, ownMessages]);
|
|
97
|
+
let ready = (state !== null) && (state.language === language);
|
|
98
|
+
return {
|
|
99
|
+
messages: ready ? state.messages : (ownMessages || undefined),
|
|
100
|
+
ready,
|
|
101
|
+
update,
|
|
102
|
+
};
|
|
103
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soluling/react-intl",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Over-the-air translations from Soluling Cloud for React Intl (react-intl, FormatJS): a hook that gives IntlProvider the messages published in Soluling Cloud",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Soluling",
|
|
7
|
+
"homepage": "https://github.com/soluling/I18N/tree/master/Library/ReactIntl#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/soluling/I18N"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"react-intl",
|
|
14
|
+
"formatjs",
|
|
15
|
+
"react",
|
|
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
|
+
"react": ">=16.8"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@happy-dom/global-registrator": "^21.0.4",
|
|
54
|
+
"@types/react": "^19.2.0",
|
|
55
|
+
"react": "^19.3.1",
|
|
56
|
+
"react-dom": "^19.3.1",
|
|
57
|
+
"react-intl": "^12.2.2",
|
|
58
|
+
"typescript": "~5.9.3"
|
|
59
|
+
}
|
|
60
|
+
}
|