gt-react 5.0.3 → 5.0.4
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/dist/branches/Branch.js +41 -0
- package/dist/branches/Branch.js.map +1 -0
- package/dist/branches/plurals/Plural.js +48 -0
- package/dist/branches/plurals/Plural.js.map +1 -0
- package/dist/branches/plurals/getPluralBranch.js +75 -0
- package/dist/branches/plurals/getPluralBranch.js.map +1 -0
- package/dist/client.d.ts +6 -67
- package/dist/client.js +6 -0
- package/dist/client.js.map +1 -0
- package/dist/cookies/getLocaleCookie.js +25 -0
- package/dist/cookies/getLocaleCookie.js.map +1 -0
- package/dist/hooks/useBrowserLocale.js +41 -0
- package/dist/hooks/useBrowserLocale.js.map +1 -0
- package/dist/hooks/useDefaultLocale.js +16 -0
- package/dist/hooks/useDefaultLocale.js.map +1 -0
- package/dist/hooks/useGT.js +44 -0
- package/dist/hooks/useGT.js.map +1 -0
- package/dist/hooks/useLocale.js +14 -0
- package/dist/hooks/useLocale.js.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/inline/T.d.ts.map +1 -1
- package/dist/inline/T.js +110 -0
- package/dist/inline/T.js.map +1 -0
- package/dist/internal/addGTIdentifier.js +99 -0
- package/dist/internal/addGTIdentifier.js.map +1 -0
- package/dist/internal/flattenDictionary.js +35 -0
- package/dist/internal/flattenDictionary.js.map +1 -0
- package/dist/internal/hashReactChildrenObjects.js +54 -0
- package/dist/internal/hashReactChildrenObjects.js.map +1 -0
- package/dist/internal/writeChildrenAsObjects.js +89 -0
- package/dist/internal/writeChildrenAsObjects.js.map +1 -0
- package/dist/internal.js +12 -0
- package/dist/internal.js.map +1 -0
- package/dist/primitives/primitives.js +12 -0
- package/dist/primitives/primitives.js.map +1 -0
- package/dist/primitives/types.js +2 -0
- package/dist/primitives/types.js.map +1 -0
- package/dist/provider/GTContext.js +12 -0
- package/dist/provider/GTContext.js.map +1 -0
- package/dist/provider/GTProvider.js +160 -0
- package/dist/provider/GTProvider.js.map +1 -0
- package/dist/provider/helpers/extractEntryMetadata.js +12 -0
- package/dist/provider/helpers/extractEntryMetadata.js.map +1 -0
- package/dist/provider/helpers/getDictionaryEntry.js +18 -0
- package/dist/provider/helpers/getDictionaryEntry.js.map +1 -0
- package/dist/provider/helpers/getGTProp.js +7 -0
- package/dist/provider/helpers/getGTProp.js.map +1 -0
- package/dist/provider/helpers/isValidDictionaryEntry.js +2 -0
- package/dist/provider/helpers/isValidDictionaryEntry.js.map +1 -0
- package/dist/provider/helpers/isVariableObject.js +14 -0
- package/dist/provider/helpers/isVariableObject.js.map +1 -0
- package/dist/provider/rendering/renderDefaultChildren.js +70 -0
- package/dist/provider/rendering/renderDefaultChildren.js.map +1 -0
- package/dist/provider/rendering/renderTranslatedChildren.d.ts +0 -6
- package/dist/provider/rendering/renderTranslatedChildren.d.ts.map +1 -1
- package/dist/provider/rendering/renderTranslatedChildren.js +158 -0
- package/dist/provider/rendering/renderTranslatedChildren.js.map +1 -0
- package/dist/provider/rendering/renderVariable.d.ts +7 -0
- package/dist/provider/rendering/renderVariable.d.ts.map +1 -0
- package/dist/provider/rendering/renderVariable.js +19 -0
- package/dist/provider/rendering/renderVariable.js.map +1 -0
- package/dist/variables/Currency.js +52 -0
- package/dist/variables/Currency.js.map +1 -0
- package/dist/variables/DateTime.js +50 -0
- package/dist/variables/DateTime.js.map +1 -0
- package/dist/variables/Num.js +40 -0
- package/dist/variables/Num.js.map +1 -0
- package/dist/variables/Var.js +28 -0
- package/dist/variables/Var.js.map +1 -0
- package/dist/variables/_defaultVariableNames.js +8 -0
- package/dist/variables/_defaultVariableNames.js.map +1 -0
- package/dist/variables/_getVariableProps.js +21 -0
- package/dist/variables/_getVariableProps.js.map +1 -0
- package/package.json +5 -9
- package/tsconfig.json +0 -1
- package/dist/client.cjs.min.cjs +0 -2
- package/dist/client.cjs.min.cjs.map +0 -1
- package/dist/client.esm.min.mjs +0 -2
- package/dist/client.esm.min.mjs.map +0 -1
- package/dist/index.cjs.min.cjs +0 -2
- package/dist/index.cjs.min.cjs.map +0 -1
- package/dist/index.esm.min.mjs +0 -2
- package/dist/index.esm.min.mjs.map +0 -1
- package/dist/internal.cjs.min.cjs +0 -2
- package/dist/internal.cjs.min.cjs.map +0 -1
- package/dist/internal.esm.min.mjs +0 -2
- package/dist/internal.esm.min.mjs.map +0 -1
- package/rollup.config.mjs +0 -117
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
/**
|
|
14
|
+
* The `<Branch>` component dynamically renders a specified branch of content or a fallback child component.
|
|
15
|
+
* It allows for flexible content switching based on the `branch` prop and an object of possible branches (`...branches`).
|
|
16
|
+
* If the specified `branch` is present in the `branches` object, it renders the content of that branch.
|
|
17
|
+
* If the `branch` is not found, it renders the provided `children` as fallback content.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```jsx
|
|
21
|
+
* <Branch branch="summary" summary={<p>This is a summary</p>} details={<p>Details here</p>}>
|
|
22
|
+
* <p>Fallback content</p>
|
|
23
|
+
* </Branch>
|
|
24
|
+
* ```
|
|
25
|
+
* If the `branch` prop is set to `"summary"`, it will render `<p>This is a summary</p>`. If the `branch` is not set or does not match any keys in the branches object, it renders the fallback content `<p>Fallback content</p>`.
|
|
26
|
+
*
|
|
27
|
+
* @param {any} [children] - Fallback content to render if no matching branch is found.
|
|
28
|
+
* @param {string} [name="branch"] - Optional name for the component, used for metadata or tracking purposes.
|
|
29
|
+
* @param {string} [branch] - The name of the branch to render. The component looks for this key in the `...branches` object.
|
|
30
|
+
* @param {object} [branches] - An object containing possible branches as keys and their corresponding content as values.
|
|
31
|
+
* @returns {JSX.Element} The rendered branch or fallback content.
|
|
32
|
+
*/
|
|
33
|
+
function Branch(_a) {
|
|
34
|
+
var children = _a.children, _b = _a.name, name = _b === void 0 ? "branch" : _b, branch = _a.branch, props = __rest(_a, ["children", "name", "branch"]);
|
|
35
|
+
var generaltranslation = props["data-generaltranslation"], branches = __rest(props, ['data-generaltranslation']);
|
|
36
|
+
var renderedBranch = (branch && typeof branches[branch] !== 'undefined') ? branches[branch] : children;
|
|
37
|
+
return (_jsx("span", { "data-generaltranslation": generaltranslation, "data-_gt-name": name || "branch", "data-_gt-branch-name": branch, children: renderedBranch }));
|
|
38
|
+
}
|
|
39
|
+
Branch.gtTransformation = "branch";
|
|
40
|
+
export default Branch;
|
|
41
|
+
//# sourceMappingURL=Branch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Branch.js","sourceRoot":"","sources":["../../src/branches/Branch.tsx"],"names":[],"mappings":";;;;;;;;;;;;AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAS,MAAM,CAAC,EAOf;IANG,IAAA,QAAQ,cAAA,EAAE,YAAe,EAAf,IAAI,mBAAG,QAAQ,KAAA,EAAE,MAAM,YAAA,EAAK,KAAK,cAD/B,8BAEf,CAD8C;IAOnC,IAA2B,kBAAkB,GAAkB,KAAK,2BAAvB,EAAK,QAAQ,UAAK,KAAK,EAAtE,2BAA8D,CAAF,CAAW;IAC7E,IAAM,cAAc,GAAG,CAAC,MAAM,IAAI,OAAO,QAAQ,CAAC,MAAM,CAAC,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;IACzG,OAAO,CACH,0CAA+B,kBAAkB,mBAAiB,IAAI,IAAI,QAAQ,0BAAwB,MAAM,YAC3G,cAAc,GACZ,CACV,CAAC;AACN,CAAC;AAED,MAAM,CAAC,gBAAgB,GAAG,QAAQ,CAAC;AACnC,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
import { getPluralBranch } from "../../internal";
|
|
14
|
+
import useLocale from "../../hooks/useLocale";
|
|
15
|
+
import useDefaultLocale from "../../hooks/useDefaultLocale";
|
|
16
|
+
/**
|
|
17
|
+
* The `<Plural>` component dynamically renders content based on the plural form of the given number (`n`).
|
|
18
|
+
* It determines which content to display by matching the value of `n` to the appropriate pluralization branch,
|
|
19
|
+
* based on the current locale or a default locale. If no matching plural branch is found, the component renders
|
|
20
|
+
* the fallback `children` content.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```jsx
|
|
24
|
+
* <Plural n={1} one="There is 1 item">
|
|
25
|
+
* There are {n} items
|
|
26
|
+
* </Plural>
|
|
27
|
+
* ```
|
|
28
|
+
* In this example, if `n` is 1, it renders `"There is 1 item"`. If `n` is a different number, it renders
|
|
29
|
+
* `"There are {n} items"`.
|
|
30
|
+
*
|
|
31
|
+
* @param {any} [children] - Fallback content to render if no matching plural branch is found.
|
|
32
|
+
* @param {number} [n] - The number used to determine the plural form. This is required for pluralization to work.
|
|
33
|
+
* @returns {JSX.Element} The rendered content corresponding to the plural form of `n`, or the fallback content.
|
|
34
|
+
* @throws {Error} If `n` is not provided or not a valid number.
|
|
35
|
+
*/
|
|
36
|
+
function Plural(_a) {
|
|
37
|
+
var children = _a.children, n = _a.n, props = __rest(_a, ["children", "n"]);
|
|
38
|
+
var generaltranslation = props["data-generaltranslation"], branches = __rest(props, ['data-generaltranslation']);
|
|
39
|
+
var locale = useLocale();
|
|
40
|
+
var defaultLocale = useDefaultLocale();
|
|
41
|
+
if (typeof n !== 'number')
|
|
42
|
+
throw new Error("Plural with children \"".concat(children, "\" requires \"n\" option."));
|
|
43
|
+
var branch = getPluralBranch(n, [locale, defaultLocale], branches) || children;
|
|
44
|
+
return (_jsx("span", { "data-generaltranslation": generaltranslation, "data-_gt-n": n, children: branch }));
|
|
45
|
+
}
|
|
46
|
+
Plural.gtTransformation = "plural";
|
|
47
|
+
export default Plural;
|
|
48
|
+
//# sourceMappingURL=Plural.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Plural.js","sourceRoot":"","sources":["../../../src/branches/plurals/Plural.tsx"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,SAAS,MAAM,uBAAuB,CAAC;AAC9C,OAAO,gBAAgB,MAAM,8BAA8B,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAS,MAAM,CAAC,EAMf;IALG,IAAA,QAAQ,cAAA,EAAE,CAAC,OAAA,EAAK,KAAK,cADT,iBAEf,CADwB;IAMb,IAA2B,kBAAkB,GAAkB,KAAK,2BAAvB,EAAK,QAAQ,UAAK,KAAK,EAAtE,2BAA8D,CAAF,CAAW;IAC7E,IAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAC3B,IAAM,aAAa,GAAG,gBAAgB,EAAE,CAAC;IACzC,IAAI,OAAO,CAAC,KAAK,QAAQ;QACrB,MAAM,IAAI,KAAK,CAAC,iCAAyB,QAAQ,8BAAwB,CAAC,CAAC;IAC/E,IAAM,MAAM,GAAG,eAAe,CAAC,CAAC,EAAE,CAAC,MAAM,EAAE,aAAa,CAAC,EAAE,QAAQ,CAAC,IAAI,QAAQ,CAAC;IACjF,OAAO,CACH,0CAA+B,kBAAkB,gBAAc,CAAC,YAC3D,MAAM,GACJ,CACV,CAAC;AACN,CAAC;AAED,MAAM,CAAC,gBAAgB,GAAG,QAAQ,CAAC;AAEnC,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper function to get the branch name from a number based on provided options.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} n - The number to determine the branch name for.
|
|
5
|
+
* @param {Record<string, any>} options - The options containing possible branch names.
|
|
6
|
+
* @returns {string} The determined branch name.
|
|
7
|
+
*/
|
|
8
|
+
export function getBranchNameFromNumber(n, locales, options) {
|
|
9
|
+
var pluralRules = new Intl.PluralRules(locales);
|
|
10
|
+
var provisionalBranchName = pluralRules.select(n);
|
|
11
|
+
// aliases
|
|
12
|
+
var absN = Math.abs(n);
|
|
13
|
+
// 0
|
|
14
|
+
if (absN === 0 && options["zero"])
|
|
15
|
+
return "zero"; // override
|
|
16
|
+
// 1
|
|
17
|
+
if (absN === 1) {
|
|
18
|
+
if (options["singular"])
|
|
19
|
+
return "singular"; // override
|
|
20
|
+
if (options["one"])
|
|
21
|
+
return "one"; // override
|
|
22
|
+
}
|
|
23
|
+
if (provisionalBranchName === "one" && options["singular"])
|
|
24
|
+
return "singular";
|
|
25
|
+
// 2
|
|
26
|
+
if (absN === 2) {
|
|
27
|
+
if (options["dual"])
|
|
28
|
+
return "dual"; // override
|
|
29
|
+
if (options["two"])
|
|
30
|
+
return "two"; // override
|
|
31
|
+
}
|
|
32
|
+
if (provisionalBranchName === "two" && options["dual"])
|
|
33
|
+
return "dual";
|
|
34
|
+
// fallbacks
|
|
35
|
+
if (options[provisionalBranchName])
|
|
36
|
+
return provisionalBranchName;
|
|
37
|
+
// two
|
|
38
|
+
if (provisionalBranchName === "two" && options["dual"])
|
|
39
|
+
return "dual";
|
|
40
|
+
if (provisionalBranchName === "two" && options["plural"])
|
|
41
|
+
return "plural";
|
|
42
|
+
if (provisionalBranchName === "two" && options["other"])
|
|
43
|
+
return "other";
|
|
44
|
+
// few
|
|
45
|
+
if (provisionalBranchName === "few" && options["plural"])
|
|
46
|
+
return "plural";
|
|
47
|
+
if (provisionalBranchName === "few" && options["other"])
|
|
48
|
+
return "other";
|
|
49
|
+
// many
|
|
50
|
+
if (provisionalBranchName === "many" && options["plural"])
|
|
51
|
+
return "plural";
|
|
52
|
+
if (provisionalBranchName === "many" && options["other"])
|
|
53
|
+
return "other";
|
|
54
|
+
// other
|
|
55
|
+
if (provisionalBranchName === "other" && options["plural"])
|
|
56
|
+
return "plural";
|
|
57
|
+
return "";
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Main function to get the appropriate branch based on the provided number and branches.
|
|
61
|
+
*
|
|
62
|
+
* @param {number} n - The number to determine the branch for.
|
|
63
|
+
* @param {any} branches - The object containing possible branches.
|
|
64
|
+
* @returns {any} The determined branch.
|
|
65
|
+
*/
|
|
66
|
+
export default function getPluralBranch(n, locales, branches) {
|
|
67
|
+
var branchName = '';
|
|
68
|
+
var branch = null;
|
|
69
|
+
if (typeof n === 'number' && !branch && branches)
|
|
70
|
+
branchName = getBranchNameFromNumber(n, locales, branches);
|
|
71
|
+
if (branchName && !branch)
|
|
72
|
+
branch = branches[branchName];
|
|
73
|
+
return branch;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=getPluralBranch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPluralBranch.js","sourceR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
|
|
6
|
-
|
|
7
|
-
declare const GTContext: react.Context<GTContextType | undefined>;
|
|
8
|
-
|
|
9
|
-
declare function renderDefaultChildren({ children, variables, variablesOptions, defaultLocale }: {
|
|
10
|
-
children: ReactNode;
|
|
11
|
-
variables?: Record<string, any>;
|
|
12
|
-
variablesOptions?: Record<string, any>;
|
|
13
|
-
defaultLocale: string;
|
|
14
|
-
}): react__default.ReactNode;
|
|
15
|
-
|
|
16
|
-
type TranslatedChild = TranslatedElement | string | VariableObject;
|
|
17
|
-
type TranslatedChildren = TranslatedChild | TranslatedChild[];
|
|
18
|
-
type TranslatedElement = {
|
|
19
|
-
type: string;
|
|
20
|
-
props: {
|
|
21
|
-
'data-generaltranslation': {
|
|
22
|
-
id: number;
|
|
23
|
-
transformation: string;
|
|
24
|
-
variableType?: "variable" | "number" | "currency" | "datetime";
|
|
25
|
-
branches?: Record<string, any>;
|
|
26
|
-
defaultChildren?: any;
|
|
27
|
-
[key: string]: any;
|
|
28
|
-
};
|
|
29
|
-
children?: TranslatedChildren;
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
type VariableObject = {
|
|
33
|
-
key: string;
|
|
34
|
-
variable?: "variable" | "number" | "datetime" | "currency";
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
declare function renderTranslatedChildren({ source, target, variables, variablesOptions, locales }: {
|
|
38
|
-
source: ReactNode;
|
|
39
|
-
target: TranslatedChildren;
|
|
40
|
-
variables?: Record<string, any>;
|
|
41
|
-
variablesOptions?: Record<string, any>;
|
|
42
|
-
locales: string[];
|
|
43
|
-
}): ReactNode;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Hook to retrieve the browser's default language, with support for a fallback and locale stored in a cookie.
|
|
47
|
-
*
|
|
48
|
-
* @param {string} [defaultLocale=libraryDefaultLocale] - The default locale to use if the browser locale is not available.
|
|
49
|
-
* @param {string} [cookieName=localeCookieName] - The name of the cookie to check for a stored locale. If omitted, no cookie is used.
|
|
50
|
-
* @returns {string} The resolved browser locale, either from the cookie, browser settings, or the default locale.
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* const browserLocale = useBrowserLocale('en-US');
|
|
54
|
-
* console.log(browserLocale); // Outputs the browser's locale, or 'en-US' if unavailable
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* const browserLocale = useBrowserLocale('fr', 'localeCookie');
|
|
58
|
-
* console.log(browserLocale); // Outputs locale from cookie 'localeCookie' if available, or browser's locale otherwise
|
|
59
|
-
*
|
|
60
|
-
* @description
|
|
61
|
-
* This hook attempts to determine the browser's preferred language. If a locale is stored in a cookie (specified by `cookieName`),
|
|
62
|
-
* it will take precedence. If not, it falls back to the `navigator.language` or `navigator.userLanguage`. If none of these are available,
|
|
63
|
-
* the provided `defaultLocale` is used.
|
|
64
|
-
*/
|
|
65
|
-
declare function useBrowserLocale(defaultLocale?: string, cookieName?: string, locales?: string[]): string;
|
|
66
|
-
|
|
67
|
-
export { GTContext as _GTContext, renderDefaultChildren as _renderDefaultChildren, renderTranslatedChildren as _renderTranslatedChildren, useBrowserLocale };
|
|
1
|
+
import { GTContext as _GTContext } from "./provider/GTContext";
|
|
2
|
+
import _renderDefaultChildren from "./provider/rendering/renderDefaultChildren";
|
|
3
|
+
import _renderTranslatedChildren from "./provider/rendering/renderTranslatedChildren";
|
|
4
|
+
import useBrowserLocale from './hooks/useBrowserLocale';
|
|
5
|
+
export { _renderDefaultChildren, _renderTranslatedChildren, _GTContext, useBrowserLocale };
|
|
6
|
+
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { GTContext as _GTContext } from "./provider/GTContext";
|
|
2
|
+
import _renderDefaultChildren from "./provider/rendering/renderDefaultChildren";
|
|
3
|
+
import _renderTranslatedChildren from "./provider/rendering/renderTranslatedChildren";
|
|
4
|
+
import useBrowserLocale from './hooks/useBrowserLocale';
|
|
5
|
+
export { _renderDefaultChildren, _renderTranslatedChildren, _GTContext, useBrowserLocale };
|
|
6
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,IAAI,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,sBAAsB,MAAM,4CAA4C,CAAC;AAChF,OAAO,yBAAyB,MAAM,+CAA+C,CAAC;AACtF,OAAO,gBAAgB,MAAM,0BAA0B,CAAA;AACvD,OAAO,EACH,sBAAsB,EAAE,yBAAyB,EACjD,UAAU,EAAE,gBAAgB,EAC/B,CAAA"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { localeCookieName } from "../primitives/primitives";
|
|
2
|
+
/**
|
|
3
|
+
* Function to get the value of a specific cookie by its name.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} name - The name of the cookie to retrieve.
|
|
6
|
+
* @returns {string|null} - The value of the cookie, or null if not found.
|
|
7
|
+
*/
|
|
8
|
+
export default function getLocaleCookie(name) {
|
|
9
|
+
if (name === void 0) { name = localeCookieName; }
|
|
10
|
+
var cookieString = document.cookie;
|
|
11
|
+
// Split the cookies string by "; " to get an array of "key=value" strings
|
|
12
|
+
var cookiesArray = cookieString.split("; ");
|
|
13
|
+
// Loop through the array to find the cookie with the specified name
|
|
14
|
+
for (var _i = 0, cookiesArray_1 = cookiesArray; _i < cookiesArray_1.length; _i++) {
|
|
15
|
+
var cookie = cookiesArray_1[_i];
|
|
16
|
+
var _a = cookie.split("="), cookieName = _a[0], cookieValue = _a[1];
|
|
17
|
+
// Check if this cookie has the name we are looking for
|
|
18
|
+
if (cookieName === name) {
|
|
19
|
+
return decodeURIComponent(cookieValue);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
// If the cookie is not found, return null
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=getLocaleCookie.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getLocaleCookie.js","sourceRoot":"","sources":["../../src/cookies/getLocaleCookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,eAAe,CAAC,IAA+B;IAA/B,qBAAA,EAAA,uBAA+B;IACnE,IAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC;IAErC,0EAA0E;IAC1E,IAAM,YAAY,GAAG,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAE9C,oEAAoE;IACpE,KAAmB,UAAY,EAAZ,6BAAY,EAAZ,0BAAY,EAAZ,IAAY,EAAE,CAAC;QAA7B,IAAI,MAAM,qBAAA;QACP,IAAA,KAA4B,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAA5C,UAAU,QAAA,EAAE,WAAW,QAAqB,CAAC;QAEpD,uDAAuD;QACvD,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC;YACxB,OAAO,kBAAkB,CAAC,WAAW,CAAC,CAAC;QACzC,CAAC;IACH,CAAC;IAED,0CAA0C;IAC1C,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
import { libraryDefaultLocale, localeCookieName } from '../primitives/primitives';
|
|
3
|
+
import getLocaleCookie from '../cookies/getLocaleCookie';
|
|
4
|
+
import { determineLanguage } from 'generaltranslation';
|
|
5
|
+
/**
|
|
6
|
+
* Hook to retrieve the browser's default language, with support for a fallback and locale stored in a cookie.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} [defaultLocale=libraryDefaultLocale] - The default locale to use if the browser locale is not available.
|
|
9
|
+
* @param {string} [cookieName=localeCookieName] - The name of the cookie to check for a stored locale. If omitted, no cookie is used.
|
|
10
|
+
* @returns {string} The resolved browser locale, either from the cookie, browser settings, or the default locale.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const browserLocale = useBrowserLocale('en-US');
|
|
14
|
+
* console.log(browserLocale); // Outputs the browser's locale, or 'en-US' if unavailable
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const browserLocale = useBrowserLocale('fr', 'localeCookie');
|
|
18
|
+
* console.log(browserLocale); // Outputs locale from cookie 'localeCookie' if available, or browser's locale otherwise
|
|
19
|
+
*
|
|
20
|
+
* @description
|
|
21
|
+
* This hook attempts to determine the browser's preferred language. If a locale is stored in a cookie (specified by `cookieName`),
|
|
22
|
+
* it will take precedence. If not, it falls back to the `navigator.language` or `navigator.userLanguage`. If none of these are available,
|
|
23
|
+
* the provided `defaultLocale` is used.
|
|
24
|
+
*/
|
|
25
|
+
export default function useBrowserLocale(defaultLocale, cookieName, locales) {
|
|
26
|
+
if (defaultLocale === void 0) { defaultLocale = libraryDefaultLocale; }
|
|
27
|
+
if (cookieName === void 0) { cookieName = localeCookieName; }
|
|
28
|
+
var _a = useState(''), locale = _a[0], setLocale = _a[1];
|
|
29
|
+
useEffect(function () {
|
|
30
|
+
var _a;
|
|
31
|
+
var browserLocale = (cookieName ? getLocaleCookie(cookieName) : undefined)
|
|
32
|
+
|| (locales ? determineLanguage(navigator.languages, locales) : undefined)
|
|
33
|
+
|| ((_a = navigator.languages) === null || _a === void 0 ? void 0 : _a[0])
|
|
34
|
+
|| navigator.language
|
|
35
|
+
|| (navigator === null || navigator === void 0 ? void 0 : navigator.userLanguage)
|
|
36
|
+
|| defaultLocale;
|
|
37
|
+
setLocale(browserLocale);
|
|
38
|
+
}, [defaultLocale, locales]);
|
|
39
|
+
return locale;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=useBrowserLocale.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useBrowserLocale.js","sourceRoot":"","sources":["../../src/hooks/useBrowserLocale.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAClF,OAAO,eAAe,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CACpC,aAA4C,EAC5C,UAAqC,EACrC,OAAkB;IAFlB,8BAAA,EAAA,oCAA4C;IAC5C,2BAAA,EAAA,6BAAqC;IAG/B,IAAA,KAAsB,QAAQ,CAAS,EAAE,CAAC,EAAzC,MAAM,QAAA,EAAE,SAAS,QAAwB,CAAC;IACjD,SAAS,CAAC;;QACN,IAAM,aAAa,GACf,CAAC,UAAU,CAAC,CAAC,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;eACnD,CAAC,OAAO,CAAC,CAAC,CAAC,iBAAiB,CAAC,SAAS,CAAC,SAAqB,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;gBACnF,MAAA,SAAS,CAAC,SAAS,0CAAG,CAAC,CAAC,CAAA;eACxB,SAAS,CAAC,QAAQ;gBACjB,SAAiB,aAAjB,SAAS,uBAAT,SAAS,CAAU,YAAY,CAAA;eAChC,aAAa,CACnB;QACD,SAAS,CAAC,aAAa,CAAC,CAAC;IAC7B,CAAC,EAAE,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC,CAAC;IAC7B,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import useGTContext from "../provider/GTContext";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves the application's default locale from the `<GTProvider>` context.
|
|
4
|
+
*
|
|
5
|
+
* If no default locale is passed to the `<GTProvider>`, it defaults to providing 'en'.
|
|
6
|
+
*
|
|
7
|
+
* @returns {string} The application's default locale, e.g., 'en-US'.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const locale = useDefaultLocale();
|
|
11
|
+
* console.log(locale); // 'en-US'
|
|
12
|
+
*/
|
|
13
|
+
export default function useDefaultLocale() {
|
|
14
|
+
return useGTContext("useDefaultLocale(): Unable to access default locale outside of a <GTProvider>").defaultLocale;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=useDefaultLocale.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDefaultLocale.js","sourceRoot":"","sources":["../../src/hooks/useDefaultLocale.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,uBAAuB,CAAC;AAEjD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB;IACpC,OAAO,YAAY,CACf,+EAA+E,CAClF,CAAC,aAAa,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import useGTContext from "../provider/GTContext";
|
|
2
|
+
/**
|
|
3
|
+
* Gets the translation function `t` provided by `<GTProvider>`.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} [id] - Optional prefix to prepend to the translation keys.
|
|
6
|
+
* @returns {Function} A translation function that accepts a key string and returns the translated value.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const t = useGT('user');
|
|
10
|
+
* console.log(t('name')); // Translates item 'user.name'
|
|
11
|
+
*
|
|
12
|
+
* const t = useTranslation();
|
|
13
|
+
* console.log(t('hello')); // Translates item 'hello'
|
|
14
|
+
*/
|
|
15
|
+
export default function useGT(id) {
|
|
16
|
+
if (id === void 0) { id = ''; }
|
|
17
|
+
// Create a prefix for translation keys if an id is provided
|
|
18
|
+
var getID = function (suffix) {
|
|
19
|
+
return id ? "".concat(id, ".").concat(suffix) : suffix;
|
|
20
|
+
};
|
|
21
|
+
// Get the translation context
|
|
22
|
+
var translate = useGTContext("useGT('".concat(id, "'): No context provided. You're trying to get the t() function on the client, which can only be done inside a <GTProvider>.")).translate;
|
|
23
|
+
/**
|
|
24
|
+
* Translates a dictionary item based on its `id` and options.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} [id=''] - The ID of the item in the dictionary to translate.
|
|
27
|
+
* @param {Record<string, any>} [options={}] - Variables or parameters (e.g., `n`) passed into the translation for dynamic content.
|
|
28
|
+
* @param {Function} [f] - `f` is executed with `options` as parameters, and its result is translated based on the dictionary value of `id`. You likely don't need this parameter unless you are using `gt-next`.
|
|
29
|
+
*
|
|
30
|
+
* @returns {string | JSX.Element}
|
|
31
|
+
*/
|
|
32
|
+
function t(id, options, f) {
|
|
33
|
+
if (id === void 0) { id = ''; }
|
|
34
|
+
if (options === void 0) { options = {}; }
|
|
35
|
+
var prefixedID = getID(id);
|
|
36
|
+
var translation = translate ? translate(prefixedID, options, f) : undefined;
|
|
37
|
+
if (!translation)
|
|
38
|
+
console.warn("t('".concat(id, "') finding no translation for dictionary item ").concat(prefixedID, " !"));
|
|
39
|
+
return translation;
|
|
40
|
+
}
|
|
41
|
+
;
|
|
42
|
+
return t;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=useGT.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useGT.js","sourceRoot":"","sources":["../../src/hooks/useGT.ts"],"names":[],"mappings":"AACA,OAAO,YAAY,MAAM,uBAAuB,CAAC;AAEjD;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,OAAO,UAAU,KAAK,CAAC,EAAe;IAAf,mBAAA,EAAA,OAAe;IAOzC,4DAA4D;IAC5D,IAAM,KAAK,GAAG,UAAC,MAAc;QACzB,OAAO,EAAE,CAAC,CAAC,CAAC,UAAG,EAAE,cAAI,MAAM,CAAE,CAAC,CAAC,CAAC,MAAM,CAAC;IAC3C,CAAC,CAAA;IAED,8BAA8B;IACtB,IAAA,SAAS,GAAK,YAAY,CAC9B,iBAAU,EAAE,gIAA6H,CAC5I,UAFgB,CAEf;IAEF;;;;;;;;MAQE;IACF,SAAS,CAAC,CACN,EAAe,EACf,OAAiC,EACjC,CAAY;QAFZ,mBAAA,EAAA,OAAe;QACf,wBAAA,EAAA,YAAiC;QAGjC,IAAM,UAAU,GAAG,KAAK,CAAC,EAAE,CAAC,CAAC;QAC7B,IAAM,WAAW,GAAG,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAC9E,IAAI,CAAC,WAAW;YAAE,OAAO,CAAC,IAAI,CAAC,aAAM,EAAE,2DAAiD,UAAU,OAAI,CAAC,CAAA;QACvG,OAAO,WAAW,CAAC;IACvB,CAAC;IAAA,CAAC;IAEF,OAAO,CAAC,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import useGTContext from "../provider/GTContext";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves the user's locale from the `<GTProvider>` context.
|
|
4
|
+
*
|
|
5
|
+
* @returns {string} The user's locale, e.g., 'en-US'.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const locale = useLocale();
|
|
9
|
+
* console.log(locale); // 'en-US'
|
|
10
|
+
*/
|
|
11
|
+
export default function useLocale() {
|
|
12
|
+
return useGTContext("useLocale(): Unable to access user's locale outside of a <GTProvider>").locale;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=useLocale.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLocale.js","sourceRoot":"","sources":["../../src/hooks/useLocale.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,uBAAuB,CAAC;AAEjD;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,SAAS;IAC7B,OAAO,YAAY,CACf,uEAAuE,CAC1E,CAAC,MAAM,CAAC;AACb,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import T from "./inline/T";
|
|
2
|
+
import useGT from "./hooks/useGT";
|
|
3
|
+
import useDefaultLocale from "./hooks/useDefaultLocale";
|
|
4
|
+
import useLocale from "./hooks/useLocale";
|
|
5
|
+
import GTProvider from "./provider/GTProvider";
|
|
6
|
+
import Var from "./variables/Var";
|
|
7
|
+
import Num from "./variables/Num";
|
|
8
|
+
import Currency from "./variables/Currency";
|
|
9
|
+
import DateTime from "./variables/DateTime";
|
|
10
|
+
import Plural from "./branches/plurals/Plural";
|
|
11
|
+
import Branch from "./branches/Branch";
|
|
12
|
+
export { Var, Num, Currency, DateTime, T, GTProvider, Plural, Branch, useGT, useDefaultLocale, useLocale };
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,YAAY,CAAC;AAC3B,OAAO,KAAK,MAAM,eAAe,CAAC;AAClC,OAAO,gBAAgB,MAAM,0BAA0B,CAAC;AACxD,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAC1C,OAAO,UAAU,MAAM,uBAAuB,CAAC;AAC/C,OAAO,GAAG,MAAM,iBAAiB,CAAC;AAClC,OAAO,GAAG,MAAM,iBAAiB,CAAC;AAClC,OAAO,QAAQ,MAAM,sBAAsB,CAAC;AAC5C,OAAO,QAAQ,MAAM,sBAAsB,CAAC;AAC5C,OAAO,MAAM,MAAM,2BAA2B,CAAC;AAC/C,OAAO,MAAM,MAAM,mBAAmB,CAAC;AAEvC,OAAO,EACH,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,QAAQ,EAC5B,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAC7B,KAAK,EAAE,gBAAgB,EAAE,SAAS,EACrC,CAAA"}
|
package/dist/inline/T.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"T.d.ts","sourceRoot":"","sources":["../../src/inline/T.tsx"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC,EACtB,QAAQ,EAAE,EAAE,EAAE,GAAG,KAAK,EACzB,EAAE;IACC,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,EAAE,EAAE,MAAM,CAAA;IACV,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CACrB,GAAG,GAAG,CAAC,OAAO,
|
|
1
|
+
{"version":3,"file":"T.d.ts","sourceRoot":"","sources":["../../src/inline/T.tsx"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC,EACtB,QAAQ,EAAE,EAAE,EAAE,GAAG,KAAK,EACzB,EAAE;IACC,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,EAAE,EAAE,MAAM,CAAA;IACV,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CACrB,GAAG,GAAG,CAAC,OAAO,CAkDd"}
|
package/dist/inline/T.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
13
|
+
var t = {};
|
|
14
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
15
|
+
t[p] = s[p];
|
|
16
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
17
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
18
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
19
|
+
t[p[i]] = s[p[i]];
|
|
20
|
+
}
|
|
21
|
+
return t;
|
|
22
|
+
};
|
|
23
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
24
|
+
import React from "react";
|
|
25
|
+
import { isSameLanguage } from "generaltranslation";
|
|
26
|
+
import useDefaultLocale from "../hooks/useDefaultLocale";
|
|
27
|
+
import useLocale from "../hooks/useLocale";
|
|
28
|
+
import renderDefaultChildren from "../provider/rendering/renderDefaultChildren";
|
|
29
|
+
import { addGTIdentifier } from "../internal";
|
|
30
|
+
import useGTContext from "../provider/GTContext";
|
|
31
|
+
import renderTranslatedChildren from "../provider/rendering/renderTranslatedChildren";
|
|
32
|
+
import useGT from "../hooks/useGT";
|
|
33
|
+
import { useMemo } from "react";
|
|
34
|
+
/**
|
|
35
|
+
* Translation component that handles rendering translated content, including plural forms.
|
|
36
|
+
* Used with the required `id` parameter instead of `const t = useGT()`.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} [id] - Required identifier for the translation string.
|
|
39
|
+
* @param {React.ReactNode} children - The content to be translated or displayed.
|
|
40
|
+
* @param {any} [context] - Additional context used for translation.
|
|
41
|
+
* @param {Object} [props] - Additional props for the component.
|
|
42
|
+
* @returns {JSX.Element} The rendered translation or fallback content based on the provided configuration.
|
|
43
|
+
*
|
|
44
|
+
* @throws {Error} If a plural translation is requested but the `n` option is not provided.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```jsx
|
|
48
|
+
* // Basic usage:
|
|
49
|
+
* <T id="welcome_message" variables={{ name: "John" }}>
|
|
50
|
+
* Hello, <Var name="name"/>!
|
|
51
|
+
* </T>
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```jsx
|
|
56
|
+
* // Using plural translations:
|
|
57
|
+
* <T id="item_count" variables={{ n: 3 }} singular={"You have one item"}>
|
|
58
|
+
* You have <Num/> items
|
|
59
|
+
* </T>
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
export default function T(_a) {
|
|
64
|
+
var children = _a.children, id = _a.id, props = __rest(_a, ["children", "id"]);
|
|
65
|
+
if (!id) {
|
|
66
|
+
throw new Error("Client-side <T> with no provided 'id' prop. Children: ".concat(children));
|
|
67
|
+
}
|
|
68
|
+
var variables = props.variables, variablesOptions = props.variablesOptions;
|
|
69
|
+
var translations = useGTContext("<T id=\"".concat(id, "\"> used on the client-side outside of <GTProvider>")).translations;
|
|
70
|
+
var t = useGT();
|
|
71
|
+
if (!children) {
|
|
72
|
+
return _jsx(React.Fragment, { children: t(id, __assign({ variables: variables }, (variablesOptions && { variablesOptions: variablesOptions }))) }, id);
|
|
73
|
+
}
|
|
74
|
+
var locale = useLocale();
|
|
75
|
+
var defaultLocale = useDefaultLocale();
|
|
76
|
+
var taggedChildren = useMemo(function () { return addGTIdentifier(children); }, [children]);
|
|
77
|
+
var translationRequired = (function () {
|
|
78
|
+
if (!locale)
|
|
79
|
+
return false;
|
|
80
|
+
if (isSameLanguage(locale, defaultLocale))
|
|
81
|
+
return false;
|
|
82
|
+
return true;
|
|
83
|
+
})();
|
|
84
|
+
if (!translationRequired) {
|
|
85
|
+
return renderDefaultChildren({
|
|
86
|
+
children: taggedChildren,
|
|
87
|
+
variables: variables,
|
|
88
|
+
variablesOptions: variablesOptions,
|
|
89
|
+
defaultLocale: defaultLocale
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
// Do translation
|
|
93
|
+
var translation = translations[id];
|
|
94
|
+
if (!translation || !translation.t) {
|
|
95
|
+
console.error("<T id=\"".concat(id, "\"> is used in a client component without a corresponding translation."));
|
|
96
|
+
return renderDefaultChildren({
|
|
97
|
+
children: taggedChildren,
|
|
98
|
+
variables: variables,
|
|
99
|
+
variablesOptions: variablesOptions,
|
|
100
|
+
defaultLocale: defaultLocale
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return renderTranslatedChildren({
|
|
104
|
+
source: taggedChildren, target: translation.t,
|
|
105
|
+
variables: variables,
|
|
106
|
+
variablesOptions: variablesOptions,
|
|
107
|
+
locales: [locale, defaultLocale]
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=T.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"T.js","sourceRoot":"","sources":["../../src/inline/T.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,gBAAgB,MAAM,2BAA2B,CAAC;AACzD,OAAO,SAAS,MAAM,oBAAoB,CAAC;AAC3C,OAAO,qBAAqB,MAAM,6CAA6C,CAAC;AAChF,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,YAAY,MAAM,uBAAuB,CAAC;AACjD,OAAO,wBAAwB,MAAM,gDAAgD,CAAC;AACtF,OAAO,KAAK,MAAM,gBAAgB,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC,EAOzB;IANG,IAAA,QAAQ,cAAA,EAAE,EAAE,QAAA,EAAK,KAAK,cADA,kBAEzB,CADyB;IAQtB,IAAI,CAAC,EAAE,EAAE,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,gEAAyD,QAAQ,CAAE,CAAC,CAAA;IACxF,CAAC;IAEO,IAAA,SAAS,GAAuB,KAAK,UAA5B,EAAE,gBAAgB,GAAK,KAA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