@pisell/core 1.0.5 → 1.0.6

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.
@@ -13,27 +13,27 @@ export declare class Locales {
13
13
  [key in Locale]: LibraryItem;
14
14
  };
15
15
  constructor(app: App, options?: LocalesOptions);
16
- getLocale(): string;
17
- getCurrentTexts(locale?: Locale): {
16
+ getLocale: () => string;
17
+ getCurrentTexts: (locale?: Locale) => {
18
18
  [key: string]: string;
19
19
  };
20
- setLocale(locale: Locale, reload?: boolean): void;
21
- getText(id: string, locale?: Locale): string;
22
- isCN(): boolean;
20
+ setLocale: (locale: Locale, reload?: boolean) => void;
21
+ getText: (id: string, locale?: Locale) => string;
22
+ isCN: () => boolean;
23
23
  loadLibraryByUrl: (urls: LoadLibraryByUrlParams) => Promise<{
24
24
  [x: string]: LibraryItem;
25
25
  }>;
26
26
  loadLibraryByItems(libraryList: LibraryItem[]): void;
27
- loadLibrary(urls: LoadLibraryByUrlParams): Promise<{
27
+ loadLibrary: (urls: LoadLibraryByUrlParams) => Promise<{
28
28
  [x: string]: LibraryItem;
29
29
  }>;
30
- translation(text: string | {
31
- [key: Locale]: string;
32
- }, locale?: Locale): string | number;
33
- getLibraryByData(data: LibraryItem[]): LibraryItem;
34
- createTextsByLibrary(id: string, library?: {
35
- [key in Locale]: LibraryItem;
36
- }): {
30
+ translation: (text: string | {
31
+ [key: string]: string;
32
+ }, locale?: Locale) => string | number;
33
+ getLibraryByData: (data: LibraryItem[]) => LibraryItem;
34
+ createTextsByLibrary: (id: string, library?: {
35
+ [x: string]: LibraryItem;
36
+ } | undefined) => {
37
37
  [x: string]: string;
38
38
  };
39
39
  }
@@ -32,6 +32,28 @@ export var Locales = /*#__PURE__*/function () {
32
32
  pre[cur.locale] = cur;
33
33
  return pre;
34
34
  }, {}));
