PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / react-i18n / index.js

index.js in Gutenberg 12.1.0, at build/react-i18n/index.js

188 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /******/ (function() { // webpackBootstrap
2 /******/ "use strict";
3 /******/ // The require scope
4 /******/ var __webpack_require__ = {};
5 /******/
6 /************************************************************************/
7 /******/ /* webpack/runtime/define property getters */
8 /******/ !function() {
9 /******/ // define getter functions for harmony exports
10 /******/ __webpack_require__.d = function(exports, definition) {
11 /******/ for(var key in definition) {
12 /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
13 /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
14 /******/ }
15 /******/ }
16 /******/ };
17 /******/ }();
18 /******/
19 /******/ /* webpack/runtime/hasOwnProperty shorthand */
20 /******/ !function() {
21 /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
22 /******/ }();
23 /******/
24 /******/ /* webpack/runtime/make namespace object */
25 /******/ !function() {
26 /******/ // define __esModule on exports
27 /******/ __webpack_require__.r = function(exports) {
28 /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
29 /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
30 /******/ }
31 /******/ Object.defineProperty(exports, '__esModule', { value: true });
32 /******/ };
33 /******/ }();
34 /******/
35 /************************************************************************/
36 var __webpack_exports__ = {};
37 // ESM COMPAT FLAG
38 __webpack_require__.r(__webpack_exports__);
39
40 // EXPORTS
41 __webpack_require__.d(__webpack_exports__, {
42 "I18nProvider": function() { return /* binding */ I18nProvider; },
43 "useI18n": function() { return /* binding */ useI18n; },
44 "withI18n": function() { return /* binding */ withI18n; }
45 });
46
47 ;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/extends.js
48 function _extends() {
49 _extends = Object.assign || function (target) {
50 for (var i = 1; i < arguments.length; i++) {
51 var source = arguments[i];
52
53 for (var key in source) {
54 if (Object.prototype.hasOwnProperty.call(source, key)) {
55 target[key] = source[key];
56 }
57 }
58 }
59
60 return target;
61 };
62
63 return _extends.apply(this, arguments);
64 }
65 ;// CONCATENATED MODULE: external ["wp","element"]
66 var external_wp_element_namespaceObject = window["wp"]["element"];
67 ;// CONCATENATED MODULE: external ["wp","i18n"]
68 var external_wp_i18n_namespaceObject = window["wp"]["i18n"];
69 ;// CONCATENATED MODULE: ./packages/react-i18n/build-module/index.js
70
71
72
73 /**
74 * External dependencies
75 */
76 // Disable reason: Type-only import, this is fine. See https://github.com/typescript-eslint/typescript-eslint/issues/2661
77 // eslint-disable-next-line no-restricted-imports
78
79 /**
80 * WordPress dependencies
81 */
82
83
84
85 /**
86 * Utility to make a new context value
87 *
88 * @param i18n
89 */
90 function makeContextValue(i18n) {
91 return {
92 __: i18n.__.bind(i18n),
93 _x: i18n._x.bind(i18n),
94 _n: i18n._n.bind(i18n),
95 _nx: i18n._nx.bind(i18n),
96 isRTL: i18n.isRTL.bind(i18n),
97 hasTranslation: i18n.hasTranslation.bind(i18n)
98 };
99 }
100
101 const I18nContext = (0,external_wp_element_namespaceObject.createContext)(makeContextValue(external_wp_i18n_namespaceObject.defaultI18n));
102
103 /**
104 * The `I18nProvider` should be mounted above any localized components:
105 *
106 * @example
107 * ```js
108 * import { createI18n } from '@wordpress/react-i18n';
109 * import { I18nProvider } from '@wordpress/react-i18n';
110 * const i18n = createI18n();
111 *
112 * ReactDom.render(
113 * <I18nProvider i18n={ i18n }>
114 * <App />
115 * </I18nProvider>,
116 * el
117 * );
118 * ```
119 *
120 * You can also instantiate the provider without the `i18n` prop. In that case it will use the
121 * default `I18n` instance exported from `@wordpress/i18n`.
122 *
123 * @param props i18n provider props.
124 * @return Children wrapped in the I18nProvider.
125 */
126 function I18nProvider(props) {
127 const {
128 children,
129 i18n = external_wp_i18n_namespaceObject.defaultI18n
130 } = props;
131 const [update, forceUpdate] = (0,external_wp_element_namespaceObject.useReducer)(() => [], []); // rerender translations whenever the i18n instance fires a change event
132
133 (0,external_wp_element_namespaceObject.useEffect)(() => i18n.subscribe(forceUpdate), [i18n]);
134 const value = (0,external_wp_element_namespaceObject.useMemo)(() => makeContextValue(i18n), [i18n, update]);
135 return (0,external_wp_element_namespaceObject.createElement)(I18nContext.Provider, {
136 value: value
137 }, children);
138 }
139 /**
140 * React hook providing access to i18n functions. It exposes the `__`, `_x`, `_n`, `_nx`,
141 * `isRTL` and `hasTranslation` functions from [`@wordpress/i18n`](../i18n).
142 * Refer to their documentation there.
143 *
144 * @example
145 * ```js
146 * import { useI18n } from '@wordpress/react-i18n';
147 *
148 * function MyComponent() {
149 * const { __ } = useI18n();
150 * return __( 'Hello, world!' );
151 * }
152 * ```
153 */
154
155 const useI18n = () => (0,external_wp_element_namespaceObject.useContext)(I18nContext);
156
157 /**
158 * React higher-order component that passes the i18n translate functions (the same set
159 * as exposed by the `useI18n` hook) to the wrapped component as props.
160 *
161 * @example
162 * ```js
163 * import { withI18n } from '@wordpress/react-i18n';
164 *
165 * function MyComponent( { __ } ) {
166 * return __( 'Hello, world!' );
167 * }
168 *
169 * export default withI18n( MyComponent );
170 * ```
171 *
172 * @param InnerComponent React component to be wrapped and receive the i18n functions like `__`
173 * @return The wrapped component
174 */
175 function withI18n(InnerComponent) {
176 const EnhancedComponent = props => {
177 const i18nProps = useI18n();
178 return (0,external_wp_element_namespaceObject.createElement)(InnerComponent, _extends({}, props, i18nProps));
179 };
180
181 const innerComponentName = InnerComponent.displayName || InnerComponent.name || 'Component';
182 EnhancedComponent.displayName = `WithI18n(${innerComponentName})`;
183 return EnhancedComponent;
184 }
185 //# sourceMappingURL=index.js.map
186 (window.wp = window.wp || {}).reactI18n = __webpack_exports__;
187 /******/ })()
188 ;