@soluling/react-intl 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 +13 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +7 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -65,6 +65,17 @@ import { getOtaLanguages } from '@soluling/react-intl';
|
|
|
65
65
|
const published = await getOtaLanguages('<project GUID>', { fileFilter: fileName => fileName.endsWith('.json') });
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
+
## Several versions of the application
|
|
69
|
+
|
|
70
|
+
When the developers change the text of an id but keep the id, 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*. Give the hook the application's original messages, such as the extracted `en.json`, and it uses a published translation only when its original text is the same, and the application's own message for the others:
|
|
71
|
+
|
|
72
|
+
```jsx
|
|
73
|
+
import en from './translations/en.json'; // extracted with formatjs extract
|
|
74
|
+
|
|
75
|
+
const { messages, ready } = useOtaMessages(guid, language, own[language], { original: en });
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
A fix made in Soluling Cloud reaches all the versions whose text it is. Soluling Cloud publishes the originals when it builds the project; until then the published translations are used as they are.
|
|
68
79
|
## Options
|
|
69
80
|
|
|
70
81
|
The last parameter of `useOtaMessages` and `loadOtaMessages`:
|
|
@@ -73,6 +84,8 @@ The last parameter of `useOtaMessages` and `loadOtaMessages`:
|
|
|
73
84
|
| --- | --- | --- |
|
|
74
85
|
| `fileFilter` | the `.json` files | Which published files are the application's: `(fileName) => boolean` |
|
|
75
86
|
| `timeout` | `3000` | Milliseconds to wait for the first download |
|
|
87
|
+
| `original` | none | The application's original messages, to compare the original texts with |
|
|
88
|
+
| `guard` | `true` | Compare the original texts when `original` is given |
|
|
76
89
|
| `checkInterval` | a day | Milliseconds between the background checks for newer files; 0 checks on every load |
|
|
77
90
|
| `cache` | `true` | Save the files in the browser (localStorage) |
|
|
78
91
|
| `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,7 +2,10 @@ 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, string>;
|
|
5
|
-
export
|
|
5
|
+
export interface OtaMessagesOptions extends OtaOptions {
|
|
6
|
+
original?: Record<string, unknown> | null;
|
|
7
|
+
guard?: boolean;
|
|
8
|
+
}
|
|
6
9
|
/**
|
|
7
10
|
* The messages of the language: the ones published in Soluling Cloud, over the application's own
|
|
8
11
|
* messages, which keep the ids that the published file doesn't have. After the first visit, the
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
// The manifest, the browser cache, and the checks are in @soluling/ota, the over-the-air core of
|
|
17
17
|
// Soluling's JavaScript libraries.
|
|
18
18
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
19
|
-
import { loadOtaFile, withParents } from '@soluling/ota';
|
|
19
|
+
import { guardMessages, loadOtaFile, parseJson, withParents } from '@soluling/ota';
|
|
20
20
|
// The published languages, for the application's language list, so that an application needs only
|
|
21
21
|
// this package
|
|
22
22
|
export { getOtaLanguages, SOLULING_API_URL, SOLULING_CDN_URL } from '@soluling/ota';
|
|
@@ -63,13 +63,19 @@ export async function loadOtaMessages(projectGuid, language, ownMessages, option
|
|
|
63
63
|
let guid = (projectGuid || '').trim();
|
|
64
64
|
if (!guid || !language)
|
|
65
65
|
return own;
|
|
66
|
+
let appOriginal = ((options?.guard !== false) && options?.original) || null;
|
|
66
67
|
let file = await loadOtaFile(guid, withParents([language]), {
|
|
67
68
|
fileFilter: fileName => fileName.toLowerCase().endsWith('.json'),
|
|
68
69
|
...(options || {}),
|
|
70
|
+
originals: !!appOriginal,
|
|
69
71
|
});
|
|
70
72
|
let published = file ? parseMessages(file.data) : null;
|
|
71
73
|
if (!published)
|
|
72
74
|
return own;
|
|
75
|
+
// The translations of this version's texts: an id whose original text changed keeps the
|
|
76
|
+
// application's own message
|
|
77
|
+
if (appOriginal)
|
|
78
|
+
published = guardMessages(published, parseJson(file.original), appOriginal);
|
|
73
79
|
return { ...(own || {}), ...published };
|
|
74
80
|
}
|
|
75
81
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soluling/react-intl",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
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
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.
|
|
47
|
+
"@soluling/ota": "^1.3.0"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"react": ">=16.8"
|