35
+ _defineProperty(this, "getLocale", function () {
36
+ return _this.locale;
37
+ });
38
+ _defineProperty(this, "getCurrentTexts", function (locale) {
39
+ return _this.library[locale || _this.locale].translations;
40
+ });
41
+ _defineProperty(this, "setLocale", function (locale) {
42
+ var reload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
43
+ _this.locale = locale;
44
+ if (reload) {
45
+ window.location.reload();
46
+ return;
47
+ }
48
+ });
49
+ _defineProperty(this, "getText", function (id, locale) {
50
+ var _this$getCurrentTexts;
51
+ return ((_this$getCurrentTexts = _this.getCurrentTexts(locale)) === null || _this$getCurrentTexts === void 0 ? void 0 : _this$getCurrentTexts[id]) || id;
52
+ });
53
+ _defineProperty(this, "isCN", function () {
54
+ var locale = _this.getLocale();
55
+ return locale === "zh-CN" || locale === "zh-HK";
56
+ });
35
57
  _defineProperty(this, "loadLibraryByUrl", /*#__PURE__*/function () {
36
58
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(urls) {
37
59
  var fetchList, res, library, _iterator, _step, item, _item;
@@ -121,87 +143,29 @@ export var Locales = /*#__PURE__*/function () {
121
143
  return _ref.apply(this, arguments);
122
144
  };
123
145
  }());
124
- this.app = app;
125
- this.locale = (options === null || options === void 0 ? void 0 : options.locale) || this.app.storage.getStorage('locale') || en.locale;
126
- this.library = (options === null || options === void 0 ? void 0 : options.library) || [original, en, zhCN, zhHK].reduce(function (pre, cur) {
127
- pre[cur.locale] = cur;
128
- return pre;
129
- }, {});
130
- }
131
- _createClass(Locales, [{
132
- key: "getLocale",
133
- value: function getLocale() {
134
- return this.locale;
135
- }
136
- }, {
137
- key: "getCurrentTexts",
138
- value: function getCurrentTexts(locale) {
139
- return this.library[locale || this.locale].translations;
140
- }
141
- }, {
142
- key: "setLocale",
143
- value: function setLocale(locale) {
144
- var reload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
145
- this.locale = locale;
146
- if (reload) {
147
- window.location.reload();
148
- return;
149
- }
150
- }
151
- }, {
152
- key: "getText",
153
- value: function getText(id, locale) {
154
- var _this$getCurrentTexts;
155
- return ((_this$getCurrentTexts = this.getCurrentTexts(locale)) === null || _this$getCurrentTexts === void 0 ? void 0 : _this$getCurrentTexts[id]) || id;
156
- }
157
- }, {
158
- key: "isCN",
159
- value: function isCN() {
160
- var locale = this.getLocale();
161
- return locale === "zh-CN" || locale === "zh-HK";
162
- }
163
- }, {
164
- key: "loadLibraryByItems",
165
- value: function loadLibraryByItems(libraryList) {
166
- var library = _objectSpread({}, this.library);
167
- libraryList.forEach(function (item) {
168
- library = _objectSpread(_objectSpread({}, library), {}, _defineProperty({}, item.locale, _.mergeWith(library[item.locale] || {}, item)));
169
- });
170
-
171
- // 设置
172
- this.library = library;
173
- }
174
- }, {
175
- key: "loadLibrary",
176
- value: function loadLibrary(urls) {
177
- return this.loadLibraryByUrl(urls);
178
- }
179
- }, {
180
- key: "translation",
181
- value: function translation(text, locale) {
146
+ _defineProperty(this, "loadLibrary", function (urls) {
147
+ return _this.loadLibraryByUrl(urls);
148
+ });
149
+ _defineProperty(this, "translation", function (text, locale) {
182
150
  if (!text) {
183
151
  return "";
184
152
  }
185
153
  if (isString(text) || isNumber(text)) {
186
154
  return text;
187
155
  }
188
- var primary = locale || this.getLocale();
156
+ var primary = locale || _this.getLocale();
189
157
  if (typeof text === 'string') {
190
158
  return text;
191
159
  }
192
160
  return text[primary] || text["original"] || text["en"] || text["zh-CN"] || text["zh-HK"];
193
- }
194
- }, {
195
- key: "getLibraryByData",
196
- value: function getLibraryByData(data) {
161
+ });
162
+ _defineProperty(this, "getLibraryByData", function (data) {
197
163
  return data.reduce(function (pre, next) {
198
164
  return _objectSpread(_objectSpread({}, pre), {}, _defineProperty({}, next.locale, next));
199
165
  }, {});
200
- }
201
- }, {
202
- key: "createTextsByLibrary",
203
- value: function createTextsByLibrary(id, library) {
204
- var _library = library || this.library;
166
+ });
167
+ _defineProperty(this, "createTextsByLibrary", function (id, library) {
168
+ var _library = library || _this.library;
205
169
  return Object.keys(_library).reduce(function (pre, next) {
206
170
  var _library$next$transla;
207
171
  var _pre = _objectSpread({}, pre);
@@ -211,6 +175,24 @@ export var Locales = /*#__PURE__*/function () {
211
175
  }
212
176
  return _pre;
213
177
  }, {});
178
+ });
179
+ this.app = app;
180
+ this.locale = (options === null || options === void 0 ? void 0 : options.locale) || this.app.storage.getStorage('locale') || en.locale;
181
+ this.library = (options === null || options === void 0 ? void 0 : options.library) || [original, en, zhCN, zhHK].reduce(function (pre, cur) {
182
+ pre[cur.locale] = cur;
183
+ return pre;
184
+ }, {});
185
+ }
186
+ _createClass(Locales, [{
187
+ key: "loadLibraryByItems",
188
+ value: function loadLibraryByItems(libraryList) {
189
+ var library = _objectSpread({}, this.library);
190
+ libraryList.forEach(function (item) {
191
+ library = _objectSpread(_objectSpread({}, library), {}, _defineProperty({}, item.locale, _.mergeWith(library[item.locale] || {}, item)));
192
+ });
193
+
194
+ // 设置
195
+ this.library = library;
214
196
  }
215
197
  }]);
216
198
  return Locales;
@@ -121,7 +121,10 @@ export var RouterManager = /*#__PURE__*/function () {
121
121
  firstRoute = path;
122
122
  }
123
123
  var component = this.renderComponent(item);
124
- routes.push( /*#__PURE__*/React.createElement(Route, {
124
+ routes.push(
125
+ /*#__PURE__*/
126
+ //@ts-ignore
127
+ React.createElement(Route, {
125
128
  path: path,
126
129
  key: path,
127
130
  exact: true
@@ -13,27 +13,27 @@ export declare class Locales {
13
13
  [key in Locale]: LibraryItem;
14
14
  };
15
15
  constructor(app: App, options?: LocalesOptions);
16
- getLocale(): string;
17
- getCurrentTexts(locale?: Locale): {
16
+ getLocale: () => string;
17
+ getCurrentTexts: (locale?: Locale) => {
18
18
  [key: string]: string;
19
19
  };
20
- setLocale(locale: Locale, reload?: boolean): void;
21
- getText(id: string, locale?: Locale): string;
22
- isCN(): boolean;
20
+ setLocale: (locale: Locale, reload?: boolean) => void;
21
+ getText: (id: string, locale?: Locale) => string;
22
+ isCN: () => boolean;
23
23
  loadLibraryByUrl: (urls: LoadLibraryByUrlParams) => Promise<{
24
24
  [x: string]: LibraryItem;
25
25
  }>;
26
26
  loadLibraryByItems(libraryList: LibraryItem[]): void;
27
- loadLibrary(urls: LoadLibraryByUrlParams): Promise<{
27
+ loadLibrary: (urls: LoadLibraryByUrlParams) => Promise<{
28
28
  [x: string]: LibraryItem;
29
29
  }>;
30
- translation(text: string | {
31
- [key: Locale]: string;
32
- }, locale?: Locale): string | number;
33
- getLibraryByData(data: LibraryItem[]): LibraryItem;
34
- createTextsByLibrary(id: string, library?: {
35
- [key in Locale]: LibraryItem;
36
- }): {
30
+ translation: (text: string | {
31
+ [key: string]: string;
32
+ }, locale?: Locale) => string | number;
33
+ getLibraryByData: (data: LibraryItem[]) => LibraryItem;
34
+ createTextsByLibrary: (id: string, library?: {
35
+ [x: string]: LibraryItem;
36
+ } | undefined) => {
37
37
  [x: string]: string;
38
38
  };
39
39
  }
@@ -47,6 +47,27 @@ var Locales = class {
47
47
  pre[cur.locale] = cur;
48
48
  return pre;
49
49
  }, {});
50
+ this.getLocale = () => {
51
+ return this.locale;
52
+ };
53
+ this.getCurrentTexts = (locale) => {
54
+ return this.library[locale || this.locale].translations;
55
+ };
56
+ this.setLocale = (locale, reload = false) => {
57
+ this.locale = locale;
58
+ if (reload) {
59
+ window.location.reload();
60
+ return;
61
+ }
62
+ };
63
+ this.getText = (id, locale) => {
64
+ var _a;
65
+ return ((_a = this.getCurrentTexts(locale)) == null ? void 0 : _a[id]) || id;
66
+ };
67
+ this.isCN = () => {
68
+ let locale = this.getLocale();
69
+ return locale === "zh-CN" || locale === "zh-HK";
70
+ };
50
71
  this.loadLibraryByUrl = async (urls) => {
51
72
  try {
52
73
  let fetchList = urls.map((item) => {
@@ -77,6 +98,42 @@ var Locales = class {
77
98
  throw err;
78
99
  }
79
100
  };
101
+ this.loadLibrary = (urls) => {
102
+ return this.loadLibraryByUrl(urls);
103
+ };
104
+ this.translation = (text, locale) => {
105
+ if (!text) {
106
+ return "";
107
+ }
108
+ if ((0, import_utils.isString)(text) || (0, import_utils.isNumber)(text)) {
109
+ return text;
110
+ }
111
+ let primary = locale || this.getLocale();
112
+ if (typeof text === "string") {
113
+ return text;
114
+ }
115
+ return text[primary] || text["original"] || text["en"] || text["zh-CN"] || text["zh-HK"];
116
+ };
117
+ this.getLibraryByData = (data) => {
118
+ return data.reduce((pre, next) => {
119
+ return {
120
+ ...pre,
121
+ [next.locale]: next
122
+ };
123
+ }, {});
124
+ };
125
+ this.createTextsByLibrary = (id, library) => {
126
+ const _library = library || this.library;
127
+ return Object.keys(_library).reduce((pre, next) => {
128
+ var _a;
129
+ let _pre = { ...pre };
130
+ let cur = (_a = _library[next].translations) == null ? void 0 : _a[id];
131
+ if (cur) {
132
+ _pre[_library[next].locale] = cur;
133
+ }
134
+ return _pre;
135
+ }, {});
136
+ };
80
137
  this.app = app;
81
138
  this.locale = (options == null ? void 0 : options.locale) || this.app.storage.getStorage("locale") || import_en.default.locale;
82
139
  this.library = (options == null ? void 0 : options.library) || [import_original.default, import_en.default, import_zh_CN.default, import_zh_HK.default].reduce((pre, cur) => {
@@ -84,27 +141,6 @@ var Locales = class {
84
141
  return pre;
85
142
  }, {});
86
143
  }
87
- getLocale() {
88
- return this.locale;
89
- }
90
- getCurrentTexts(locale) {
91
- return this.library[locale || this.locale].translations;
92
- }
93
- setLocale(locale, reload = false) {
94
- this.locale = locale;
95
- if (reload) {
96
- window.location.reload();
97
- return;
98
- }
99
- }
100
- getText(id, locale) {
101
- var _a;
102
- return ((_a = this.getCurrentTexts(locale)) == null ? void 0 : _a[id]) || id;
103
- }
104
- isCN() {
105
- let locale = this.getLocale();
106
- return locale === "zh-CN" || locale === "zh-HK";
107
- }
108
144
  loadLibraryByItems(libraryList) {
109
145
  let library = { ...this.library };
110
146
  libraryList.forEach((item) => {
@@ -115,42 +151,6 @@ var Locales = class {
115
151
  });
116
152
  this.library = library;
117
153
  }
118
- loadLibrary(urls) {
119
- return this.loadLibraryByUrl(urls);
120
- }
121
- translation(text, locale) {
122
- if (!text) {
123
- return "";
124
- }
125
- if ((0, import_utils.isString)(text) || (0, import_utils.isNumber)(text)) {
126
- return text;
127
- }
128
- let primary = locale || this.getLocale();
129
- if (typeof text === "string") {
130
- return text;
131
- }
132
- return text[primary] || text["original"] || text["en"] || text["zh-CN"] || text["zh-HK"];
133
- }
134
- getLibraryByData(data) {
135
- return data.reduce((pre, next) => {
136
- return {
137
- ...pre,
138
- [next.locale]: next
139
- };
140
- }, {});
141
- }
142
- createTextsByLibrary(id, library) {
143
- const _library = library || this.library;
144
- return Object.keys(_library).reduce((pre, next) => {
145
- var _a;
146
- let _pre = { ...pre };
147
- let cur = (_a = _library[next].translations) == null ? void 0 : _a[id];
148
- if (cur) {
149
- _pre[_library[next].locale] = cur;
150
- }
151
- return _pre;
152
- }, {});
153
- }
154
154
  };
155
155
  // Annotate the CommonJS export names for ESM import in node:
156
156
  0 && (module.exports = {
@@ -106,6 +106,7 @@ var RouterManager = class {
106
106
  }
107
107
  const component = this.renderComponent(item);
108
108
  routes.push(
109
+ //@ts-ignore
109
110
  /* @__PURE__ */ import_react.default.createElement(import_react_router_dom.Route, { path, key: path, exact: true }, /* @__PURE__ */ import_react.default.createElement(import_react.Suspense, { fallback: fallback || /* @__PURE__ */ import_react.default.createElement("div", null) }, component))
110
111
  );
111
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pisell/core",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "sideEffects": false,
5
5
  "main": "./lib/index.js",
6
6
  "module": "./es/index.js",