@sheet-i18n/react 0.1.0 → 0.1.1-canary.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/dist/index.d.mts +36 -2
- package/dist/index.d.ts +36 -2
- package/dist/index.js +309 -15
- package/dist/index.mjs +259 -5
- package/package.json +12 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { MessageDescriptor, IntlShape } from 'react-intl';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
type IntlProviderProps<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>> = {
|
|
5
|
+
i18nStore: ReturnType<typeof createI18nStore<TSupportedLocales, TLocaleSet>>;
|
|
6
|
+
currentLocale: TSupportedLocales[number];
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
};
|
|
9
|
+
declare function IntlProvider<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>>({ i18nStore, currentLocale, children, }: IntlProviderProps<TSupportedLocales, TLocaleSet>): react_jsx_runtime.JSX.Element;
|
|
10
|
+
|
|
11
|
+
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
12
|
+
type $TParams = Partial<UseIntlParams>;
|
|
13
|
+
interface UseTranslationParams<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TSheetTitle = keyof TLocaleSet[TSupportedLocales[number]]> {
|
|
14
|
+
sheetTitle: TSheetTitle;
|
|
15
|
+
i18nStore: ReturnType<typeof createI18nStore<TSupportedLocales, TLocaleSet>>;
|
|
16
|
+
}
|
|
17
|
+
declare function useTranslation<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TSheetTitle extends keyof TLocaleSet[TSupportedLocales[number]], TMessageId extends keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle]>({ sheetTitle, }: UseTranslationParams<TSupportedLocales, TLocaleSet, TSheetTitle>): {
|
|
18
|
+
t: <TValues extends $TParams["0"], TOpts extends $TParams["1"], T_Descriptor extends $TParams["2"]>(id: TMessageId, values?: TValues, opts?: TOpts, _descriptor?: T_Descriptor) => string | TValues[keyof TValues] | (string | TValues[keyof TValues])[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
interface I18nContextState<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>> {
|
|
22
|
+
supportedLocales: Readonly<TSupportedLocales>;
|
|
23
|
+
defaultLocale: TSupportedLocales[number];
|
|
24
|
+
localeSet: TLocaleSet;
|
|
25
|
+
}
|
|
26
|
+
declare function createI18nContext<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>>(i18nStore: ReturnType<typeof createI18nStore<TSupportedLocales, TLocaleSet>>): {
|
|
27
|
+
IntlProvider: ({ currentLocale, children, }: Omit<IntlProviderProps<TSupportedLocales, TLocaleSet>, "i18nStore">) => react_jsx_runtime.JSX.Element;
|
|
28
|
+
useTranslation: <SheetTitle extends keyof TLocaleSet[TSupportedLocales[number]]>(sheetTitle: SheetTitle) => ReturnType<typeof useTranslation<TSupportedLocales, TLocaleSet, SheetTitle, keyof TLocaleSet[TSupportedLocales[number]][SheetTitle]>>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
declare function createI18nStore<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>>(i18nStoreParams: I18nContextState<TSupportedLocales, TLocaleSet>): {
|
|
32
|
+
supportedLocales: Readonly<TSupportedLocales>;
|
|
33
|
+
defaultLocale: TSupportedLocales[number];
|
|
34
|
+
localeSet: TLocaleSet;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export { type I18nContextState, IntlProvider, type IntlProviderProps, createI18nContext, createI18nStore, useTranslation };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { MessageDescriptor, IntlShape } from 'react-intl';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
type IntlProviderProps<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>> = {
|
|
5
|
+
i18nStore: ReturnType<typeof createI18nStore<TSupportedLocales, TLocaleSet>>;
|
|
6
|
+
currentLocale: TSupportedLocales[number];
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
};
|
|
9
|
+
declare function IntlProvider<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>>({ i18nStore, currentLocale, children, }: IntlProviderProps<TSupportedLocales, TLocaleSet>): react_jsx_runtime.JSX.Element;
|
|
10
|
+
|
|
11
|
+
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
12
|
+
type $TParams = Partial<UseIntlParams>;
|
|
13
|
+
interface UseTranslationParams<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TSheetTitle = keyof TLocaleSet[TSupportedLocales[number]]> {
|
|
14
|
+
sheetTitle: TSheetTitle;
|
|
15
|
+
i18nStore: ReturnType<typeof createI18nStore<TSupportedLocales, TLocaleSet>>;
|
|
16
|
+
}
|
|
17
|
+
declare function useTranslation<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TSheetTitle extends keyof TLocaleSet[TSupportedLocales[number]], TMessageId extends keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle]>({ sheetTitle, }: UseTranslationParams<TSupportedLocales, TLocaleSet, TSheetTitle>): {
|
|
18
|
+
t: <TValues extends $TParams["0"], TOpts extends $TParams["1"], T_Descriptor extends $TParams["2"]>(id: TMessageId, values?: TValues, opts?: TOpts, _descriptor?: T_Descriptor) => string | TValues[keyof TValues] | (string | TValues[keyof TValues])[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
interface I18nContextState<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>> {
|
|
22
|
+
supportedLocales: Readonly<TSupportedLocales>;
|
|
23
|
+
defaultLocale: TSupportedLocales[number];
|
|
24
|
+
localeSet: TLocaleSet;
|
|
25
|
+
}
|
|
26
|
+
declare function createI18nContext<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>>(i18nStore: ReturnType<typeof createI18nStore<TSupportedLocales, TLocaleSet>>): {
|
|
27
|
+
IntlProvider: ({ currentLocale, children, }: Omit<IntlProviderProps<TSupportedLocales, TLocaleSet>, "i18nStore">) => react_jsx_runtime.JSX.Element;
|
|
28
|
+
useTranslation: <SheetTitle extends keyof TLocaleSet[TSupportedLocales[number]]>(sheetTitle: SheetTitle) => ReturnType<typeof useTranslation<TSupportedLocales, TLocaleSet, SheetTitle, keyof TLocaleSet[TSupportedLocales[number]][SheetTitle]>>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
declare function createI18nStore<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>>(i18nStoreParams: I18nContextState<TSupportedLocales, TLocaleSet>): {
|
|
32
|
+
supportedLocales: Readonly<TSupportedLocales>;
|
|
33
|
+
defaultLocale: TSupportedLocales[number];
|
|
34
|
+
localeSet: TLocaleSet;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export { type I18nContextState, IntlProvider, type IntlProviderProps, createI18nContext, createI18nStore, useTranslation };
|
package/dist/index.js
CHANGED
|
@@ -1,30 +1,324 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
function _assert_this_initialized(self) {
|
|
3
|
+
if (self === void 0) {
|
|
4
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
5
|
+
}
|
|
6
|
+
return self;
|
|
7
|
+
}
|
|
8
|
+
function _call_super(_this, derived, args) {
|
|
9
|
+
derived = _get_prototype_of(derived);
|
|
10
|
+
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
|
|
11
|
+
}
|
|
12
|
+
function _class_call_check(instance, Constructor) {
|
|
13
|
+
if (!(instance instanceof Constructor)) {
|
|
14
|
+
throw new TypeError("Cannot call a class as a function");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function _define_property(obj, key, value) {
|
|
18
|
+
if (key in obj) {
|
|
19
|
+
Object.defineProperty(obj, key, {
|
|
20
|
+
value: value,
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true
|
|
24
|
+
});
|
|
25
|
+
} else {
|
|
26
|
+
obj[key] = value;
|
|
27
|
+
}
|
|
28
|
+
return obj;
|
|
29
|
+
}
|
|
30
|
+
function _get_prototype_of(o) {
|
|
31
|
+
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
|
|
32
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
33
|
+
};
|
|
34
|
+
return _get_prototype_of(o);
|
|
35
|
+
}
|
|
36
|
+
function _inherits(subClass, superClass) {
|
|
37
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
38
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
39
|
+
}
|
|
40
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
41
|
+
constructor: {
|
|
42
|
+
value: subClass,
|
|
43
|
+
writable: true,
|
|
44
|
+
configurable: true
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
if (superClass) _set_prototype_of(subClass, superClass);
|
|
48
|
+
}
|
|
49
|
+
function _object_spread(target) {
|
|
50
|
+
for(var i = 1; i < arguments.length; i++){
|
|
51
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
52
|
+
var ownKeys = Object.keys(source);
|
|
53
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
54
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
55
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
ownKeys.forEach(function(key) {
|
|
59
|
+
_define_property(target, key, source[key]);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return target;
|
|
63
|
+
}
|
|
64
|
+
function ownKeys(object, enumerableOnly) {
|
|
65
|
+
var keys = Object.keys(object);
|
|
66
|
+
if (Object.getOwnPropertySymbols) {
|
|
67
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
68
|
+
if (enumerableOnly) {
|
|
69
|
+
symbols = symbols.filter(function(sym) {
|
|
70
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
keys.push.apply(keys, symbols);
|
|
74
|
+
}
|
|
75
|
+
return keys;
|
|
76
|
+
}
|
|
77
|
+
function _object_spread_props(target, source) {
|
|
78
|
+
source = source != null ? source : {};
|
|
79
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
80
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
81
|
+
} else {
|
|
82
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
83
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return target;
|
|
87
|
+
}
|
|
88
|
+
function _possible_constructor_return(self, call) {
|
|
89
|
+
if (call && (_type_of(call) === "object" || typeof call === "function")) {
|
|
90
|
+
return call;
|
|
91
|
+
}
|
|
92
|
+
return _assert_this_initialized(self);
|
|
93
|
+
}
|
|
94
|
+
function _set_prototype_of(o, p) {
|
|
95
|
+
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
|
96
|
+
o.__proto__ = p;
|
|
97
|
+
return o;
|
|
98
|
+
};
|
|
99
|
+
return _set_prototype_of(o, p);
|
|
100
|
+
}
|
|
101
|
+
function _type_of(obj) {
|
|
102
|
+
"@swc/helpers - typeof";
|
|
103
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
104
|
+
}
|
|
105
|
+
function _is_native_reflect_construct() {
|
|
106
|
+
try {
|
|
107
|
+
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
|
108
|
+
} catch (_) {}
|
|
109
|
+
return (_is_native_reflect_construct = function() {
|
|
110
|
+
return !!result;
|
|
111
|
+
})();
|
|
112
|
+
}
|
|
2
113
|
var __defProp = Object.defineProperty;
|
|
3
114
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
115
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
116
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all)
|
|
7
|
-
|
|
8
|
-
|
|
117
|
+
var __export = function(target, all) {
|
|
118
|
+
for(var name in all)__defProp(target, name, {
|
|
119
|
+
get: all[name],
|
|
120
|
+
enumerable: true
|
|
121
|
+
});
|
|
9
122
|
};
|
|
10
|
-
var __copyProps = (to, from, except, desc)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
123
|
+
var __copyProps = function(to, from, except, desc) {
|
|
124
|
+
if (from && (typeof from === "undefined" ? "undefined" : _type_of(from)) === "object" || typeof from === "function") {
|
|
125
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
126
|
+
try {
|
|
127
|
+
var _loop = function() {
|
|
128
|
+
var key = _step.value;
|
|
129
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
130
|
+
get: function() {
|
|
131
|
+
return from[key];
|
|
132
|
+
},
|
|
133
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
|
|
137
|
+
} catch (err) {
|
|
138
|
+
_didIteratorError = true;
|
|
139
|
+
_iteratorError = err;
|
|
140
|
+
} finally{
|
|
141
|
+
try {
|
|
142
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
143
|
+
_iterator.return();
|
|
144
|
+
}
|
|
145
|
+
} finally{
|
|
146
|
+
if (_didIteratorError) {
|
|
147
|
+
throw _iteratorError;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return to;
|
|
153
|
+
};
|
|
154
|
+
var __toCommonJS = function(mod) {
|
|
155
|
+
return __copyProps(__defProp({}, "__esModule", {
|
|
156
|
+
value: true
|
|
157
|
+
}), mod);
|
|
17
158
|
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
159
|
// src/index.ts
|
|
21
160
|
var src_exports = {};
|
|
22
161
|
__export(src_exports, {
|
|
23
|
-
|
|
162
|
+
IntlProvider: function() {
|
|
163
|
+
return IntlProvider;
|
|
164
|
+
},
|
|
165
|
+
createI18nContext: function() {
|
|
166
|
+
return createI18nContext;
|
|
167
|
+
},
|
|
168
|
+
createI18nStore: function() {
|
|
169
|
+
return createI18nStore;
|
|
170
|
+
},
|
|
171
|
+
useTranslation: function() {
|
|
172
|
+
return useTranslation;
|
|
173
|
+
}
|
|
24
174
|
});
|
|
25
175
|
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
|
|
176
|
+
// src/createI18nStore.ts
|
|
177
|
+
var import_shared_utils = require("@sheet-i18n/shared-utils");
|
|
178
|
+
// src/Errors.ts
|
|
179
|
+
var import_errors = require("@sheet-i18n/errors");
|
|
180
|
+
var InvalidI18nStoreParamsError = /*#__PURE__*/ function(_import_errors_CustomError) {
|
|
181
|
+
_inherits(InvalidI18nStoreParamsError, _import_errors_CustomError);
|
|
182
|
+
function InvalidI18nStoreParamsError() {
|
|
183
|
+
_class_call_check(this, InvalidI18nStoreParamsError);
|
|
184
|
+
return _call_super(this, InvalidI18nStoreParamsError, arguments);
|
|
185
|
+
}
|
|
186
|
+
return InvalidI18nStoreParamsError;
|
|
187
|
+
}(import_errors.CustomError);
|
|
188
|
+
var InvalidSupportedLocalesError = /*#__PURE__*/ function(_import_errors_CustomError) {
|
|
189
|
+
_inherits(InvalidSupportedLocalesError, _import_errors_CustomError);
|
|
190
|
+
function InvalidSupportedLocalesError() {
|
|
191
|
+
_class_call_check(this, InvalidSupportedLocalesError);
|
|
192
|
+
return _call_super(this, InvalidSupportedLocalesError, arguments);
|
|
193
|
+
}
|
|
194
|
+
return InvalidSupportedLocalesError;
|
|
195
|
+
}(import_errors.CustomError);
|
|
196
|
+
var NoDefaultLocaleInSupportedLocalesError = /*#__PURE__*/ function(_import_errors_CustomError) {
|
|
197
|
+
_inherits(NoDefaultLocaleInSupportedLocalesError, _import_errors_CustomError);
|
|
198
|
+
function NoDefaultLocaleInSupportedLocalesError() {
|
|
199
|
+
_class_call_check(this, NoDefaultLocaleInSupportedLocalesError);
|
|
200
|
+
return _call_super(this, NoDefaultLocaleInSupportedLocalesError, arguments);
|
|
201
|
+
}
|
|
202
|
+
return NoDefaultLocaleInSupportedLocalesError;
|
|
203
|
+
}(import_errors.CustomError);
|
|
204
|
+
var InvalidLocaleSetError = /*#__PURE__*/ function(_import_errors_CustomError) {
|
|
205
|
+
_inherits(InvalidLocaleSetError, _import_errors_CustomError);
|
|
206
|
+
function InvalidLocaleSetError() {
|
|
207
|
+
_class_call_check(this, InvalidLocaleSetError);
|
|
208
|
+
return _call_super(this, InvalidLocaleSetError, arguments);
|
|
209
|
+
}
|
|
210
|
+
return InvalidLocaleSetError;
|
|
211
|
+
}(import_errors.CustomError);
|
|
212
|
+
var InvalidI18nContextStateError = /*#__PURE__*/ function(_import_errors_CustomError) {
|
|
213
|
+
_inherits(InvalidI18nContextStateError, _import_errors_CustomError);
|
|
214
|
+
function InvalidI18nContextStateError() {
|
|
215
|
+
_class_call_check(this, InvalidI18nContextStateError);
|
|
216
|
+
return _call_super(this, InvalidI18nContextStateError, arguments);
|
|
217
|
+
}
|
|
218
|
+
return InvalidI18nContextStateError;
|
|
219
|
+
}(import_errors.CustomError);
|
|
220
|
+
// src/createI18nStore.ts
|
|
221
|
+
var preRequisites = [
|
|
222
|
+
"supportedLocales",
|
|
223
|
+
"localeSet",
|
|
224
|
+
"defaultLocale"
|
|
225
|
+
];
|
|
226
|
+
function createI18nStore(i18nStoreParams) {
|
|
227
|
+
if (import_shared_utils.validator.hasInvalidValuePrerequisites(i18nStoreParams)) {
|
|
228
|
+
throw new InvalidI18nStoreParamsError("Invalid params when initiating i18nStore. ".concat(preRequisites.join(", "), " is required."));
|
|
229
|
+
}
|
|
230
|
+
var supportedLocales = i18nStoreParams.supportedLocales, defaultLocale = i18nStoreParams.defaultLocale, localeSet = i18nStoreParams.localeSet;
|
|
231
|
+
var _supportedLocales = supportedLocales;
|
|
232
|
+
var _defaultLocale = defaultLocale;
|
|
233
|
+
isInvalidSupportedLocales(_supportedLocales, _defaultLocale, localeSet);
|
|
234
|
+
return _object_spread({}, i18nStoreParams);
|
|
235
|
+
}
|
|
236
|
+
function isInvalidSupportedLocales(supportedLocales, defaultLocale, localeSet) {
|
|
237
|
+
if (!Array.isArray(supportedLocales) || typeof defaultLocale !== "string" || (typeof localeSet === "undefined" ? "undefined" : _type_of(localeSet)) !== "object") {
|
|
238
|
+
throw new InvalidI18nStoreParamsError("Invalid params. please input the correct params for initiating i18nStore.");
|
|
239
|
+
}
|
|
240
|
+
var isNotAllString = false;
|
|
241
|
+
var isNoDefaultInSupported = true;
|
|
242
|
+
var isNotValidLocaleSet = false;
|
|
243
|
+
supportedLocales.forEach(function(l) {
|
|
244
|
+
if (typeof l !== "string") isNotAllString = true;
|
|
245
|
+
if (l === defaultLocale) isNoDefaultInSupported = false;
|
|
246
|
+
if (import_shared_utils.validator.isNullish(localeSet[l])) isNotValidLocaleSet = true;
|
|
247
|
+
});
|
|
248
|
+
if (isNotAllString) {
|
|
249
|
+
throw new InvalidSupportedLocalesError('"supportedLocales" must be string array');
|
|
250
|
+
}
|
|
251
|
+
if (isNoDefaultInSupported) {
|
|
252
|
+
throw new NoDefaultLocaleInSupportedLocalesError('There is no "defaultLocale" in "supportedLocales"');
|
|
253
|
+
}
|
|
254
|
+
if (isNotValidLocaleSet) {
|
|
255
|
+
throw new InvalidLocaleSetError('"localeSet" is invalid. The keys consist of supportedLocales');
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// src/IntlProvider.tsx
|
|
259
|
+
var import_react_intl = require("react-intl");
|
|
260
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
261
|
+
function IntlProvider(param) {
|
|
262
|
+
var i18nStore = param.i18nStore, currentLocale = param.currentLocale, children = param.children;
|
|
263
|
+
var defaultLocale = i18nStore.defaultLocale, localeSet = i18nStore.localeSet;
|
|
264
|
+
var locale = currentLocale !== null && currentLocale !== void 0 ? currentLocale : defaultLocale;
|
|
265
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_intl.IntlProvider, {
|
|
266
|
+
locale: locale,
|
|
267
|
+
messages: localeSet[currentLocale],
|
|
268
|
+
children: children
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
// src/useTranslation.ts
|
|
272
|
+
var import_react_intl2 = require("react-intl");
|
|
273
|
+
var intlCache = (0, import_react_intl2.createIntlCache)();
|
|
274
|
+
function useTranslation(param) {
|
|
275
|
+
var sheetTitle = param.sheetTitle;
|
|
276
|
+
var currentIntl = (0, import_react_intl2.useIntl)();
|
|
277
|
+
var totalMessages = currentIntl.messages;
|
|
278
|
+
var targetMessages = totalMessages[sheetTitle];
|
|
279
|
+
var newIntl = (0, import_react_intl2.createIntl)(_object_spread_props(_object_spread({}, currentIntl), {
|
|
280
|
+
messages: targetMessages
|
|
281
|
+
}), intlCache);
|
|
282
|
+
var t = function(id, values, opts, _descriptor) {
|
|
283
|
+
var descriptor = _object_spread_props(_object_spread({}, _descriptor !== null && _descriptor !== void 0 ? _descriptor : {}), {
|
|
284
|
+
id: id
|
|
285
|
+
});
|
|
286
|
+
return newIntl.$t(descriptor, values, opts);
|
|
287
|
+
};
|
|
288
|
+
return {
|
|
289
|
+
t: t
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
// src/createI18nContext.tsx
|
|
293
|
+
var import_shared_utils2 = require("@sheet-i18n/shared-utils");
|
|
294
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
295
|
+
function createI18nContext(i18nStore) {
|
|
296
|
+
if (import_shared_utils2.validator.isNullish(i18nStore)) {
|
|
297
|
+
throw new InvalidI18nContextStateError("\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter");
|
|
298
|
+
}
|
|
299
|
+
var IntlProviderImpl = function(param) {
|
|
300
|
+
var currentLocale = param.currentLocale, children = param.children;
|
|
301
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(IntlProvider, {
|
|
302
|
+
currentLocale: currentLocale,
|
|
303
|
+
i18nStore: i18nStore,
|
|
304
|
+
children: children
|
|
305
|
+
});
|
|
306
|
+
};
|
|
307
|
+
var useTranslationImpl = function(sheetTitle) {
|
|
308
|
+
return useTranslation({
|
|
309
|
+
sheetTitle: sheetTitle,
|
|
310
|
+
i18nStore: i18nStore
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
return {
|
|
314
|
+
IntlProvider: IntlProviderImpl,
|
|
315
|
+
useTranslation: useTranslationImpl
|
|
316
|
+
};
|
|
317
|
+
}
|
|
27
318
|
// Annotate the CommonJS export names for ESM import in node:
|
|
28
319
|
0 && (module.exports = {
|
|
29
|
-
|
|
320
|
+
IntlProvider: IntlProvider,
|
|
321
|
+
createI18nContext: createI18nContext,
|
|
322
|
+
createI18nStore: createI18nStore,
|
|
323
|
+
useTranslation: useTranslation
|
|
30
324
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,259 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
1
|
+
// src/createI18nStore.ts
|
|
2
|
+
function _assert_this_initialized(self) {
|
|
3
|
+
if (self === void 0) {
|
|
4
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
5
|
+
}
|
|
6
|
+
return self;
|
|
7
|
+
}
|
|
8
|
+
function _call_super(_this, derived, args) {
|
|
9
|
+
derived = _get_prototype_of(derived);
|
|
10
|
+
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
|
|
11
|
+
}
|
|
12
|
+
function _class_call_check(instance, Constructor) {
|
|
13
|
+
if (!(instance instanceof Constructor)) {
|
|
14
|
+
throw new TypeError("Cannot call a class as a function");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function _define_property(obj, key, value) {
|
|
18
|
+
if (key in obj) {
|
|
19
|
+
Object.defineProperty(obj, key, {
|
|
20
|
+
value: value,
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true
|
|
24
|
+
});
|
|
25
|
+
} else {
|
|
26
|
+
obj[key] = value;
|
|
27
|
+
}
|
|
28
|
+
return obj;
|
|
29
|
+
}
|
|
30
|
+
function _get_prototype_of(o) {
|
|
31
|
+
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
|
|
32
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
33
|
+
};
|
|
34
|
+
return _get_prototype_of(o);
|
|
35
|
+
}
|
|
36
|
+
function _inherits(subClass, superClass) {
|
|
37
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
38
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
39
|
+
}
|
|
40
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
41
|
+
constructor: {
|
|
42
|
+
value: subClass,
|
|
43
|
+
writable: true,
|
|
44
|
+
configurable: true
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
if (superClass) _set_prototype_of(subClass, superClass);
|
|
48
|
+
}
|
|
49
|
+
function _object_spread(target) {
|
|
50
|
+
for(var i = 1; i < arguments.length; i++){
|
|
51
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
52
|
+
var ownKeys = Object.keys(source);
|
|
53
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
54
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
55
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
ownKeys.forEach(function(key) {
|
|
59
|
+
_define_property(target, key, source[key]);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return target;
|
|
63
|
+
}
|
|
64
|
+
function ownKeys(object, enumerableOnly) {
|
|
65
|
+
var keys = Object.keys(object);
|
|
66
|
+
if (Object.getOwnPropertySymbols) {
|
|
67
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
68
|
+
if (enumerableOnly) {
|
|
69
|
+
symbols = symbols.filter(function(sym) {
|
|
70
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
keys.push.apply(keys, symbols);
|
|
74
|
+
}
|
|
75
|
+
return keys;
|
|
76
|
+
}
|
|
77
|
+
function _object_spread_props(target, source) {
|
|
78
|
+
source = source != null ? source : {};
|
|
79
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
80
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
81
|
+
} else {
|
|
82
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
83
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return target;
|
|
87
|
+
}
|
|
88
|
+
function _possible_constructor_return(self, call) {
|
|
89
|
+
if (call && (_type_of(call) === "object" || typeof call === "function")) {
|
|
90
|
+
return call;
|
|
91
|
+
}
|
|
92
|
+
return _assert_this_initialized(self);
|
|
93
|
+
}
|
|
94
|
+
function _set_prototype_of(o, p) {
|
|
95
|
+
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
|
96
|
+
o.__proto__ = p;
|
|
97
|
+
return o;
|
|
98
|
+
};
|
|
99
|
+
return _set_prototype_of(o, p);
|
|
100
|
+
}
|
|
101
|
+
function _type_of(obj) {
|
|
102
|
+
"@swc/helpers - typeof";
|
|
103
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
104
|
+
}
|
|
105
|
+
function _is_native_reflect_construct() {
|
|
106
|
+
try {
|
|
107
|
+
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
|
108
|
+
} catch (_) {}
|
|
109
|
+
return (_is_native_reflect_construct = function() {
|
|
110
|
+
return !!result;
|
|
111
|
+
})();
|
|
112
|
+
}
|
|
113
|
+
import { validator } from "@sheet-i18n/shared-utils";
|
|
114
|
+
// src/Errors.ts
|
|
115
|
+
import { CustomError } from "@sheet-i18n/errors";
|
|
116
|
+
var InvalidI18nStoreParamsError = /*#__PURE__*/ function(CustomError) {
|
|
117
|
+
"use strict";
|
|
118
|
+
_inherits(InvalidI18nStoreParamsError, CustomError);
|
|
119
|
+
function InvalidI18nStoreParamsError() {
|
|
120
|
+
_class_call_check(this, InvalidI18nStoreParamsError);
|
|
121
|
+
return _call_super(this, InvalidI18nStoreParamsError, arguments);
|
|
122
|
+
}
|
|
123
|
+
return InvalidI18nStoreParamsError;
|
|
124
|
+
}(CustomError);
|
|
125
|
+
var InvalidSupportedLocalesError = /*#__PURE__*/ function(CustomError) {
|
|
126
|
+
"use strict";
|
|
127
|
+
_inherits(InvalidSupportedLocalesError, CustomError);
|
|
128
|
+
function InvalidSupportedLocalesError() {
|
|
129
|
+
_class_call_check(this, InvalidSupportedLocalesError);
|
|
130
|
+
return _call_super(this, InvalidSupportedLocalesError, arguments);
|
|
131
|
+
}
|
|
132
|
+
return InvalidSupportedLocalesError;
|
|
133
|
+
}(CustomError);
|
|
134
|
+
var NoDefaultLocaleInSupportedLocalesError = /*#__PURE__*/ function(CustomError) {
|
|
135
|
+
"use strict";
|
|
136
|
+
_inherits(NoDefaultLocaleInSupportedLocalesError, CustomError);
|
|
137
|
+
function NoDefaultLocaleInSupportedLocalesError() {
|
|
138
|
+
_class_call_check(this, NoDefaultLocaleInSupportedLocalesError);
|
|
139
|
+
return _call_super(this, NoDefaultLocaleInSupportedLocalesError, arguments);
|
|
140
|
+
}
|
|
141
|
+
return NoDefaultLocaleInSupportedLocalesError;
|
|
142
|
+
}(CustomError);
|
|
143
|
+
var InvalidLocaleSetError = /*#__PURE__*/ function(CustomError) {
|
|
144
|
+
"use strict";
|
|
145
|
+
_inherits(InvalidLocaleSetError, CustomError);
|
|
146
|
+
function InvalidLocaleSetError() {
|
|
147
|
+
_class_call_check(this, InvalidLocaleSetError);
|
|
148
|
+
return _call_super(this, InvalidLocaleSetError, arguments);
|
|
149
|
+
}
|
|
150
|
+
return InvalidLocaleSetError;
|
|
151
|
+
}(CustomError);
|
|
152
|
+
var InvalidI18nContextStateError = /*#__PURE__*/ function(CustomError) {
|
|
153
|
+
"use strict";
|
|
154
|
+
_inherits(InvalidI18nContextStateError, CustomError);
|
|
155
|
+
function InvalidI18nContextStateError() {
|
|
156
|
+
_class_call_check(this, InvalidI18nContextStateError);
|
|
157
|
+
return _call_super(this, InvalidI18nContextStateError, arguments);
|
|
158
|
+
}
|
|
159
|
+
return InvalidI18nContextStateError;
|
|
160
|
+
}(CustomError);
|
|
161
|
+
// src/createI18nStore.ts
|
|
162
|
+
var preRequisites = [
|
|
163
|
+
"supportedLocales",
|
|
164
|
+
"localeSet",
|
|
165
|
+
"defaultLocale"
|
|
166
|
+
];
|
|
167
|
+
function createI18nStore(i18nStoreParams) {
|
|
168
|
+
if (validator.hasInvalidValuePrerequisites(i18nStoreParams)) {
|
|
169
|
+
throw new InvalidI18nStoreParamsError("Invalid params when initiating i18nStore. ".concat(preRequisites.join(", "), " is required."));
|
|
170
|
+
}
|
|
171
|
+
var supportedLocales = i18nStoreParams.supportedLocales, defaultLocale = i18nStoreParams.defaultLocale, localeSet = i18nStoreParams.localeSet;
|
|
172
|
+
var _supportedLocales = supportedLocales;
|
|
173
|
+
var _defaultLocale = defaultLocale;
|
|
174
|
+
isInvalidSupportedLocales(_supportedLocales, _defaultLocale, localeSet);
|
|
175
|
+
return _object_spread({}, i18nStoreParams);
|
|
176
|
+
}
|
|
177
|
+
function isInvalidSupportedLocales(supportedLocales, defaultLocale, localeSet) {
|
|
178
|
+
if (!Array.isArray(supportedLocales) || typeof defaultLocale !== "string" || (typeof localeSet === "undefined" ? "undefined" : _type_of(localeSet)) !== "object") {
|
|
179
|
+
throw new InvalidI18nStoreParamsError("Invalid params. please input the correct params for initiating i18nStore.");
|
|
180
|
+
}
|
|
181
|
+
var isNotAllString = false;
|
|
182
|
+
var isNoDefaultInSupported = true;
|
|
183
|
+
var isNotValidLocaleSet = false;
|
|
184
|
+
supportedLocales.forEach(function(l) {
|
|
185
|
+
if (typeof l !== "string") isNotAllString = true;
|
|
186
|
+
if (l === defaultLocale) isNoDefaultInSupported = false;
|
|
187
|
+
if (validator.isNullish(localeSet[l])) isNotValidLocaleSet = true;
|
|
188
|
+
});
|
|
189
|
+
if (isNotAllString) {
|
|
190
|
+
throw new InvalidSupportedLocalesError('"supportedLocales" must be string array');
|
|
191
|
+
}
|
|
192
|
+
if (isNoDefaultInSupported) {
|
|
193
|
+
throw new NoDefaultLocaleInSupportedLocalesError('There is no "defaultLocale" in "supportedLocales"');
|
|
194
|
+
}
|
|
195
|
+
if (isNotValidLocaleSet) {
|
|
196
|
+
throw new InvalidLocaleSetError('"localeSet" is invalid. The keys consist of supportedLocales');
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
// src/IntlProvider.tsx
|
|
200
|
+
import { IntlProvider as ReactIntlProvider } from "react-intl";
|
|
201
|
+
import { jsx } from "react/jsx-runtime";
|
|
202
|
+
function IntlProvider(param) {
|
|
203
|
+
var i18nStore = param.i18nStore, currentLocale = param.currentLocale, children = param.children;
|
|
204
|
+
var defaultLocale = i18nStore.defaultLocale, localeSet = i18nStore.localeSet;
|
|
205
|
+
var locale = currentLocale !== null && currentLocale !== void 0 ? currentLocale : defaultLocale;
|
|
206
|
+
return /* @__PURE__ */ jsx(ReactIntlProvider, {
|
|
207
|
+
locale: locale,
|
|
208
|
+
messages: localeSet[currentLocale],
|
|
209
|
+
children: children
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
// src/useTranslation.ts
|
|
213
|
+
import { createIntlCache, createIntl, useIntl } from "react-intl";
|
|
214
|
+
var intlCache = createIntlCache();
|
|
215
|
+
function useTranslation(param) {
|
|
216
|
+
var sheetTitle = param.sheetTitle;
|
|
217
|
+
var currentIntl = useIntl();
|
|
218
|
+
var totalMessages = currentIntl.messages;
|
|
219
|
+
var targetMessages = totalMessages[sheetTitle];
|
|
220
|
+
var newIntl = createIntl(_object_spread_props(_object_spread({}, currentIntl), {
|
|
221
|
+
messages: targetMessages
|
|
222
|
+
}), intlCache);
|
|
223
|
+
var t = function(id, values, opts, _descriptor) {
|
|
224
|
+
var descriptor = _object_spread_props(_object_spread({}, _descriptor !== null && _descriptor !== void 0 ? _descriptor : {}), {
|
|
225
|
+
id: id
|
|
226
|
+
});
|
|
227
|
+
return newIntl.$t(descriptor, values, opts);
|
|
228
|
+
};
|
|
229
|
+
return {
|
|
230
|
+
t: t
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
// src/createI18nContext.tsx
|
|
234
|
+
import { validator as validator2 } from "@sheet-i18n/shared-utils";
|
|
235
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
236
|
+
function createI18nContext(i18nStore) {
|
|
237
|
+
if (validator2.isNullish(i18nStore)) {
|
|
238
|
+
throw new InvalidI18nContextStateError("\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter");
|
|
239
|
+
}
|
|
240
|
+
var IntlProviderImpl = function(param) {
|
|
241
|
+
var currentLocale = param.currentLocale, children = param.children;
|
|
242
|
+
return /* @__PURE__ */ jsx2(IntlProvider, {
|
|
243
|
+
currentLocale: currentLocale,
|
|
244
|
+
i18nStore: i18nStore,
|
|
245
|
+
children: children
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
var useTranslationImpl = function(sheetTitle) {
|
|
249
|
+
return useTranslation({
|
|
250
|
+
sheetTitle: sheetTitle,
|
|
251
|
+
i18nStore: i18nStore
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
return {
|
|
255
|
+
IntlProvider: IntlProviderImpl,
|
|
256
|
+
useTranslation: useTranslationImpl
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
export { IntlProvider, createI18nContext, createI18nStore, useTranslation };
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sheet-i18n/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1-canary.1",
|
|
4
4
|
"description": "i18n client logic based on react",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
|
-
"types": "./dist/index.d.
|
|
7
|
+
"types": "./dist/index.d.mts",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist/**/*"
|
|
10
10
|
],
|
|
@@ -25,7 +25,16 @@
|
|
|
25
25
|
},
|
|
26
26
|
"license": "ISC",
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@
|
|
28
|
+
"@swc/core": "^1.10.2",
|
|
29
|
+
"@types/react": "^19.0.2",
|
|
30
|
+
"@types/react-dom": "^19.0.2",
|
|
31
|
+
"@sheet-i18n/errors": "0.2.2-canary.0",
|
|
32
|
+
"@sheet-i18n/typescript-config": "0.2.2-canary.0",
|
|
33
|
+
"@sheet-i18n/shared-utils": "0.2.2-canary.0"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"react": "^19.0.0",
|
|
37
|
+
"react-intl": "^7.0.4"
|
|
29
38
|
},
|
|
30
39
|
"scripts": {
|
|
31
40
|
"build": "tsup",
|