@ohos-ports/enact-i18n 5.6.0-beta.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/$L/$L.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ // Type definitions for i18n/$L
2
+
3
+ type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
4
+ type Merge<M, N> = Omit<M, Extract<keyof M, keyof N>> & N;
5
+
6
+ /**
7
+ * Maps a string or key/value object to a translated string for the current locale.
8
+ */
9
+ export function toIString(str: string | object): IString;
10
+ /**
11
+ * Maps a string or key/value object to a translated string for the current locale.
12
+ */
13
+ export function $L(str: string | object): string;
14
+
15
+ export default $L;
package/$L/$L.js ADDED
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.$L = $L;
7
+ exports["default"] = void 0;
8
+ exports.toIString = toIString;
9
+ require("../src/glue");
10
+ var _resBundle = require("../src/resBundle");
11
+ /**
12
+ * Provides functions to map to translated strings.
13
+ *
14
+ * Usage:
15
+ * ```
16
+ * import $L, {toIString} from '@enact/i18n/$L';
17
+ * $L('Close'); // => "Close" in the current locale
18
+ * toIString('Close'); // => an ilib IString representing "Close" in the current locale
19
+ * ```
20
+ *
21
+ * @module i18n/$L
22
+ * @exports $L
23
+ * @exports $toIString
24
+ */
25
+
26
+ /**
27
+ * Maps a string or key/value object to a translated string for the current locale.
28
+ *
29
+ * @function
30
+ * @memberof i18n/$L
31
+ * @param {String|Object} str Source string
32
+ *
33
+ * @returns {ilib.IString} The translated string
34
+ */
35
+ function toIString(str) {
36
+ var rb = (0, _resBundle.getResBundle)();
37
+ return (0, _resBundle.getIStringFromBundle)(str, rb);
38
+ }
39
+
40
+ /**
41
+ * Maps a string or key/value object to a translated string for the current locale.
42
+ *
43
+ * @function
44
+ * @memberof i18n/$L
45
+ * @param {String|Object} str Source string
46
+ *
47
+ * @returns {String} The translated string
48
+ */
49
+ function $L(str) {
50
+ return String(toIString(str));
51
+ }
52
+ var _default = exports["default"] = $L;
@@ -0,0 +1,3 @@
1
+ {
2
+ "main": "$L.js"
3
+ }
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+
3
+ require("@testing-library/jest-dom");
4
+ var _react = require("@testing-library/react");
5
+ var _I18nDecorator = require("../../I18nDecorator");
6
+ var _resBundle = require("../../src/resBundle");
7
+ var _$L = require("../$L");
8
+ var _jsxRuntime = require("react/jsx-runtime");
9
+ describe('$L', function () {
10
+ test('should get the key string when there is no translated value', function () {
11
+ var expected = 'hello';
12
+ var Component = function Component(props) {
13
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
14
+ className: props.className,
15
+ children: (0, _$L.$L)(expected)
16
+ });
17
+ };
18
+ var Wrapped = (0, _I18nDecorator.I18nDecorator)({
19
+ sync: true
20
+ }, Component);
21
+ (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, {}));
22
+ var actual = _react.screen.getByText(expected);
23
+ expect(actual).toBeInTheDocument();
24
+ });
25
+ test('should clear ResBundle with calling `clearResBundle`', function () {
26
+ var expected = null;
27
+ var Component = function Component(props) {
28
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
29
+ className: props.className,
30
+ children: (0, _$L.$L)('hello')
31
+ });
32
+ };
33
+ var Wrapped = (0, _I18nDecorator.I18nDecorator)({
34
+ sync: true
35
+ }, Component);
36
+ (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, {}));
37
+ (0, _resBundle.clearResBundle)();
38
+ var actual = (0, _resBundle.getResBundle)();
39
+ expect(actual).toEqual(expected);
40
+ });
41
+ });