@uxf/core-react 11.72.3 → 11.74.1
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 +73 -3
- package/package.json +1 -1
- package/translations/create-default-t.d.ts +2 -0
- package/translations/create-default-t.js +30 -0
- package/translations/create-dev-t.d.ts +2 -0
- package/translations/create-dev-t.js +23 -0
- package/translations/index.d.ts +2 -0
- package/translations/index.js +11 -0
- package/translations/replace-variables.d.ts +1 -0
- package/translations/replace-variables.js +6 -0
- package/translations/resolve-plural-key.d.ts +2 -0
- package/translations/resolve-plural-key.js +19 -0
- package/translations/types.d.ts +4 -0
- package/translations/types.js +2 -0
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
Conditionally hide some jsx content.
|
|
9
9
|
```tsx
|
|
10
|
-
|
|
10
|
+
import { Hide } from "@uxf/core-react/components/hide";
|
|
11
11
|
|
|
12
12
|
<Hide when={SOME_CONDITION}>
|
|
13
13
|
...some content
|
|
@@ -18,7 +18,7 @@ const { Hide } from "@uxf/core-react/components/hide";
|
|
|
18
18
|
|
|
19
19
|
Conditionally show some jsx content.
|
|
20
20
|
```tsx
|
|
21
|
-
|
|
21
|
+
import { Show } from "@uxf/core-react/components/show";
|
|
22
22
|
|
|
23
23
|
<Show when={SOME_CONDITION}>
|
|
24
24
|
...some content
|
|
@@ -31,7 +31,7 @@ Polyfill for custom css variable `--viewport-height` to mimic functionality of b
|
|
|
31
31
|
|
|
32
32
|
Add component to react app root (eg. `_app.tsx`).
|
|
33
33
|
```tsx
|
|
34
|
-
|
|
34
|
+
import { ViewportHeightPolyfill } from "@uxf/core-react/components/viewport-height-polyfill";
|
|
35
35
|
|
|
36
36
|
<ViewportHeightPolyfill />
|
|
37
37
|
```
|
|
@@ -394,3 +394,73 @@ import { twScreens } from "@generated/tw-tokens/tw-screens";
|
|
|
394
394
|
|
|
395
395
|
const isDesktop = useMediaQuery(`(min-width: ${twScreens.sm})`);
|
|
396
396
|
```
|
|
397
|
+
|
|
398
|
+
## Translations
|
|
399
|
+
|
|
400
|
+
Provides a translation system that can be used in both single-language and multi-language projects.
|
|
401
|
+
|
|
402
|
+
### Single-language projects
|
|
403
|
+
|
|
404
|
+
For single-language projects, use the `TranslationsProvider` context and `createDefaultT` function to provide translations for the packages you use:
|
|
405
|
+
|
|
406
|
+
```tsx
|
|
407
|
+
import { TranslationsProvider } from "@uxf/core-react/translations";
|
|
408
|
+
import { createDefaultT } from "@uxf/core-react/translations/create-default-t";
|
|
409
|
+
|
|
410
|
+
// Import translations for packages you use
|
|
411
|
+
import uiTranslations from "@uxf/ui/translations/cs.json";
|
|
412
|
+
import formTranslations from "@uxf/form/translations/cs.json";
|
|
413
|
+
|
|
414
|
+
// Create a translation function for your language
|
|
415
|
+
const t = createDefaultT({
|
|
416
|
+
...uiTranslations,
|
|
417
|
+
...formTranslations
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
// Provide the translation function to your app
|
|
421
|
+
function App() {
|
|
422
|
+
return (
|
|
423
|
+
<TranslationsProvider value={t}>
|
|
424
|
+
{/* Your app content */}
|
|
425
|
+
</TranslationsProvider>
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
### Multi-language projects
|
|
431
|
+
|
|
432
|
+
For multi-language projects, use the `TranslationsProvider` context with a translation function from your preferred translation library (e.g., next-translate):
|
|
433
|
+
|
|
434
|
+
```tsx
|
|
435
|
+
import { TranslationsProvider } from "@uxf/core-react/translations";
|
|
436
|
+
import useTranslation from "next-translate/useTranslation"; // Or any other translation library
|
|
437
|
+
|
|
438
|
+
function App() {
|
|
439
|
+
// Get the translation function from your translation library
|
|
440
|
+
const { t } = useTranslation();
|
|
441
|
+
|
|
442
|
+
return (
|
|
443
|
+
<TranslationsProvider value={t}>
|
|
444
|
+
{/* Your app content */}
|
|
445
|
+
</TranslationsProvider>
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
### Using translations in components
|
|
451
|
+
|
|
452
|
+
To use translations in your components, use the `useUxfTranslation` hook:
|
|
453
|
+
|
|
454
|
+
```tsx
|
|
455
|
+
import { useUxfTranslation } from "@uxf/core-react/translations";
|
|
456
|
+
|
|
457
|
+
function MyComponent() {
|
|
458
|
+
const t = useUxfTranslation();
|
|
459
|
+
|
|
460
|
+
return (
|
|
461
|
+
<div>
|
|
462
|
+
{t("form:validation.required")}
|
|
463
|
+
</div>
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
```
|
package/package.json
CHANGED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDefaultT = createDefaultT;
|
|
4
|
+
const replace_variables_1 = require("./replace-variables");
|
|
5
|
+
const resolve_plural_key_1 = require("./resolve-plural-key");
|
|
6
|
+
function createDefaultT(translationObject) {
|
|
7
|
+
return function (key, options) {
|
|
8
|
+
const pckg = key.split(":");
|
|
9
|
+
const path = pckg[1].split(".");
|
|
10
|
+
let translation = key;
|
|
11
|
+
try {
|
|
12
|
+
// eslint-disable-next-line
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
translation = path.reduce((acc, k) => acc[k], translationObject[pckg[0]]);
|
|
15
|
+
if (options) {
|
|
16
|
+
const pluralKey = (0, resolve_plural_key_1.resolvePluralKey)(options.count);
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
18
|
+
if (pluralKey && typeof translation === "object" && translation[pluralKey]) {
|
|
19
|
+
translation = translation[pluralKey];
|
|
20
|
+
}
|
|
21
|
+
translation = (0, replace_variables_1.replaceVariables)(translation, options);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// eslint-disable-next-line no-console
|
|
26
|
+
console.warn(`Can't find translation for key: "${key}"`);
|
|
27
|
+
}
|
|
28
|
+
return translation;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDevT = createDevT;
|
|
4
|
+
const replace_variables_1 = require("./replace-variables");
|
|
5
|
+
const resolve_plural_key_1 = require("./resolve-plural-key");
|
|
6
|
+
function createDevT(locale, translationObject) {
|
|
7
|
+
return function (key, options) {
|
|
8
|
+
const pckg = key.split(":");
|
|
9
|
+
const path = pckg[1].split(".");
|
|
10
|
+
// eslint-disable-next-line
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
let translation = path.reduce((acc, k) => acc[k], translationObject[pckg[0]])[locale];
|
|
13
|
+
if (options) {
|
|
14
|
+
const pluralKey = (0, resolve_plural_key_1.resolvePluralKey)(options.count);
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
16
|
+
if (pluralKey && typeof translation === "object" && translation[pluralKey]) {
|
|
17
|
+
translation = translation[pluralKey];
|
|
18
|
+
}
|
|
19
|
+
translation = (0, replace_variables_1.replaceVariables)(translation, options);
|
|
20
|
+
}
|
|
21
|
+
return translation;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.TranslationsProvider = void 0;
|
|
5
|
+
exports.useUxfTranslation = useUxfTranslation;
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const translationsContext = (0, react_1.createContext)((key) => key);
|
|
8
|
+
exports.TranslationsProvider = translationsContext.Provider;
|
|
9
|
+
function useUxfTranslation() {
|
|
10
|
+
return (0, react_1.useContext)(translationsContext);
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function replaceVariables(translation: string, options: Record<string, any>): string;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.replaceVariables = replaceVariables;
|
|
4
|
+
function replaceVariables(translation, options) {
|
|
5
|
+
return Object.keys(options).reduce((result, variable) => result.replace(new RegExp(`{{${variable}}}`, "g"), options[variable]), translation);
|
|
6
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolvePluralKey = resolvePluralKey;
|
|
4
|
+
const is_nil_1 = require("@uxf/core/utils/is-nil");
|
|
5
|
+
function resolvePluralKey(count) {
|
|
6
|
+
if ((0, is_nil_1.isNil)(count)) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
if (count === 0) {
|
|
10
|
+
return "zero";
|
|
11
|
+
}
|
|
12
|
+
if (count === 1) {
|
|
13
|
+
return "one";
|
|
14
|
+
}
|
|
15
|
+
if (count <= 4) {
|
|
16
|
+
return "few";
|
|
17
|
+
}
|
|
18
|
+
return "many";
|
|
19
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type SupportedLanguages = "cs" | "sk" | "en" | "de";
|
|
2
|
+
export type SupportedPluralKeywords = "0" | "zero" | "one" | "few" | "many" | "other";
|
|
3
|
+
export type TranslationFunction = (key: string, options?: Record<string, any>) => string;
|
|
4
|
+
export type TranslationsContextValue = TranslationFunction;
|