@entropic-bond/localize-react 1.6.3 → 1.6.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.
|
@@ -5,8 +5,38 @@ export interface LocaleEntries {
|
|
|
5
5
|
export interface LocalizedState {
|
|
6
6
|
locale: LocaleEntries;
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* Creates a safe localizer function. The function will return the translated
|
|
10
|
+
* value associated to the `keyPath`. If the locale is undefined or the `keyPath` is
|
|
11
|
+
* not found in the locale, the function will return the `keyPath`.
|
|
12
|
+
* @param locale the locale to use
|
|
13
|
+
* @param throwOnKeyNotFound if true, the function will throw an error if the keyPath is not found in the locale
|
|
14
|
+
* @returns a function that will return the translated value associated to the keyPath
|
|
15
|
+
* @throws Error if the keyPath is not found in the locale and throwOnKeyNotFound is true
|
|
16
|
+
* @see safeLocalize
|
|
17
|
+
* @sample
|
|
18
|
+
* ```ts
|
|
19
|
+
* const safeLocalizer = createSafeLocalizerFor( locale ) // returns the localizer function
|
|
20
|
+
* safeLocalizer( 'myComponent.myKey' ) // returns the translated value
|
|
21
|
+
* ```
|
|
22
|
+
* @sample const safeLocalizer = createSafeLocalizerFor( locale, true )
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
8
25
|
export declare function createSafeLocalizerFor(locale: LocaleEntries | undefined, throwOnKeyNotFound?: boolean): (keyPath: string) => string;
|
|
9
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Returns the translated value associated to the keyPath. If the locale is
|
|
28
|
+
* undefined or the keyPath is not found in the locale, the function will return
|
|
29
|
+
* the keyPath.
|
|
30
|
+
* @param locale the locale to use
|
|
31
|
+
* @param keyPath the key path to the value to return
|
|
32
|
+
* @param throwOnKeyNotFound if true, the function will throw an error if the keyPath is not found in the locale
|
|
33
|
+
* @returns the translated value associated to the keyPath
|
|
34
|
+
* @throws Error if the keyPath is not found in the locale and throwOnKeyNotFound is true
|
|
35
|
+
* @see createSafeLocalizerFor
|
|
36
|
+
* @sample safeLocalize( locale, 'myComponent.myKey' )
|
|
37
|
+
* @sample safeLocalize( locale, 'myRootKey', true )
|
|
38
|
+
*/
|
|
39
|
+
export declare function safeLocalize(locale: LocaleEntries | undefined, keyPath: string, throwOnKeyNotFound?: boolean): string;
|
|
10
40
|
export type StateWithLocale<S> = S & LocalizedState;
|
|
11
41
|
/**
|
|
12
42
|
* Derive React components from this class to provide locale capabilities.
|
|
@@ -3,6 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useLocale = exports.localize = exports.LocalizedComponent = exports.safeLocalize = exports.createSafeLocalizerFor = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
5
|
const locale_1 = require("./locale");
|
|
6
|
+
/**
|
|
7
|
+
* Creates a safe localizer function. The function will return the translated
|
|
8
|
+
* value associated to the `keyPath`. If the locale is undefined or the `keyPath` is
|
|
9
|
+
* not found in the locale, the function will return the `keyPath`.
|
|
10
|
+
* @param locale the locale to use
|
|
11
|
+
* @param throwOnKeyNotFound if true, the function will throw an error if the keyPath is not found in the locale
|
|
12
|
+
* @returns a function that will return the translated value associated to the keyPath
|
|
13
|
+
* @throws Error if the keyPath is not found in the locale and throwOnKeyNotFound is true
|
|
14
|
+
* @see safeLocalize
|
|
15
|
+
* @sample
|
|
16
|
+
* ```ts
|
|
17
|
+
* const safeLocalizer = createSafeLocalizerFor( locale ) // returns the localizer function
|
|
18
|
+
* safeLocalizer( 'myComponent.myKey' ) // returns the translated value
|
|
19
|
+
* ```
|
|
20
|
+
* @sample const safeLocalizer = createSafeLocalizerFor( locale, true )
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
6
23
|
function createSafeLocalizerFor(locale, throwOnKeyNotFound = false) {
|
|
7
24
|
return (keyPath, throwOnNotFound = throwOnKeyNotFound) => {
|
|
8
25
|
if (!locale)
|
|
@@ -14,8 +31,21 @@ function createSafeLocalizerFor(locale, throwOnKeyNotFound = false) {
|
|
|
14
31
|
};
|
|
15
32
|
}
|
|
16
33
|
exports.createSafeLocalizerFor = createSafeLocalizerFor;
|
|
17
|
-
|
|
18
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Returns the translated value associated to the keyPath. If the locale is
|
|
36
|
+
* undefined or the keyPath is not found in the locale, the function will return
|
|
37
|
+
* the keyPath.
|
|
38
|
+
* @param locale the locale to use
|
|
39
|
+
* @param keyPath the key path to the value to return
|
|
40
|
+
* @param throwOnKeyNotFound if true, the function will throw an error if the keyPath is not found in the locale
|
|
41
|
+
* @returns the translated value associated to the keyPath
|
|
42
|
+
* @throws Error if the keyPath is not found in the locale and throwOnKeyNotFound is true
|
|
43
|
+
* @see createSafeLocalizerFor
|
|
44
|
+
* @sample safeLocalize( locale, 'myComponent.myKey' )
|
|
45
|
+
* @sample safeLocalize( locale, 'myRootKey', true )
|
|
46
|
+
*/
|
|
47
|
+
function safeLocalize(locale, keyPath, throwOnKeyNotFound = false) {
|
|
48
|
+
return createSafeLocalizerFor(locale, throwOnKeyNotFound)(keyPath);
|
|
19
49
|
}
|
|
20
50
|
exports.safeLocalize = safeLocalize;
|
|
21
51
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localized-component.js","sourceRoot":"","sources":["../src/localized-component.tsx"],"names":[],"mappings":";;;AAAA,iCAAsD;AACtD,qCAAiC;AAUjC,SAAgB,sBAAsB,CAAE,MAAiC,EAAE,kBAAkB,GAAG,KAAK;IACpG,OAAO,CAAE,OAAe,EAAE,eAAe,GAAG,kBAAkB,EAAG,EAAE;QAClE,IAAK,CAAC,MAAM;YAAG,OAAO,OAAO,CAAA;QAC7B,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAE,GAAO,EAAE,IAAY,EAAG,EAAE,CAAC,GAAG,CAAE,IAAI,CAAE,EAAE,MAAM,CAAE,CAAA;QAC1F,IAAK,KAAK,KAAK,SAAS,IAAI,eAAe;YAAG,MAAM,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