storybook-i18n 1.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/.babelrc.js ADDED
@@ -0,0 +1,19 @@
1
+ module.exports = {
2
+ presets: [
3
+ "@babel/preset-env",
4
+ "@babel/preset-typescript",
5
+ "@babel/preset-react",
6
+ ],
7
+ env: {
8
+ esm: {
9
+ presets: [
10
+ [
11
+ "@babel/preset-env",
12
+ {
13
+ modules: false,
14
+ },
15
+ ],
16
+ ],
17
+ },
18
+ },
19
+ };
package/CHANGELOG.md ADDED
@@ -0,0 +1,256 @@
1
+ # v1.0.4 (Fri Sep 24 2021)
2
+
3
+ #### 🐛 Bug Fix
4
+
5
+ - Support for left/right strings [#6](https://github.com/stevensacks/storybook-i18n/pull/6) ([@stevensacks](https://github.com/stevensacks))
6
+ - Remove obsolete node dependencies [#4](https://github.com/stevensacks/storybook-i18n/pull/4) ([@mheob](https://github.com/mheob))
7
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
8
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
9
+
10
+ #### ⚠️ Pushed to `main`
11
+
12
+ - feat(project): update libs, add setState smoke test ([@stevensacks](https://github.com/stevensacks))
13
+ - update libs ([@stevensacks](https://github.com/stevensacks))
14
+ - lock version of zx to allow node 14 ([@stevensacks](https://github.com/stevensacks))
15
+ - do not overwrite locale if already set ([@stevensacks](https://github.com/stevensacks))
16
+ - update read me and set to v1.0.0 ([@stevensacks](https://github.com/stevensacks))
17
+ - v0.9.8 ([@stevensacks](https://github.com/stevensacks))
18
+ - v0.9.6 ([@stevensacks](https://github.com/stevensacks))
19
+ - update version ([@stevensacks](https://github.com/stevensacks))
20
+ - rename to storybook-i18n ([@stevensacks](https://github.com/stevensacks))
21
+ - initial push ([@stevensacks](https://github.com/stevensacks))
22
+ - Initial commit ([@stevensacks](https://github.com/stevensacks))
23
+
24
+ #### Authors: 3
25
+
26
+ - Alexander Böhm ([@mheob](https://github.com/mheob))
27
+ - Michael Shilman ([@shilman](https://github.com/shilman))
28
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
29
+
30
+ ---
31
+
32
+ # v1.0.3 (Fri Aug 06 2021)
33
+
34
+ #### 🐛 Bug Fix
35
+
36
+ - Support for left/right strings [#6](https://github.com/stevensacks/storybook-i18n/pull/6) ([@stevensacks](https://github.com/stevensacks))
37
+ - Remove obsolete node dependencies [#4](https://github.com/stevensacks/storybook-i18n/pull/4) ([@mheob](https://github.com/mheob))
38
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
39
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
40
+
41
+ #### ⚠️ Pushed to `main`
42
+
43
+ - update libs ([@stevensacks](https://github.com/stevensacks))
44
+ - lock version of zx to allow node 14 ([@stevensacks](https://github.com/stevensacks))
45
+ - do not overwrite locale if already set ([@stevensacks](https://github.com/stevensacks))
46
+ - update read me and set to v1.0.0 ([@stevensacks](https://github.com/stevensacks))
47
+ - v0.9.8 ([@stevensacks](https://github.com/stevensacks))
48
+ - v0.9.6 ([@stevensacks](https://github.com/stevensacks))
49
+ - update version ([@stevensacks](https://github.com/stevensacks))
50
+ - rename to storybook-i18n ([@stevensacks](https://github.com/stevensacks))
51
+ - initial push ([@stevensacks](https://github.com/stevensacks))
52
+ - Initial commit ([@stevensacks](https://github.com/stevensacks))
53
+
54
+ #### Authors: 3
55
+
56
+ - Alexander Böhm ([@mheob](https://github.com/mheob))
57
+ - Michael Shilman ([@shilman](https://github.com/shilman))
58
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
59
+
60
+ ---
61
+
62
+ # v1.0.2 (Fri Aug 06 2021)
63
+
64
+ #### 🐛 Bug Fix
65
+
66
+ - Remove obsolete node dependencies [#4](https://github.com/stevensacks/storybook-i18n/pull/4) ([@mheob](https://github.com/mheob))
67
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
68
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
69
+
70
+ #### ⚠️ Pushed to `main`
71
+
72
+ - lock version of zx to allow node 14 ([@stevensacks](https://github.com/stevensacks))
73
+ - do not overwrite locale if already set ([@stevensacks](https://github.com/stevensacks))
74
+ - update read me and set to v1.0.0 ([@stevensacks](https://github.com/stevensacks))
75
+ - v0.9.8 ([@stevensacks](https://github.com/stevensacks))
76
+ - v0.9.6 ([@stevensacks](https://github.com/stevensacks))
77
+ - update version ([@stevensacks](https://github.com/stevensacks))
78
+ - rename to storybook-i18n ([@stevensacks](https://github.com/stevensacks))
79
+ - initial push ([@stevensacks](https://github.com/stevensacks))
80
+ - Initial commit ([@stevensacks](https://github.com/stevensacks))
81
+
82
+ #### Authors: 3
83
+
84
+ - Alexander Böhm ([@mheob](https://github.com/mheob))
85
+ - Michael Shilman ([@shilman](https://github.com/shilman))
86
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
87
+
88
+ ---
89
+
90
+ # v1.0.1 (Wed Jul 28 2021)
91
+
92
+ #### 🐛 Bug Fix
93
+
94
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
95
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
96
+
97
+ #### ⚠️ Pushed to `main`
98
+
99
+ - do not overwrite locale if already set ([@stevensacks](https://github.com/stevensacks))
100
+ - update read me and set to v1.0.0 ([@stevensacks](https://github.com/stevensacks))
101
+ - v0.9.8 ([@stevensacks](https://github.com/stevensacks))
102
+ - v0.9.6 ([@stevensacks](https://github.com/stevensacks))
103
+ - update version ([@stevensacks](https://github.com/stevensacks))
104
+ - rename to storybook-i18n ([@stevensacks](https://github.com/stevensacks))
105
+ - initial push ([@stevensacks](https://github.com/stevensacks))
106
+ - Initial commit ([@stevensacks](https://github.com/stevensacks))
107
+
108
+ #### Authors: 2
109
+
110
+ - Michael Shilman ([@shilman](https://github.com/shilman))
111
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
112
+
113
+ ---
114
+
115
+ # v0.9.7 (Mon Jul 12 2021)
116
+
117
+ #### 🐛 Bug Fix
118
+
119
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
120
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
121
+
122
+ #### ⚠️ Pushed to `main`
123
+
124
+ - v0.9.6 (stevensacks@gmail.com)
125
+ - update version (stevensacks@gmail.com)
126
+ - rename to storybook-i18n (stevensacks@gmail.com)
127
+ - initial push (stevensacks@gmail.com)
128
+ - Initial commit (stevensacks@gmail.com)
129
+
130
+ #### Authors: 2
131
+
132
+ - Michael Shilman ([@shilman](https://github.com/shilman))
133
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
134
+
135
+ ---
136
+
137
+ # v0.9.5 (Mon Jul 12 2021)
138
+
139
+ #### 🐛 Bug Fix
140
+
141
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
142
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
143
+
144
+ #### ⚠️ Pushed to `main`
145
+
146
+ - update version ([@stevensacks](https://github.com/stevensacks))
147
+ - rename to storybook-i18n ([@stevensacks](https://github.com/stevensacks))
148
+ - initial push ([@stevensacks](https://github.com/stevensacks))
149
+ - Initial commit ([@stevensacks](https://github.com/stevensacks))
150
+
151
+ #### Authors: 2
152
+
153
+ - Michael Shilman ([@shilman](https://github.com/shilman))
154
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
155
+
156
+ ---
157
+
158
+ # v0.9.4 (Mon Jul 12 2021)
159
+
160
+ #### 🐛 Bug Fix
161
+
162
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
163
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
164
+
165
+ #### ⚠️ Pushed to `main`
166
+
167
+ - update version ([@stevensacks](https://github.com/stevensacks))
168
+ - rename to storybook-i18n ([@stevensacks](https://github.com/stevensacks))
169
+ - initial push ([@stevensacks](https://github.com/stevensacks))
170
+ - Initial commit ([@stevensacks](https://github.com/stevensacks))
171
+
172
+ #### Authors: 2
173
+
174
+ - Michael Shilman ([@shilman](https://github.com/shilman))
175
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
176
+
177
+ ---
178
+
179
+ # v0.9.3 (Mon Jul 12 2021)
180
+
181
+ #### 🐛 Bug Fix
182
+
183
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
184
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
185
+
186
+ #### ⚠️ Pushed to `main`
187
+
188
+ - update version ([@stevensacks](https://github.com/stevensacks))
189
+ - rename to storybook-i18n ([@stevensacks](https://github.com/stevensacks))
190
+ - initial push ([@stevensacks](https://github.com/stevensacks))
191
+ - Initial commit ([@stevensacks](https://github.com/stevensacks))
192
+
193
+ #### Authors: 2
194
+
195
+ - Michael Shilman ([@shilman](https://github.com/shilman))
196
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
197
+
198
+ ---
199
+
200
+ # v0.9.3 (Mon Jul 12 2021)
201
+
202
+ #### 🐛 Bug Fix
203
+
204
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
205
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
206
+
207
+ #### ⚠️ Pushed to `main`
208
+
209
+ - rename to storybook-i18n ([@stevensacks](https://github.com/stevensacks))
210
+ - initial push ([@stevensacks](https://github.com/stevensacks))
211
+ - Initial commit ([@stevensacks](https://github.com/stevensacks))
212
+
213
+ #### Authors: 2
214
+
215
+ - Michael Shilman ([@shilman](https://github.com/shilman))
216
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
217
+
218
+ ---
219
+
220
+ # v0.9.2 (Mon Jul 12 2021)
221
+
222
+ #### 🐛 Bug Fix
223
+
224
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
225
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
226
+
227
+ #### ⚠️ Pushed to `main`
228
+
229
+ - rename to storybook-i18n ([@stevensacks](https://github.com/stevensacks))
230
+ - initial push ([@stevensacks](https://github.com/stevensacks))
231
+ - Initial commit ([@stevensacks](https://github.com/stevensacks))
232
+
233
+ #### Authors: 2
234
+
235
+ - Michael Shilman ([@shilman](https://github.com/shilman))
236
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
237
+
238
+ ---
239
+
240
+ # v0.9.1 (Mon Jul 12 2021)
241
+
242
+ #### 🐛 Bug Fix
243
+
244
+ - Make usage instructions more explicit [#2](https://github.com/stevensacks/storybook-i18n/pull/2) ([@shilman](https://github.com/shilman))
245
+ - Proposal to make this a library [#1](https://github.com/stevensacks/storybook-i18n/pull/1) ([@shilman](https://github.com/shilman))
246
+
247
+ #### ⚠️ Pushed to `main`
248
+
249
+ - rename to storybook-i18n ([@stevensacks](https://github.com/stevensacks))
250
+ - initial push ([@stevensacks](https://github.com/stevensacks))
251
+ - Initial commit ([@stevensacks](https://github.com/stevensacks))
252
+
253
+ #### Authors: 2
254
+
255
+ - Michael Shilman ([@shilman](https://github.com/shilman))
256
+ - Steven Sacks ([@stevensacks](https://github.com/stevensacks))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Storybook contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # Storybook-i18n
2
+
3
+ A library for best-practice i18n addons in Storybook:
4
+
5
+ - Easy-to-use locale configuration
6
+ - Simple drop-down menu
7
+ - URL-linkable state for sharing
8
+
9
+ ## Addon authors
10
+
11
+ As an addon author, you can use this library by adding it as a dependency and adding the following to your `/preset.js` file:
12
+
13
+ ```js
14
+ function config(entry = []) {
15
+ return [
16
+ ...entry,
17
+ require.resolve('storybook-i18n/preview'), // <-- library's preview preset
18
+ require.resolve('./dist/esm/preset/preview'), // <-- your addon's preview preset (if present)
19
+ ];
20
+ }
21
+
22
+ function managerEntries(entry = []) {
23
+ return [
24
+ ...entry,
25
+ require.resolve('storybook-i18n/manager'),
26
+ require.resolve('./dist/esm/preset/manager'), // <-- your addon's manager (if present)
27
+ ];
28
+ }
29
+
30
+ module.exports = {config, managerEntries};
31
+ ```
32
+
33
+ The currently selected locale is available in the `locale` global, so you can access it in a decorator using the following snippet:
34
+
35
+ ```js
36
+ import { MyProvider } from 'your-i18n-library';
37
+ import { useGlobals } from '@storybook/client-api';
38
+
39
+ const myDecorator = (story, context) => {
40
+ const [{locale}] = useGlobals();
41
+
42
+ return <MyProvider locale={locale}>;
43
+ }
44
+ ```
45
+
46
+ ## End users
47
+
48
+ End users configure the `locales` and `locale` parameters in `.storybook/preview.js`.
49
+
50
+ `locales` is an object where the keys are the "ids" of the locale/language and the values are the plain text name of that locale you want to use. This is what will appear in the dropdown in the toolbar.
51
+
52
+ ```javascript
53
+ export const parameters = {
54
+ locale: "en",
55
+ locales: {
56
+ en: "English",
57
+ fr: "Français",
58
+ ja: "日本語",
59
+ },
60
+ };
61
+ ```
62
+
63
+ Users can also use full locale strings.
64
+
65
+ ```javascript
66
+ export const parameters = {
67
+ locale: "en_US",
68
+ locales: {
69
+ en_US: "English (US)",
70
+ en_GB: "English (GB)",
71
+ fr_FR: "Français",
72
+ ja_JP: "日本語",
73
+ },
74
+ };
75
+ ```
76
+
77
+ The `locales` object can also have values as an object with keys of `title`, `left`, or `right`.
78
+
79
+ This is useful if you want to include an emoji flag or some other string to the left or right side.
80
+
81
+ For example:
82
+ ```javascript
83
+ export const parameters = {
84
+ locale: "en",
85
+ locales: {
86
+ en: {title: "English", left: '🇺🇸'},
87
+ fr: {title: "Français", left: '🇫🇷'},
88
+ ja: {title: "日本語", left: '🇯🇵'},
89
+ },
90
+ };
91
+ ```
92
+
93
+ Or something like this:
94
+ ```javascript
95
+ export const parameters = {
96
+ locale: "en_US",
97
+ locales: {
98
+ en_US: {title: "English", right: 'US'},
99
+ en_GB: {title: "English", right: 'GB'},
100
+ fr_FR: {title: "Français", right: 'FR'},
101
+ ja_JP: {title: "日本語", right: 'JP'},
102
+ },
103
+ };
104
+ ```
105
+
106
+ Addons should instruct them to use whichever format your i18n implementation expects.
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+
8
+ var _react = _interopRequireDefault(require("react"));
9
+
10
+ var _components = require("@storybook/components");
11
+
12
+ var _api = require("@storybook/api");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
+
16
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
17
+
18
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
19
+
20
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
21
+
22
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
23
+
24
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
25
+
26
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
27
+
28
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
29
+
30
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
31
+
32
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
33
+
34
+ var getValue = function getValue(value) {
35
+ if (typeof value === 'string') {
36
+ return {
37
+ title: value
38
+ };
39
+ }
40
+
41
+ return {
42
+ title: value.title || '',
43
+ left: value.left,
44
+ right: value.right
45
+ };
46
+ };
47
+
48
+ var getLocales = function getLocales(locales, locale, onSelect) {
49
+ return locales ? Object.entries(locales).map(function (_ref) {
50
+ var _ref2 = _slicedToArray(_ref, 2),
51
+ key = _ref2[0],
52
+ value = _ref2[1];
53
+
54
+ return _objectSpread(_objectSpread({}, getValue(value)), {}, {
55
+ id: key,
56
+ active: key === locale,
57
+ onClick: function onClick() {
58
+ return onSelect(key);
59
+ }
60
+ });
61
+ }) : [{
62
+ id: 'none',
63
+ title: 'No locales in parameters',
64
+ active: true,
65
+ onClick: function onClick() {}
66
+ }];
67
+ };
68
+
69
+ var Tool = function Tool() {
70
+ var _useGlobals = (0, _api.useGlobals)(),
71
+ _useGlobals2 = _slicedToArray(_useGlobals, 2),
72
+ globals = _useGlobals2[0],
73
+ updateGlobals = _useGlobals2[1];
74
+
75
+ var locale = globals.locale,
76
+ locales = globals.locales;
77
+ return /*#__PURE__*/_react["default"].createElement(_components.WithTooltip, {
78
+ closeOnClick: true,
79
+ placement: "top",
80
+ tooltip: function tooltip(_ref3) {
81
+ var onHide = _ref3.onHide;
82
+ return /*#__PURE__*/_react["default"].createElement(_components.TooltipLinkList, {
83
+ links: getLocales(locales, locale, function (selected) {
84
+ if (selected !== locale) {
85
+ updateGlobals({
86
+ locale: selected
87
+ });
88
+ }
89
+
90
+ onHide();
91
+ })
92
+ });
93
+ },
94
+ trigger: "click"
95
+ }, /*#__PURE__*/_react["default"].createElement(_components.IconButton, {
96
+ key: "i18n-locale",
97
+ title: "Locale Selector"
98
+ }, /*#__PURE__*/_react["default"].createElement(_components.Icons, {
99
+ icon: "globe"
100
+ })));
101
+ };
102
+
103
+ var _default = Tool;
104
+ exports["default"] = _default;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ADDON_ID = void 0;
7
+ var ADDON_ID = 'storybook/locale-addon';
8
+ exports.ADDON_ID = ADDON_ID;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+
8
+ if (module && module.hot && module.hot.decline) {
9
+ module.hot.decline();
10
+ } // make it work with --isolatedModules
11
+
12
+
13
+ var _default = {};
14
+ exports["default"] = _default;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
+
5
+ var _react = _interopRequireDefault(require("react"));
6
+
7
+ var _addons = _interopRequireWildcard(require("@storybook/addons"));
8
+
9
+ var _constants = require("../constants");
10
+
11
+ var _Tool = _interopRequireDefault(require("../Tool"));
12
+
13
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14
+
15
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
16
+
17
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
18
+
19
+ _addons["default"].register(_constants.ADDON_ID, function () {
20
+ _addons["default"].add(_constants.ADDON_ID, {
21
+ title: 'storybook locale',
22
+ type: _addons.types.TOOL,
23
+ match: function match(_ref) {
24
+ var viewMode = _ref.viewMode;
25
+ return viewMode === 'story';
26
+ },
27
+ render: function render() {
28
+ return /*#__PURE__*/_react["default"].createElement(_Tool["default"], null);
29
+ }
30
+ });
31
+ });
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+
3
+ var _react = require("react");
4
+
5
+ var _clientApi = require("@storybook/client-api");
6
+
7
+ var _react2 = require("@storybook/react");
8
+
9
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
10
+
11
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
12
+
13
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
14
+
15
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
16
+
17
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
18
+
19
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
20
+
21
+ var withLocale = function withLocale(story, context) {
22
+ var _useGlobals = (0, _clientApi.useGlobals)(),
23
+ _useGlobals2 = _slicedToArray(_useGlobals, 2),
24
+ globals = _useGlobals2[0],
25
+ updateGlobals = _useGlobals2[1];
26
+
27
+ (0, _react.useEffect)(function () {
28
+ var _context$parameters = context.parameters,
29
+ locale = _context$parameters.locale,
30
+ locales = _context$parameters.locales;
31
+
32
+ if (locales && !globals.locales) {
33
+ if (locale && !globals.locale) {
34
+ updateGlobals({
35
+ locale: locale,
36
+ locales: locales
37
+ });
38
+ } else {
39
+ updateGlobals({
40
+ locales: locales
41
+ });
42
+ }
43
+ }
44
+ }, []);
45
+ return story(context);
46
+ };
47
+
48
+ (0, _react2.addDecorator)(withLocale);
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,92 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
8
+
9
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
10
+
11
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
12
+
13
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
14
+
15
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
16
+
17
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
18
+
19
+ import React from 'react';
20
+ import { Icons, IconButton, WithTooltip, TooltipLinkList } from '@storybook/components';
21
+ import { useGlobals } from '@storybook/api';
22
+
23
+ var getValue = function getValue(value) {
24
+ if (typeof value === 'string') {
25
+ return {
26
+ title: value
27
+ };
28
+ }
29
+
30
+ return {
31
+ title: value.title || '',
32
+ left: value.left,
33
+ right: value.right
34
+ };
35
+ };
36
+
37
+ var getLocales = function getLocales(locales, locale, onSelect) {
38
+ return locales ? Object.entries(locales).map(function (_ref) {
39
+ var _ref2 = _slicedToArray(_ref, 2),
40
+ key = _ref2[0],
41
+ value = _ref2[1];
42
+
43
+ return _objectSpread(_objectSpread({}, getValue(value)), {}, {
44
+ id: key,
45
+ active: key === locale,
46
+ onClick: function onClick() {
47
+ return onSelect(key);
48
+ }
49
+ });
50
+ }) : [{
51
+ id: 'none',
52
+ title: 'No locales in parameters',
53
+ active: true,
54
+ onClick: function onClick() {}
55
+ }];
56
+ };
57
+
58
+ var Tool = function Tool() {
59
+ var _useGlobals = useGlobals(),
60
+ _useGlobals2 = _slicedToArray(_useGlobals, 2),
61
+ globals = _useGlobals2[0],
62
+ updateGlobals = _useGlobals2[1];
63
+
64
+ var locale = globals.locale,
65
+ locales = globals.locales;
66
+ return /*#__PURE__*/React.createElement(WithTooltip, {
67
+ closeOnClick: true,
68
+ placement: "top",
69
+ tooltip: function tooltip(_ref3) {
70
+ var onHide = _ref3.onHide;
71
+ return /*#__PURE__*/React.createElement(TooltipLinkList, {
72
+ links: getLocales(locales, locale, function (selected) {
73
+ if (selected !== locale) {
74
+ updateGlobals({
75
+ locale: selected
76
+ });
77
+ }
78
+
79
+ onHide();
80
+ })
81
+ });
82
+ },
83
+ trigger: "click"
84
+ }, /*#__PURE__*/React.createElement(IconButton, {
85
+ key: "i18n-locale",
86
+ title: "Locale Selector"
87
+ }, /*#__PURE__*/React.createElement(Icons, {
88
+ icon: "globe"
89
+ })));
90
+ };
91
+
92
+ export default Tool;
@@ -0,0 +1 @@
1
+ export var ADDON_ID = 'storybook/locale-addon';
@@ -0,0 +1,6 @@
1
+ if (module && module.hot && module.hot.decline) {
2
+ module.hot.decline();
3
+ } // make it work with --isolatedModules
4
+
5
+
6
+ export default {};
@@ -0,0 +1,17 @@
1
+ import React from 'react';
2
+ import addons, { types } from '@storybook/addons';
3
+ import { ADDON_ID } from "../constants";
4
+ import Tool from '../Tool';
5
+ addons.register(ADDON_ID, function () {
6
+ addons.add(ADDON_ID, {
7
+ title: 'storybook locale',
8
+ type: types.TOOL,
9
+ match: function match(_ref) {
10
+ var viewMode = _ref.viewMode;
11
+ return viewMode === 'story';
12
+ },
13
+ render: function render() {
14
+ return /*#__PURE__*/React.createElement(Tool, null);
15
+ }
16
+ });
17
+ });
@@ -0,0 +1,44 @@
1
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+
3
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+
5
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
+
7
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
8
+
9
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
10
+
11
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
12
+
13
+ import { useEffect } from 'react';
14
+ import { useGlobals } from '@storybook/client-api';
15
+ import { addDecorator } from '@storybook/react';
16
+
17
+ var withLocale = function withLocale(story, context) {
18
+ var _useGlobals = useGlobals(),
19
+ _useGlobals2 = _slicedToArray(_useGlobals, 2),
20
+ globals = _useGlobals2[0],
21
+ updateGlobals = _useGlobals2[1];
22
+
23
+ useEffect(function () {
24
+ var _context$parameters = context.parameters,
25
+ locale = _context$parameters.locale,
26
+ locales = _context$parameters.locales;
27
+
28
+ if (locales && !globals.locales) {
29
+ if (locale && !globals.locale) {
30
+ updateGlobals({
31
+ locale: locale,
32
+ locales: locales
33
+ });
34
+ } else {
35
+ updateGlobals({
36
+ locales: locales
37
+ });
38
+ }
39
+ }
40
+ }, []);
41
+ return story(context);
42
+ };
43
+
44
+ addDecorator(withLocale);
File without changes
@@ -0,0 +1,16 @@
1
+ /// <reference types="react" />
2
+ export interface Link {
3
+ id: string;
4
+ title: string;
5
+ active: boolean;
6
+ onClick: () => void;
7
+ left?: string;
8
+ right?: string;
9
+ }
10
+ export declare type LocaleValue = string | {
11
+ title: string;
12
+ left?: string;
13
+ right?: string;
14
+ };
15
+ declare const Tool: () => JSX.Element;
16
+ export default Tool;
@@ -0,0 +1 @@
1
+ export declare const ADDON_ID = "storybook/locale-addon";
@@ -0,0 +1,2 @@
1
+ declare const _default: {};
2
+ export default _default;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/manager.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require("./dist/cjs/preset/manager");
package/package.json ADDED
@@ -0,0 +1,101 @@
1
+ {
2
+ "name": "storybook-i18n",
3
+ "version": "1.0.4",
4
+ "description": "Tool to set the locale in Storybook for i18n",
5
+ "keywords": [
6
+ "storybook-addons",
7
+ "storybook",
8
+ "i18n",
9
+ "internationalization",
10
+ "localization",
11
+ "locale",
12
+ "language"
13
+ ],
14
+ "homepage": "https://github.com/stevensacks/storybook-i18n#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/stevensacks/storybook-i18n/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/stevensacks/storybook-i18n.git"
21
+ },
22
+ "license": "MIT",
23
+ "author": "Steven Sacks",
24
+ "main": "dist/cjs/index.js",
25
+ "module": "dist/esm/index.js",
26
+ "types": "dist/ts/index.d.ts",
27
+ "files": [
28
+ "dist/**/*",
29
+ "README.md",
30
+ "*.js",
31
+ "*.d.ts"
32
+ ],
33
+ "scripts": {
34
+ "clean": "rimraf ./dist",
35
+ "buildBabel": "concurrently \"yarn buildBabel:cjs\" \"yarn buildBabel:esm\"",
36
+ "buildBabel:cjs": "babel ./src -d ./dist/cjs --extensions \".js,.jsx,.ts,.tsx\"",
37
+ "buildBabel:esm": "babel ./src -d ./dist/esm --env-name esm --extensions \".js,.jsx,.ts,.tsx\"",
38
+ "buildTsc": "tsc --declaration --emitDeclarationOnly --outDir ./dist/ts",
39
+ "prebuild": "yarn clean",
40
+ "build": "concurrently \"yarn buildBabel\" \"yarn buildTsc\"",
41
+ "build:watch": "concurrently \"yarn buildBabel:esm -- --watch\" \"yarn buildTsc -- --watch\"",
42
+ "test": "echo \"Error: no test specified\" && exit 1",
43
+ "storybook": "start-storybook -p 6006",
44
+ "start": "concurrently \"yarn build:watch\" \"yarn storybook -- --no-manager-cache --quiet\"",
45
+ "build-storybook": "build-storybook",
46
+ "prerelease": "zx scripts/prepublish-checks.mjs",
47
+ "release": "yarn build && auto shipit",
48
+ "eject-ts": "zx scripts/eject-typescript.mjs"
49
+ },
50
+ "dependencies": {},
51
+ "devDependencies": {
52
+ "@babel/cli": "^7.15.7",
53
+ "@babel/core": "^7.15.5",
54
+ "@babel/preset-env": "^7.15.6",
55
+ "@babel/preset-react": "^7.12.5",
56
+ "@babel/preset-typescript": "^7.15.0",
57
+ "@storybook/addon-essentials": "^6.3.8",
58
+ "@storybook/react": "^6.3.8",
59
+ "auto": "^10.32.0",
60
+ "babel-loader": "^8.1.0",
61
+ "boxen": "^5.1.2",
62
+ "concurrently": "^6.2.1",
63
+ "dedent": "^0.7.0",
64
+ "prettier": "^2.4.1",
65
+ "prop-types": "^15.7.2",
66
+ "react": "^17.0.1",
67
+ "react-dom": "^17.0.1",
68
+ "rimraf": "^3.0.2",
69
+ "typescript": "^4.4.3",
70
+ "zx": "=1.14.1"
71
+ },
72
+ "peerDependencies": {
73
+ "@storybook/addons": "^6.3.8",
74
+ "@storybook/api": "^6.3.8",
75
+ "@storybook/components": "^6.3.8",
76
+ "@storybook/core-events": "^6.3.8",
77
+ "@storybook/theming": "^6.3.8",
78
+ "react": "^16.8.0 || ^17.0.0",
79
+ "react-dom": "^16.8.0 || ^17.0.0"
80
+ },
81
+ "peerDependenciesMeta": {
82
+ "react": {
83
+ "optional": true
84
+ },
85
+ "react-dom": {
86
+ "optional": true
87
+ }
88
+ },
89
+ "publishConfig": {
90
+ "access": "public"
91
+ },
92
+ "storybook": {
93
+ "displayName": "Storybook i18n",
94
+ "supportedFrameworks": [
95
+ "react",
96
+ "vue",
97
+ "angular"
98
+ ],
99
+ "icon": "https://user-images.githubusercontent.com/321738/63501763-88dbf600-c4cc-11e9-96cd-94adadc2fd72.png"
100
+ }
101
+ }
package/preset.js ADDED
@@ -0,0 +1,12 @@
1
+ function config(entry = []) {
2
+ return [...entry, require.resolve("./dist/esm/preset/preview")];
3
+ }
4
+
5
+ function managerEntries(entry = []) {
6
+ return [...entry, require.resolve("./dist/esm/preset/manager")];
7
+ }
8
+
9
+ module.exports = {
10
+ managerEntries,
11
+ config,
12
+ };
package/preview.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require("./dist/cjs/preset/preview");