gt-react 8.2.21 → 8.2.23
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 +76 -9
- package/package.json +8 -6
- package/dist/errors/createErrors.d.ts +0 -13
- package/dist/errors/createErrors.d.ts.map +0 -1
- package/dist/errors/createErrors.js +0 -28
- package/dist/errors/createErrors.js.map +0 -1
- package/dist/provider/dynamic/useDynamicTranslation.d.ts +0 -28
- package/dist/provider/dynamic/useDynamicTranslation.d.ts.map +0 -1
- package/dist/provider/dynamic/useDynamicTranslation.js +0 -194
- package/dist/provider/dynamic/useDynamicTranslation.js.map +0 -1
- package/dist/provider/dynamic/useRuntimeTranslation.d.ts +0 -28
- package/dist/provider/dynamic/useRuntimeTranslation.d.ts.map +0 -1
- package/dist/provider/dynamic/useRuntimeTranslation.js +0 -194
- package/dist/provider/dynamic/useRuntimeTranslation.js.map +0 -1
- package/dist/translate/translateDictionaryStrings.d.ts +0 -3
- package/dist/translate/translateDictionaryStrings.d.ts.map +0 -1
- package/dist/translate/translateDictionaryStrings.js +0 -97
- package/dist/translate/translateDictionaryStrings.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,21 +1,88 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://generaltranslation.com" target="_blank">
|
|
3
|
+
<img src="https://generaltranslation.com/gt-logo-light.svg" alt="General Translation" width="100" height="100">
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
# gt-react: Automatic i18n for React
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
gt-react is a powerful internationalization library designed for React applications. It replaces your existing localization library, and integrates with [generaltranslation.com](https://generaltranslation.com) for translations.
|
|
6
10
|
|
|
7
|
-
See [
|
|
11
|
+
See our [docs](https://www.generaltranslation.com/docs) for more information including guides, examples, and API references.
|
|
8
12
|
|
|
9
13
|
## Installation
|
|
10
14
|
|
|
15
|
+
Install `gt-react` via npm:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install gt-react
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or with yarn:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
yarn add gt-react
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Getting Started
|
|
28
|
+
|
|
29
|
+
### Step 1: Configure Your Environment Variables
|
|
30
|
+
|
|
31
|
+
Add the following environment variables to your `.env` file:
|
|
32
|
+
|
|
11
33
|
```
|
|
12
|
-
|
|
34
|
+
GT_API_KEY="your-api-key"
|
|
35
|
+
GT_PROJECT_ID="your-project-id"
|
|
13
36
|
```
|
|
14
37
|
|
|
15
|
-
|
|
38
|
+
- Get your `API Key` and `Project ID` from the [General Translation Dashboard](https://www.generaltranslation.com).
|
|
39
|
+
|
|
40
|
+
### Step 2: Add the `<GTProvider>`
|
|
41
|
+
|
|
42
|
+
Add the `<GTProvider>` component at the root of your application.
|
|
43
|
+
|
|
44
|
+
```jsx
|
|
45
|
+
createRoot(document.getElementById("root")!).render(
|
|
46
|
+
<StrictMode>
|
|
47
|
+
<GTProvider
|
|
48
|
+
projectId={GT_PROJECT_ID}
|
|
49
|
+
devApiKey={GT_API_KEY}
|
|
50
|
+
>
|
|
51
|
+
<App />
|
|
52
|
+
</GTProvider>
|
|
53
|
+
</StrictMode>
|
|
54
|
+
);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Step 3: Translate Content with `<T>`
|
|
58
|
+
|
|
59
|
+
The `<T>` component is the simplest way to translate inline JSX content.
|
|
60
|
+
|
|
61
|
+
```jsx
|
|
62
|
+
import { T } from 'gt-react';
|
|
63
|
+
|
|
64
|
+
export default function HomePage() {
|
|
65
|
+
return (
|
|
66
|
+
<T id='greeting'>
|
|
67
|
+
<p>Hello, world!</p>
|
|
68
|
+
</T>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
If you have an existing project you would like to internationalize, you can use the `gt-react-cli` tool for initial setup.
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
npm install gt-react-cli
|
|
77
|
+
npx gt-react-cli scan
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
This will scan your project for all the text content that needs to be translated, and automatically wrap them in `<T>` and `<Var>` components.
|
|
81
|
+
|
|
82
|
+
## Documentation
|
|
16
83
|
|
|
17
|
-
|
|
84
|
+
Full documentation, including guides, examples, and API references, can be found at [General Translation Docs](www.generaltranslation.com/docs).
|
|
18
85
|
|
|
19
|
-
##
|
|
86
|
+
## Contributing
|
|
20
87
|
|
|
21
|
-
|
|
88
|
+
We welcome any contributions to our libraries. Please submit a pull request!
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gt-react",
|
|
3
|
-
"version": "8.2.
|
|
3
|
+
"version": "8.2.23",
|
|
4
4
|
"description": "A React library for automatic internationalization.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -12,18 +12,20 @@
|
|
|
12
12
|
"patch": "npm version patch",
|
|
13
13
|
"transpile": "tsc",
|
|
14
14
|
"build": "npm run transpile",
|
|
15
|
+
"clean:build": "rm -rf dist; npm run build",
|
|
16
|
+
"publish": "npm run clean:build && npm publish",
|
|
15
17
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
16
18
|
},
|
|
17
19
|
"repository": {
|
|
18
20
|
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/general-translation/gt-
|
|
21
|
+
"url": "git+https://github.com/general-translation/gt-libraries.git"
|
|
20
22
|
},
|
|
21
23
|
"author": "General Translation, Inc.",
|
|
22
|
-
"license": "
|
|
24
|
+
"license": "FSL-1.1-ALv2",
|
|
23
25
|
"bugs": {
|
|
24
|
-
"url": "https://github.com/general-translation/gt-
|
|
26
|
+
"url": "https://github.com/general-translation/gt-libraries/issues"
|
|
25
27
|
},
|
|
26
|
-
"homepage": "https://github.com/general-translation/gt-
|
|
28
|
+
"homepage": "https://github.com/general-translation/gt-libraries#readme",
|
|
27
29
|
"devDependencies": {
|
|
28
30
|
"@types/node": ">=20.0.0 <23.0.0",
|
|
29
31
|
"@types/react": ">=18.0.0 <20.0.0",
|
|
@@ -73,7 +75,7 @@
|
|
|
73
75
|
"i18n"
|
|
74
76
|
],
|
|
75
77
|
"dependencies": {
|
|
76
|
-
"@generaltranslation/supported-locales": "^1.1.
|
|
78
|
+
"@generaltranslation/supported-locales": "^1.1.3",
|
|
77
79
|
"generaltranslation": "^6.1.4"
|
|
78
80
|
}
|
|
79
81
|
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export declare const projectIdMissingError = "gt-react Error: General Translation cloud services require a project ID! Find yours at www.generaltranslation.com/dashboard.";
|
|
2
|
-
export declare const createPluralMissingError: (children: any) => string;
|
|
3
|
-
export declare const createClientSideTWithoutIdError: (children: any) => string;
|
|
4
|
-
export declare const createStringTranslationError: (content: string, id?: string) => string;
|
|
5
|
-
export declare const createClientSideTDictionaryCollisionError: (id: string) => string;
|
|
6
|
-
export declare const createClientSideTHydrationError: (id: string) => string;
|
|
7
|
-
export declare const createNestedDataGTError: (child: any) => string;
|
|
8
|
-
export declare const createNestedTError: (child: any) => string;
|
|
9
|
-
export declare const renderingError = "General Translation: Rendering error.";
|
|
10
|
-
export declare const dynamicTranslationError = "Error fetching batched translations:";
|
|
11
|
-
export declare const createLibraryNoEntryWarning: (id: string) => string;
|
|
12
|
-
export declare const createNoEntryWarning: (id: string, prefixedId: string) => string;
|
|
13
|
-
//# sourceMappingURL=createErrors.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createErrors.d.ts","sourceRoot":"","sources":["../../src/errors/createErrors.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,qBAAqB,iIAAiI,CAAA;AAEnK,eAAO,MAAM,wBAAwB,aAAc,GAAG,WAA0E,CAAA;AAEhI,eAAO,MAAM,+BAA+B,aAAc,GAAG,WAA0E,CAAA;AAEvI,eAAO,MAAM,4BAA4B,YAAa,MAAM,OAAO,MAAM,WAAiG,CAAA;AAE1K,eAAO,MAAM,yCAAyC,OAAQ,MAAM,WAAoI,CAAA;AAExM,eAAO,MAAM,+BAA+B,OAAQ,MAAM,WACuE,CAAA;AAEjI,eAAO,MAAM,uBAAuB,UAAW,GAAG,WAAmM,CAAA;AAErP,eAAO,MAAM,kBAAkB,UAAW,GAAG,WAAmG,CAAA;AAEhJ,eAAO,MAAM,cAAc,0CAA0C,CAAA;AAErE,eAAO,MAAM,uBAAuB,yCAAyC,CAAA;AAI7E,eAAO,MAAM,2BAA2B,OAAQ,MAAM,WAA0D,CAAA;AAEhH,eAAO,MAAM,oBAAoB,OAAQ,MAAM,cAAc,MAAM,WAA4E,CAAA"}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// ---- ERRORS ---- //
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.createNoEntryWarning = exports.createLibraryNoEntryWarning = exports.dynamicTranslationError = exports.renderingError = exports.createNestedTError = exports.createNestedDataGTError = exports.createClientSideTHydrationError = exports.createClientSideTDictionaryCollisionError = exports.createStringTranslationError = exports.createClientSideTWithoutIdError = exports.createPluralMissingError = exports.projectIdMissingError = void 0;
|
|
5
|
-
exports.projectIdMissingError = 'gt-react Error: General Translation cloud services require a project ID! Find yours at www.generaltranslation.com/dashboard.';
|
|
6
|
-
var createPluralMissingError = function (children) { return "<Plural> component with children \"".concat(children, "\" requires \"n\" option."); };
|
|
7
|
-
exports.createPluralMissingError = createPluralMissingError;
|
|
8
|
-
var createClientSideTWithoutIdError = function (children) { return "Client-side <T> with no provided 'id' prop. Children: \"".concat(children, "\""); };
|
|
9
|
-
exports.createClientSideTWithoutIdError = createClientSideTWithoutIdError;
|
|
10
|
-
var createStringTranslationError = function (content, id) { return "gt-next string translation error. tx(\"".concat(content, "\")").concat(id ? " with id \"".concat(id, "\"") : '', " failed."); };
|
|
11
|
-
exports.createStringTranslationError = createStringTranslationError;
|
|
12
|
-
var createClientSideTDictionaryCollisionError = function (id) { return "<T id=\"".concat(id, "\">, \"").concat(id, "\" is also used as a key in the dictionary. Don't give <T> components the same ID as dictionary entries."); };
|
|
13
|
-
exports.createClientSideTDictionaryCollisionError = createClientSideTDictionaryCollisionError;
|
|
14
|
-
var createClientSideTHydrationError = function (id) { return "<T id=\"".concat(id, "\"> is used in a client component without a valid saved translation. This can cause hydration errors.")
|
|
15
|
-
+ "\n\nTo fix this error, consider using a dictionary with useGT() or pushing translations from the command line in advance."; };
|
|
16
|
-
exports.createClientSideTHydrationError = createClientSideTHydrationError;
|
|
17
|
-
var createNestedDataGTError = function (child) { return "General Translation already in use on child with props: ".concat(child.props, ". This usually occurs when you nest <T> components within the same file. Remove one of the <T> components to continue."); };
|
|
18
|
-
exports.createNestedDataGTError = createNestedDataGTError;
|
|
19
|
-
var createNestedTError = function (child) { var _a; return "General Translation: Nested <T> components. The inner <T> has the id: \"".concat((_a = child === null || child === void 0 ? void 0 : child.props) === null || _a === void 0 ? void 0 : _a.id, "\"."); };
|
|
20
|
-
exports.createNestedTError = createNestedTError;
|
|
21
|
-
exports.renderingError = 'General Translation: Rendering error.';
|
|
22
|
-
exports.dynamicTranslationError = "Error fetching batched translations:";
|
|
23
|
-
// ---- WARNINGS ---- //
|
|
24
|
-
var createLibraryNoEntryWarning = function (id) { return "gt-react: No dictionary entry found for id: \"".concat(id, "\""); };
|
|
25
|
-
exports.createLibraryNoEntryWarning = createLibraryNoEntryWarning;
|
|
26
|
-
var createNoEntryWarning = function (id, prefixedId) { return "t('".concat(id, "') finding no translation for dictionary item ").concat(prefixedId, " !"); };
|
|
27
|
-
exports.createNoEntryWarning = createNoEntryWarning;
|
|
28
|
-
//# sourceMappingURL=createErrors.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createErrors.js","sourceRoot":"","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wB;AAEjB,IAAM,2BAA2B,GAAG,UAAC,EAAU,IAAK,OAAA,wDAAgD,EAAE,OAAG,EAArD,CAAqD,CAAA;AAAnG,QAAA,2BAA2B,+BAAwE;AAEzG,IAAM,oBAAoB,GAAG,UAAC,EAAU,EAAE,UAAkB,IAAK,OAAA,aAAM,EAAE,2DAAiD,UAAU,OAAI,EAAvE,CAAuE,CAAA;AAAlI,QAAA,oBAAoB,wBAA8G"}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export default function useDynamicTranslation({ targetLocale, projectId, devApiKey, runtimeUrl, defaultLocale, setTranslations, ...metadata }: {
|
|
2
|
-
targetLocale: string;
|
|
3
|
-
projectId?: string;
|
|
4
|
-
defaultLocale?: string;
|
|
5
|
-
devApiKey?: string;
|
|
6
|
-
runtimeUrl?: string;
|
|
7
|
-
setTranslations: React.Dispatch<React.SetStateAction<any>>;
|
|
8
|
-
[key: string]: any;
|
|
9
|
-
}): {
|
|
10
|
-
translationEnabled: boolean;
|
|
11
|
-
translateContent: (params: {
|
|
12
|
-
source: any;
|
|
13
|
-
targetLocale: string;
|
|
14
|
-
metadata: {
|
|
15
|
-
hash: string;
|
|
16
|
-
context?: string;
|
|
17
|
-
} & Record<string, any>;
|
|
18
|
-
}) => void;
|
|
19
|
-
translateChildren: (params: {
|
|
20
|
-
source: any;
|
|
21
|
-
targetLocale: string;
|
|
22
|
-
metadata: {
|
|
23
|
-
hash: string;
|
|
24
|
-
context?: string;
|
|
25
|
-
} & Record<string, any>;
|
|
26
|
-
}) => void;
|
|
27
|
-
};
|
|
28
|
-
//# sourceMappingURL=useDynamicTranslation.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useDynamicTranslation.d.ts","sourceRoot":"","sources":["../../../src/provider/dynamic/useDynamicTranslation.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,EAC1C,YAAY,EACZ,SAAS,EAAE,SAAS,EACpB,UAAU,EAAE,aAAa,EACzB,eAAe,EACf,GAAG,QAAQ,EACd,EAAE;IACC,YAAY,EAAE,MAAM,CAAA;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAA;IAC1D,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CACrB,GAAG;IACA,kBAAkB,EAAE,OAAO,CAAA;IAC3B,gBAAgB,EAAE,CAAC,MAAM,EAAE;QAAE,MAAM,EAAE,GAAG,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,OAAO,CAAC,EAAE,MAAM,CAAA;SAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAAE,KAAK,IAAI,CAAC;IAC9I,iBAAiB,EAAE,CAAC,MAAM,EAAE;QAAE,MAAM,EAAE,GAAG,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,OAAO,CAAC,EAAE,MAAM,CAAA;SAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAAE,KAAK,IAAI,CAAC;CAClJ,CAyHA"}
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
50
|
-
var t = {};
|
|
51
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
52
|
-
t[p] = s[p];
|
|
53
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
54
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
55
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
56
|
-
t[p[i]] = s[p[i]];
|
|
57
|
-
}
|
|
58
|
-
return t;
|
|
59
|
-
};
|
|
60
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
61
|
-
exports.default = useDynamicTranslation;
|
|
62
|
-
var react_1 = require("react");
|
|
63
|
-
var createErrors_1 = require("../../errors/createErrors");
|
|
64
|
-
function useDynamicTranslation(_a) {
|
|
65
|
-
var _this = this;
|
|
66
|
-
var targetLocale = _a.targetLocale, projectId = _a.projectId, devApiKey = _a.devApiKey, runtimeUrl = _a.runtimeUrl, defaultLocale = _a.defaultLocale, setTranslations = _a.setTranslations, metadata = __rest(_a, ["targetLocale", "projectId", "devApiKey", "runtimeUrl", "defaultLocale", "setTranslations"]);
|
|
67
|
-
metadata = __assign(__assign({}, metadata), { projectId: projectId, sourceLocale: defaultLocale });
|
|
68
|
-
var translationEnabled = !!(runtimeUrl && projectId);
|
|
69
|
-
if (!translationEnabled)
|
|
70
|
-
return { translationEnabled: translationEnabled, translateContent: function () { }, translateChildren: function () { } };
|
|
71
|
-
// Queue to store requested keys between renders.
|
|
72
|
-
var requestQueueRef = (0, react_1.useRef)(new Map());
|
|
73
|
-
// Trigger a fetch when keys have been added.
|
|
74
|
-
var _b = (0, react_1.useState)(0), fetchTrigger = _b[0], setFetchTrigger = _b[1];
|
|
75
|
-
var translateContent = (0, react_1.useCallback)(function (params) {
|
|
76
|
-
var id = params.metadata.id ? "".concat(params.metadata.id, "-") : '';
|
|
77
|
-
var key = "".concat(id).concat(params.metadata.hash, "-").concat(params.targetLocale);
|
|
78
|
-
requestQueueRef.current.set(key, { type: 'content', source: params.source, metadata: params.metadata });
|
|
79
|
-
setFetchTrigger(function (n) { return n + 1; });
|
|
80
|
-
}, []);
|
|
81
|
-
/**
|
|
82
|
-
* Call this from <T> components to request a translation key.
|
|
83
|
-
* Keys are batched and fetched in the next effect cycle.
|
|
84
|
-
*/
|
|
85
|
-
var translateChildren = (0, react_1.useCallback)(function (params) {
|
|
86
|
-
var id = params.metadata.id ? "".concat(params.metadata.id, "-") : '';
|
|
87
|
-
var key = "".concat(id).concat(params.metadata.hash, "-").concat(params.targetLocale);
|
|
88
|
-
requestQueueRef.current.set(key, { type: 'jsx', source: params.source, metadata: params.metadata });
|
|
89
|
-
setFetchTrigger(function (n) { return n + 1; });
|
|
90
|
-
}, []);
|
|
91
|
-
(0, react_1.useEffect)(function () {
|
|
92
|
-
if (requestQueueRef.current.size === 0) {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
var isCancelled = false;
|
|
96
|
-
(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
97
|
-
var requests, response, _a, results, newTranslations_1, error_1;
|
|
98
|
-
return __generator(this, function (_b) {
|
|
99
|
-
switch (_b.label) {
|
|
100
|
-
case 0:
|
|
101
|
-
requests = Array.from(requestQueueRef.current.values());
|
|
102
|
-
_b.label = 1;
|
|
103
|
-
case 1:
|
|
104
|
-
_b.trys.push([1, 6, 7, 8]);
|
|
105
|
-
return [4 /*yield*/, fetch("".concat(runtimeUrl, "/v1/runtime/").concat(projectId, "/client"), {
|
|
106
|
-
method: 'POST',
|
|
107
|
-
headers: __assign({ 'Content-Type': 'application/json' }, (devApiKey && { 'x-gt-dev-api-key': devApiKey })),
|
|
108
|
-
body: JSON.stringify({
|
|
109
|
-
requests: requests,
|
|
110
|
-
targetLocale: targetLocale,
|
|
111
|
-
metadata: metadata
|
|
112
|
-
}),
|
|
113
|
-
})];
|
|
114
|
-
case 2:
|
|
115
|
-
response = _b.sent();
|
|
116
|
-
if (!!response.ok) return [3 /*break*/, 4];
|
|
117
|
-
_a = Error.bind;
|
|
118
|
-
return [4 /*yield*/, response.text()];
|
|
119
|
-
case 3: throw new (_a.apply(Error, [void 0, _b.sent()]))();
|
|
120
|
-
case 4: return [4 /*yield*/, response.json()];
|
|
121
|
-
case 5:
|
|
122
|
-
results = _b.sent();
|
|
123
|
-
if (!isCancelled) {
|
|
124
|
-
newTranslations_1 = {};
|
|
125
|
-
results.forEach(function (result, index) {
|
|
126
|
-
var _a;
|
|
127
|
-
var request = requests[index];
|
|
128
|
-
if ('translation' in result && result.translation && result.reference) { // translation success
|
|
129
|
-
var translation = result.translation, _b = result.reference, id = _b.id, key = _b.key;
|
|
130
|
-
// check for mismatching ids or hashes
|
|
131
|
-
if (id !== request.metadata.id || key !== request.metadata.hash) {
|
|
132
|
-
if (!request.metadata.id) {
|
|
133
|
-
console.warn("Mismatching hashes! Expected hash: ".concat(request.metadata.hash, ", but got hash: ").concat(key, ". We will still render your translation, but make sure to update to the newest version: www.generaltranslation.com/docs"));
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
console.warn("Mismatching ids or hashes! Expected id: ".concat(request.metadata.id, ", hash: ").concat(request.metadata.hash, ", but got id: ").concat(id, ", hash: ").concat(key, ". We will still render your translation, but make sure to update to the newest version: www.generaltranslation.com/docs"));
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
newTranslations_1[id || request.metadata.hash] = (_a = {}, _a[request.metadata.hash] = translation, _a);
|
|
140
|
-
}
|
|
141
|
-
else if ('error' in result && result.error && result.code) { // translation error
|
|
142
|
-
newTranslations_1[request.metadata.id || request.metadata.hash] = {
|
|
143
|
-
error: result.error || "An error occurred.",
|
|
144
|
-
code: result.code || 500
|
|
145
|
-
};
|
|
146
|
-
// error message
|
|
147
|
-
if (!request.metadata.id) {
|
|
148
|
-
console.error("Translation failed for hash: ".concat(request.metadata.hash, " "), result);
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
console.error("Translation failed for id: ".concat(request.metadata.id, ", hash: ").concat(request.metadata.hash, " "), result);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
else { // unknown error
|
|
155
|
-
// id defaults to hash if none provided
|
|
156
|
-
newTranslations_1[request.metadata.id || request.metadata.hash] = {
|
|
157
|
-
error: "An error occurred.",
|
|
158
|
-
code: 500
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
// update our translations
|
|
163
|
-
setTranslations(function (prev) { return __assign(__assign({}, (prev || {})), newTranslations_1); });
|
|
164
|
-
}
|
|
165
|
-
return [3 /*break*/, 8];
|
|
166
|
-
case 6:
|
|
167
|
-
error_1 = _b.sent();
|
|
168
|
-
console.error(createErrors_1.dynamicTranslationError, error_1);
|
|
169
|
-
setTranslations(function (prev) {
|
|
170
|
-
var merged = __assign({}, (prev || {}));
|
|
171
|
-
requests.forEach(function (request) {
|
|
172
|
-
// id defaults to hash if none provided
|
|
173
|
-
merged[request.metadata.id || request.metadata.hash] = {
|
|
174
|
-
error: "An error occurred.",
|
|
175
|
-
code: 500
|
|
176
|
-
};
|
|
177
|
-
});
|
|
178
|
-
return merged;
|
|
179
|
-
});
|
|
180
|
-
return [3 /*break*/, 8];
|
|
181
|
-
case 7:
|
|
182
|
-
requestQueueRef.current.clear();
|
|
183
|
-
return [7 /*endfinally*/];
|
|
184
|
-
case 8: return [2 /*return*/];
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
}); })();
|
|
188
|
-
return function () {
|
|
189
|
-
isCancelled = true;
|
|
190
|
-
};
|
|
191
|
-
}, [fetchTrigger, setTranslations]);
|
|
192
|
-
return { translateContent: translateContent, translateChildren: translateChildren, translationEnabled: translationEnabled };
|
|
193
|
-
}
|
|
194
|
-
//# sourceMappingURL=useDynamicTranslation.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useDynamicTranslation.js","sourceRoot":"","sources":["../../../src/provider/dynamic/useDynamicTranslation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,wCA2IC;AA9ID,+BAAiE;AACjE,0DAAoE;AAEpE,SAAwB,qBAAqB,CAAC,EAc7C;IAdD,iBA2IC;IA1IG,IAAA,YAAY,kBAAA,EACZ,SAAS,eAAA,EAAE,SAAS,eAAA,EACpB,UAAU,gBAAA,EAAE,aAAa,mBAAA,EACzB,eAAe,qBAAA,EACZ,QAAQ,cAL+B,4FAM7C,CADc;IAeX,QAAQ,yBAAQ,QAAQ,KAAE,SAAS,WAAA,EAAE,YAAY,EAAE,aAAa,GAAE,CAAC;IAEnE,IAAM,kBAAkB,GAAG,CAAC,CAAC,CAAC,UAAU,IAAI,SAAS,CAAC,CAAC;IACvD,IAAI,CAAC,kBAAkB;QAAE,OAAO,EAAE,kBAAkB,oBAAA,EAAE,gBAAgB,EAAE,cAAO,CAAC,EAAE,iBAAiB,EAAE,cAAO,CAAC,EAAE,CAAC;IAEhH,iDAAiD;IAC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;aAEvC,CAAC,EAAE,CAAC;QACL,OAAO;YACH,WAAW,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC;IACN,CAAC,EAAE,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC,CAAC;IAEpC,OAAO,EAAE,gBAAgB,kBAAA,EAAE,iBAAiB,mBAAA,EAAE,kBAAkB,oBAAA,EAAE,CAAC;AACvE,CAAC"}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export default function useRuntimeTranslation({ targetLocale, projectId, devApiKey, runtimeUrl, defaultLocale, setTranslations, ...metadata }: {
|
|
2
|
-
targetLocale: string;
|
|
3
|
-
projectId?: string;
|
|
4
|
-
defaultLocale?: string;
|
|
5
|
-
devApiKey?: string;
|
|
6
|
-
runtimeUrl?: string;
|
|
7
|
-
setTranslations: React.Dispatch<React.SetStateAction<any>>;
|
|
8
|
-
[key: string]: any;
|
|
9
|
-
}): {
|
|
10
|
-
translationEnabled: boolean;
|
|
11
|
-
translateContent: (params: {
|
|
12
|
-
source: any;
|
|
13
|
-
targetLocale: string;
|
|
14
|
-
metadata: {
|
|
15
|
-
hash: string;
|
|
16
|
-
context?: string;
|
|
17
|
-
} & Record<string, any>;
|
|
18
|
-
}) => void;
|
|
19
|
-
translateChildren: (params: {
|
|
20
|
-
source: any;
|
|
21
|
-
targetLocale: string;
|
|
22
|
-
metadata: {
|
|
23
|
-
hash: string;
|
|
24
|
-
context?: string;
|
|
25
|
-
} & Record<string, any>;
|
|
26
|
-
}) => void;
|
|
27
|
-
};
|
|
28
|
-
//# sourceMappingURL=useRuntimeTranslation.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useRuntimeTranslation.d.ts","sourceRoot":"","sources":["../../../src/provider/dynamic/useRuntimeTranslation.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,EAC1C,YAAY,EACZ,SAAS,EAAE,SAAS,EACpB,UAAU,EAAE,aAAa,EACzB,eAAe,EACf,GAAG,QAAQ,EACd,EAAE;IACC,YAAY,EAAE,MAAM,CAAA;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAA;IAC1D,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CACrB,GAAG;IACA,kBAAkB,EAAE,OAAO,CAAA;IAC3B,gBAAgB,EAAE,CAAC,MAAM,EAAE;QAAE,MAAM,EAAE,GAAG,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,OAAO,CAAC,EAAE,MAAM,CAAA;SAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAAE,KAAK,IAAI,CAAC;IAC9I,iBAAiB,EAAE,CAAC,MAAM,EAAE;QAAE,MAAM,EAAE,GAAG,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,OAAO,CAAC,EAAE,MAAM,CAAA;SAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAAE,KAAK,IAAI,CAAC;CAClJ,CAyHA"}
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
50
|
-
var t = {};
|
|
51
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
52
|
-
t[p] = s[p];
|
|
53
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
54
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
55
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
56
|
-
t[p[i]] = s[p[i]];
|
|
57
|
-
}
|
|
58
|
-
return t;
|
|
59
|
-
};
|
|
60
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
61
|
-
exports.default = useRuntimeTranslation;
|
|
62
|
-
var react_1 = require("react");
|
|
63
|
-
var createErrors_1 = require("../../errors/createErrors");
|
|
64
|
-
function useRuntimeTranslation(_a) {
|
|
65
|
-
var _this = this;
|
|
66
|
-
var targetLocale = _a.targetLocale, projectId = _a.projectId, devApiKey = _a.devApiKey, runtimeUrl = _a.runtimeUrl, defaultLocale = _a.defaultLocale, setTranslations = _a.setTranslations, metadata = __rest(_a, ["targetLocale", "projectId", "devApiKey", "runtimeUrl", "defaultLocale", "setTranslations"]);
|
|
67
|
-
metadata = __assign(__assign({}, metadata), { projectId: projectId, sourceLocale: defaultLocale });
|
|
68
|
-
var translationEnabled = !!(runtimeUrl && projectId);
|
|
69
|
-
if (!translationEnabled)
|
|
70
|
-
return { translationEnabled: translationEnabled, translateContent: function () { }, translateChildren: function () { } };
|
|
71
|
-
// Queue to store requested keys between renders.
|
|
72
|
-
var requestQueueRef = (0, react_1.useRef)(new Map());
|
|
73
|
-
// Trigger a fetch when keys have been added.
|
|
74
|
-
var _b = (0, react_1.useState)(0), fetchTrigger = _b[0], setFetchTrigger = _b[1];
|
|
75
|
-
var translateContent = (0, react_1.useCallback)(function (params) {
|
|
76
|
-
var id = params.metadata.id ? "".concat(params.metadata.id, "-") : '';
|
|
77
|
-
var key = "".concat(id).concat(params.metadata.hash, "-").concat(params.targetLocale);
|
|
78
|
-
requestQueueRef.current.set(key, { type: 'content', source: params.source, metadata: params.metadata });
|
|
79
|
-
setFetchTrigger(function (n) { return n + 1; });
|
|
80
|
-
}, []);
|
|
81
|
-
/**
|
|
82
|
-
* Call this from <T> components to request a translation key.
|
|
83
|
-
* Keys are batched and fetched in the next effect cycle.
|
|
84
|
-
*/
|
|
85
|
-
var translateChildren = (0, react_1.useCallback)(function (params) {
|
|
86
|
-
var id = params.metadata.id ? "".concat(params.metadata.id, "-") : '';
|
|
87
|
-
var key = "".concat(id).concat(params.metadata.hash, "-").concat(params.targetLocale);
|
|
88
|
-
requestQueueRef.current.set(key, { type: 'jsx', source: params.source, metadata: params.metadata });
|
|
89
|
-
setFetchTrigger(function (n) { return n + 1; });
|
|
90
|
-
}, []);
|
|
91
|
-
(0, react_1.useEffect)(function () {
|
|
92
|
-
if (requestQueueRef.current.size === 0) {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
var isCancelled = false;
|
|
96
|
-
(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
97
|
-
var requests, response, _a, results, newTranslations_1, error_1;
|
|
98
|
-
return __generator(this, function (_b) {
|
|
99
|
-
switch (_b.label) {
|
|
100
|
-
case 0:
|
|
101
|
-
requests = Array.from(requestQueueRef.current.values());
|
|
102
|
-
_b.label = 1;
|
|
103
|
-
case 1:
|
|
104
|
-
_b.trys.push([1, 6, 7, 8]);
|
|
105
|
-
return [4 /*yield*/, fetch("".concat(runtimeUrl, "/v1/runtime/").concat(projectId, "/client"), {
|
|
106
|
-
method: 'POST',
|
|
107
|
-
headers: __assign({ 'Content-Type': 'application/json' }, (devApiKey && { 'x-gt-dev-api-key': devApiKey })),
|
|
108
|
-
body: JSON.stringify({
|
|
109
|
-
requests: requests,
|
|
110
|
-
targetLocale: targetLocale,
|
|
111
|
-
metadata: metadata
|
|
112
|
-
}),
|
|
113
|
-
})];
|
|
114
|
-
case 2:
|
|
115
|
-
response = _b.sent();
|
|
116
|
-
if (!!response.ok) return [3 /*break*/, 4];
|
|
117
|
-
_a = Error.bind;
|
|
118
|
-
return [4 /*yield*/, response.text()];
|
|
119
|
-
case 3: throw new (_a.apply(Error, [void 0, _b.sent()]))();
|
|
120
|
-
case 4: return [4 /*yield*/, response.json()];
|
|
121
|
-
case 5:
|
|
122
|
-
results = _b.sent();
|
|
123
|
-
if (!isCancelled) {
|
|
124
|
-
newTranslations_1 = {};
|
|
125
|
-
results.forEach(function (result, index) {
|
|
126
|
-
var _a;
|
|
127
|
-
var request = requests[index];
|
|
128
|
-
if ('translation' in result && result.translation && result.reference) { // translation success
|
|
129
|
-
var translation = result.translation, _b = result.reference, id = _b.id, key = _b.key;
|
|
130
|
-
// check for mismatching ids or hashes
|
|
131
|
-
if (id !== request.metadata.id || key !== request.metadata.hash) {
|
|
132
|
-
if (!request.metadata.id) {
|
|
133
|
-
console.warn("Mismatching hashes! Expected hash: ".concat(request.metadata.hash, ", but got hash: ").concat(key, ". We will still render your translation, but make sure to update to the newest version: www.generaltranslation.com/docs"));
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
console.warn("Mismatching ids or hashes! Expected id: ".concat(request.metadata.id, ", hash: ").concat(request.metadata.hash, ", but got id: ").concat(id, ", hash: ").concat(key, ". We will still render your translation, but make sure to update to the newest version: www.generaltranslation.com/docs"));
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
newTranslations_1[id || request.metadata.hash] = (_a = {}, _a[request.metadata.hash] = translation, _a);
|
|
140
|
-
}
|
|
141
|
-
else if ('error' in result && result.error && result.code) { // translation error
|
|
142
|
-
newTranslations_1[request.metadata.id || request.metadata.hash] = {
|
|
143
|
-
error: result.error || "An error occurred.",
|
|
144
|
-
code: result.code || 500
|
|
145
|
-
};
|
|
146
|
-
// error message
|
|
147
|
-
if (!request.metadata.id) {
|
|
148
|
-
console.error("Translation failed for hash: ".concat(request.metadata.hash, " "), result);
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
console.error("Translation failed for id: ".concat(request.metadata.id, ", hash: ").concat(request.metadata.hash, " "), result);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
else { // unknown error
|
|
155
|
-
// id defaults to hash if none provided
|
|
156
|
-
newTranslations_1[request.metadata.id || request.metadata.hash] = {
|
|
157
|
-
error: "An error occurred.",
|
|
158
|
-
code: 500
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
// update our translations
|
|
163
|
-
setTranslations(function (prev) { return __assign(__assign({}, (prev || {})), newTranslations_1); });
|
|
164
|
-
}
|
|
165
|
-
return [3 /*break*/, 8];
|
|
166
|
-
case 6:
|
|
167
|
-
error_1 = _b.sent();
|
|
168
|
-
console.error(createErrors_1.dynamicTranslationError, error_1);
|
|
169
|
-
setTranslations(function (prev) {
|
|
170
|
-
var merged = __assign({}, (prev || {}));
|
|
171
|
-
requests.forEach(function (request) {
|
|
172
|
-
// id defaults to hash if none provided
|
|
173
|
-
merged[request.metadata.id || request.metadata.hash] = {
|
|
174
|
-
error: "An error occurred.",
|
|
175
|
-
code: 500
|
|
176
|
-
};
|
|
177
|
-
});
|
|
178
|
-
return merged;
|
|
179
|
-
});
|
|
180
|
-
return [3 /*break*/, 8];
|
|
181
|
-
case 7:
|
|
182
|
-
requestQueueRef.current.clear();
|
|
183
|
-
return [7 /*endfinally*/];
|
|
184
|
-
case 8: return [2 /*return*/];
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
}); })();
|
|
188
|
-
return function () {
|
|
189
|
-
isCancelled = true;
|
|
190
|
-
};
|
|
191
|
-
}, [fetchTrigger, setTranslations]);
|
|
192
|
-
return { translateContent: translateContent, translateChildren: translateChildren, translationEnabled: translationEnabled };
|
|
193
|
-
}
|
|
194
|
-
//# sourceMappingURL=useRuntimeTranslation.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useRuntimeTranslation.js","sourceRoot":"","sources":["../../../src/provider/dynamic/useRuntimeTranslation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,wCA2IC;AA9ID,+BAAiE;AACjE,0DAAoE;AAEpE,SAAwB,qBAAqB,CAAC,EAc7C;IAdD,iBA2IC;IA1IG,IAAA,YAAY,kBAAA,EACZ,SAAS,eAAA,EAAE,SAAS,eAAA,EACpB,UAAU,gBAAA,EAAE,aAAa,mBAAA,EACzB,eAAe,qBAAA,EACZ,QAAQ,cAL+B,4FAM7C,CADc;IAeX,QAAQ,yBAAQ,QAAQ,KAAE,SAAS,WAAA,EAAE,YAAY,EAAE,aAAa,GAAE,CAAC;IAEnE,IAAM,kBAAkB,GAAG,CAAC,CAAC,CAAC,UAAU,IAAI,SAAS,CAAC,CAAC;IACvD,IAAI,CAAC,kBAAkB;QAAE,OAAO,EAAE,kBAAkB,oBAAA,EAAE,gBAAgB,EAAE,cAAO,CAAC,EAAE,iBAAiB,EAAE,cAAO,CAAC,EAAE,CAAC;IAEhH,iDAAiD;IACjD,IAAM,eAAe,G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AE,CAAC;QACL,OAAO;YACH,WAAW,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC;IACN,CAAC,EAAE,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC,CAAC;IAEpC,OAAO,EAAE,gBAAgB,kBAAA,EAAE,iBAAiB,mBAAA,EAAE,kBAAkB,oBAAA,EAAE,CAAC;AACvE,CAAC"}
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { FlattenedDictionary, TranslateContentCallback, TranslationsObject } from "../types/types";
|
|
2
|
-
export default function translateDictionaryStrings(locale: string, flattenedDictionary: FlattenedDictionary, translations: TranslationsObject, translateContent: TranslateContentCallback): Promise<void>;
|
|
3
|
-
//# sourceMappingURL=translateDictionaryStrings.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"translateDictionaryStrings.d.ts","sourceRoot":"","sources":["../../src/translate/translateDictionaryStrings.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAGnG,wBAA8B,0BAA0B,CACtD,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,mBAAmB,EACxC,YAAY,EAAE,kBAAkB,EAChC,gBAAgB,EAAE,wBAAwB,iBA4C3C"}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
import { splitStringToContent } from "generaltranslation";
|
|
38
|
-
import { hashJsxChildren } from "generaltranslation/id";
|
|
39
|
-
import { extractEntryMetadata } from "../internal";
|
|
40
|
-
import { createLibraryNoEntryWarning } from "../messages/createMessages";
|
|
41
|
-
export default function translateDictionaryStrings(locale, flattenedDictionary, translations, translateContent) {
|
|
42
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
43
|
-
var _this = this;
|
|
44
|
-
return __generator(this, function (_a) {
|
|
45
|
-
switch (_a.label) {
|
|
46
|
-
case 0: return [4 /*yield*/, Promise.all(
|
|
47
|
-
// iterate through dictionary
|
|
48
|
-
Object.entries(flattenedDictionary).map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
49
|
-
var _c, entry, metadata, source, context, hash, translationEntry, error_1;
|
|
50
|
-
var _d;
|
|
51
|
-
var id = _b[0], dictionaryEntry = _b[1];
|
|
52
|
-
return __generator(this, function (_e) {
|
|
53
|
-
switch (_e.label) {
|
|
54
|
-
case 0:
|
|
55
|
-
// check that the dictionary entry is valid
|
|
56
|
-
if (dictionaryEntry === undefined) {
|
|
57
|
-
console.warn(createLibraryNoEntryWarning(id));
|
|
58
|
-
return [2 /*return*/];
|
|
59
|
-
}
|
|
60
|
-
_c = extractEntryMetadata(dictionaryEntry), entry = _c.entry, metadata = _c.metadata;
|
|
61
|
-
// dictionary strings only
|
|
62
|
-
if (typeof entry !== 'string') {
|
|
63
|
-
return [2 /*return*/];
|
|
64
|
-
}
|
|
65
|
-
source = splitStringToContent(entry);
|
|
66
|
-
context = metadata === null || metadata === void 0 ? void 0 : metadata.context;
|
|
67
|
-
hash = hashJsxChildren({ source: source, context: context });
|
|
68
|
-
translationEntry = (_d = translations[id]) === null || _d === void 0 ? void 0 : _d[hash];
|
|
69
|
-
if (!!translationEntry) return [3 /*break*/, 4];
|
|
70
|
-
_e.label = 1;
|
|
71
|
-
case 1:
|
|
72
|
-
_e.trys.push([1, 3, , 4]);
|
|
73
|
-
console.log('Translating:', id, hash, context);
|
|
74
|
-
return [4 /*yield*/, translateContent({
|
|
75
|
-
source: source,
|
|
76
|
-
targetLocale: locale,
|
|
77
|
-
metadata: { id: id, hash: hash, context: context }
|
|
78
|
-
})];
|
|
79
|
-
case 2:
|
|
80
|
-
_e.sent();
|
|
81
|
-
return [3 /*break*/, 4];
|
|
82
|
-
case 3:
|
|
83
|
-
error_1 = _e.sent();
|
|
84
|
-
console.error(error_1);
|
|
85
|
-
return [3 /*break*/, 4];
|
|
86
|
-
case 4: return [2 /*return*/];
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
}); }))];
|
|
90
|
-
case 1:
|
|
91
|
-
_a.sent();
|
|
92
|
-
return [2 /*return*/];
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
//# sourceMappingURL=translateDictionaryStrings.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"translateDictionaryStrings.js","sourceRoot":"","sources":["../../src/translate/translateDictionaryStrings.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,OAAO,EAAmB,oBAAoB,EAA2C,MAAM,aAAa,CAAC;AAC7G,OAAO,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAC;AAIzE,MAAM,CAAC,OAAO,UAAgB,0BAA0B,CACtD,MAAc,EACd,mBAAwC,EACxC,YAAgC,EAChC,gBAA0C;;;;;wBAE1C,qBAAM,OAAO,CAAC,GAAG;oBACf,6BAA6B;oBAC7B,MAAM,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAAC,GAAG,CAAC,gEAAO,EAAgD;;;4BAA/C,EAAE,QA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