@nocobase/plugin-localization 1.0.0-alpha.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.
Files changed (54) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +35 -0
  3. package/client.d.ts +3 -0
  4. package/client.js +65 -0
  5. package/dist/client/Localization.d.ts +2 -0
  6. package/dist/client/index.d.ts +5 -0
  7. package/dist/client/index.js +1 -0
  8. package/dist/client/locale/index.d.ts +2 -0
  9. package/dist/client/schemas/localization.d.ts +2 -0
  10. package/dist/externalVersion.js +15 -0
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.js +39 -0
  13. package/dist/locale/en-US.json +24 -0
  14. package/dist/locale/ko_KR.json +23 -0
  15. package/dist/locale/zh-CN.json +24 -0
  16. package/dist/node_modules/deepmerge/.editorconfig +7 -0
  17. package/dist/node_modules/deepmerge/.eslintcache +1 -0
  18. package/dist/node_modules/deepmerge/dist/cjs.js +1 -0
  19. package/dist/node_modules/deepmerge/dist/umd.js +139 -0
  20. package/dist/node_modules/deepmerge/index.d.ts +20 -0
  21. package/dist/node_modules/deepmerge/index.js +106 -0
  22. package/dist/node_modules/deepmerge/license.txt +21 -0
  23. package/dist/node_modules/deepmerge/package.json +1 -0
  24. package/dist/node_modules/deepmerge/rollup.config.js +22 -0
  25. package/dist/server/actions/localization.d.ts +15 -0
  26. package/dist/server/actions/localization.js +229 -0
  27. package/dist/server/actions/localizationTexts.d.ts +5 -0
  28. package/dist/server/actions/localizationTexts.js +130 -0
  29. package/dist/server/collections/localization-texts.d.ts +2 -0
  30. package/dist/server/collections/localization-texts.js +86 -0
  31. package/dist/server/collections/localization-translations.d.ts +2 -0
  32. package/dist/server/collections/localization-translations.js +84 -0
  33. package/dist/server/constans.d.ts +8 -0
  34. package/dist/server/constans.js +48 -0
  35. package/dist/server/index.d.ts +1 -0
  36. package/dist/server/index.js +33 -0
  37. package/dist/server/migrations/20230706200900-roles-add-translation.d.ts +6 -0
  38. package/dist/server/migrations/20230706200900-roles-add-translation.js +50 -0
  39. package/dist/server/migrations/20231206161851-fix-module.d.ts +6 -0
  40. package/dist/server/migrations/20231206161851-fix-module.js +91 -0
  41. package/dist/server/migrations/20240425224508-change-locale-module.d.ts +6 -0
  42. package/dist/server/migrations/20240425224508-change-locale-module.js +42 -0
  43. package/dist/server/migrations/20240426123538-delete-pkg-name-ns.d.ts +20 -0
  44. package/dist/server/migrations/20240426123538-delete-pkg-name-ns.js +73 -0
  45. package/dist/server/plugin.d.ts +15 -0
  46. package/dist/server/plugin.js +168 -0
  47. package/dist/server/resources.d.ts +19 -0
  48. package/dist/server/resources.js +86 -0
  49. package/dist/server/utils.d.ts +3 -0
  50. package/dist/server/utils.js +80 -0
  51. package/dist/swagger/index.json +156 -0
  52. package/package.json +28 -0
  53. package/server.d.ts +3 -0
  54. package/server.js +65 -0
@@ -0,0 +1,50 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var roles_add_translation_exports = {};
19
+ __export(roles_add_translation_exports, {
20
+ default: () => AddTranslationToRoleTitleMigration
21
+ });
22
+ module.exports = __toCommonJS(roles_add_translation_exports);
23
+ var import_server = require("@nocobase/server");
24
+ class AddTranslationToRoleTitleMigration extends import_server.Migration {
25
+ appVersion = "<0.11.1-alpha.1";
26
+ async up() {
27
+ const repo = this.context.db.getRepository("fields");
28
+ const field = await repo.findOne({
29
+ where: {
30
+ collectionName: "roles",
31
+ name: "title"
32
+ }
33
+ });
34
+ if (field) {
35
+ await repo.update({
36
+ filter: {
37
+ key: field.key
38
+ },
39
+ values: {
40
+ options: {
41
+ ...field.options,
42
+ translation: true
43
+ }
44
+ }
45
+ });
46
+ }
47
+ }
48
+ async down() {
49
+ }
50
+ }
@@ -0,0 +1,6 @@
1
+ import { Migration } from '@nocobase/server';
2
+ export default class FixModuleMigration extends Migration {
3
+ appVersion: string;
4
+ up(): Promise<void>;
5
+ down(): Promise<void>;
6
+ }
@@ -0,0 +1,91 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var fix_module_exports = {};
19
+ __export(fix_module_exports, {
20
+ default: () => FixModuleMigration
21
+ });
22
+ module.exports = __toCommonJS(fix_module_exports);
23
+ var import_database = require("@nocobase/database");
24
+ var import_server = require("@nocobase/server");
25
+ var import_localization = require("../actions/localization");
26
+ var import_constans = require("../constans");
27
+ class FixModuleMigration extends import_server.Migration {
28
+ appVersion = "<0.17.0-alpha.3";
29
+ async up() {
30
+ const result = await this.app.version.satisfies("<=0.17.0-alpha.4");
31
+ if (!result) {
32
+ return;
33
+ }
34
+ const resources = await this.app.localeManager.getCacheResources("zh-CN");
35
+ const menus = await (0, import_localization.getTextsFromMenu)(this.context.db, true);
36
+ const collections = await (0, import_localization.getTextsFromDB)(this.context.db);
37
+ const db = this.context.db;
38
+ await db.getCollection("localizationTexts").sync();
39
+ await db.sequelize.transaction(async (t) => {
40
+ const menuTexts = Object.keys(menus);
41
+ await db.getModel("localizationTexts").update(
42
+ {
43
+ module: `resources.${import_constans.NAMESPACE_MENUS}`
44
+ },
45
+ {
46
+ where: {
47
+ text: {
48
+ [import_database.Op.in]: menuTexts
49
+ }
50
+ },
51
+ transaction: t
52
+ }
53
+ );
54
+ const collectionTexts = Object.keys(collections);
55
+ await db.getModel("localizationTexts").update(
56
+ {
57
+ module: `resources.${import_constans.NAMESPACE_COLLECTIONS}`
58
+ },
59
+ {
60
+ where: {
61
+ text: {
62
+ [import_database.Op.in]: collectionTexts
63
+ }
64
+ },
65
+ transaction: t
66
+ }
67
+ );
68
+ for (const [module2, resource] of Object.entries(resources)) {
69
+ if (module2 === "client") {
70
+ continue;
71
+ }
72
+ const texts = Object.keys(resource);
73
+ await db.getModel("localizationTexts").update(
74
+ {
75
+ module: `resources.${module2}`
76
+ },
77
+ {
78
+ where: {
79
+ text: {
80
+ [import_database.Op.in]: texts
81
+ }
82
+ },
83
+ transaction: t
84
+ }
85
+ );
86
+ }
87
+ });
88
+ }
89
+ async down() {
90
+ }
91
+ }
@@ -0,0 +1,6 @@
1
+ import { Migration } from '@nocobase/server';
2
+ export default class extends Migration {
3
+ on: string;
4
+ appVersion: string;
5
+ up(): Promise<void>;
6
+ }
@@ -0,0 +1,42 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var change_locale_module_exports = {};
19
+ __export(change_locale_module_exports, {
20
+ default: () => change_locale_module_default
21
+ });
22
+ module.exports = __toCommonJS(change_locale_module_exports);
23
+ var import_server = require("@nocobase/server");
24
+ class change_locale_module_default extends import_server.Migration {
25
+ on = "afterLoad";
26
+ // 'beforeLoad' or 'afterLoad'
27
+ appVersion = "<1.0.0-alpha.1";
28
+ async up() {
29
+ const repo = this.db.getRepository("localizationTexts");
30
+ if (!repo) {
31
+ return;
32
+ }
33
+ await repo.update({
34
+ filter: {
35
+ module: "resources.localization-management"
36
+ },
37
+ values: {
38
+ module: "resources.localization"
39
+ }
40
+ });
41
+ }
42
+ }
@@ -0,0 +1,20 @@
1
+ import { Migration } from '@nocobase/server';
2
+ export default class extends Migration {
3
+ on: string;
4
+ appVersion: string;
5
+ namesMp: {
6
+ 'auth-cas': string;
7
+ 'auth-oidc': string;
8
+ 'auth-saml': string;
9
+ 'field-china-region': string;
10
+ 'action-custom-request': string;
11
+ 'action-export': string;
12
+ 'field-formula': string;
13
+ 'block-iframe': string;
14
+ 'action-import': string;
15
+ localization: string;
16
+ 'field-sequence': string;
17
+ 'auth-sms': string;
18
+ };
19
+ up(): Promise<void>;
20
+ }
@@ -0,0 +1,73 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var delete_pkg_name_ns_exports = {};
19
+ __export(delete_pkg_name_ns_exports, {
20
+ default: () => delete_pkg_name_ns_default
21
+ });
22
+ module.exports = __toCommonJS(delete_pkg_name_ns_exports);
23
+ var import_server = require("@nocobase/server");
24
+ class delete_pkg_name_ns_default extends import_server.Migration {
25
+ on = "afterLoad";
26
+ // 'beforeLoad' or 'afterLoad'
27
+ appVersion = "<1.0.0-alpha.1";
28
+ namesMp = {
29
+ "auth-cas": "cas",
30
+ "auth-oidc": "oidc",
31
+ "auth-saml": "saml",
32
+ "field-china-region": "china-region",
33
+ "action-custom-request": "custom-request",
34
+ "action-export": "export",
35
+ "field-formula": "formula-field",
36
+ "block-iframe": "iframe-block",
37
+ "action-import": "import",
38
+ localization: "localization-management",
39
+ "field-sequence": "sequence-field",
40
+ "auth-sms": "sms-auth"
41
+ };
42
+ async up() {
43
+ const resources = await this.app.localeManager.getCacheResources("en-US");
44
+ const modules = Object.keys(resources);
45
+ Object.entries(this.namesMp).forEach(([newName, oldName]) => {
46
+ if (!modules.includes(newName)) {
47
+ return;
48
+ }
49
+ modules.push(oldName, `${import_server.OFFICIAL_PLUGIN_PREFIX}${oldName}`);
50
+ });
51
+ const toBeDeleted = [];
52
+ modules.forEach((module2) => {
53
+ if (!module2.startsWith(import_server.OFFICIAL_PLUGIN_PREFIX)) {
54
+ return;
55
+ }
56
+ const name = module2.replace(import_server.OFFICIAL_PLUGIN_PREFIX, "");
57
+ if (!modules.includes(name)) {
58
+ return;
59
+ }
60
+ toBeDeleted.push(module2);
61
+ });
62
+ if (!toBeDeleted.length) {
63
+ return;
64
+ }
65
+ await this.db.getRepository("localizationTexts").destroy({
66
+ filter: {
67
+ module: {
68
+ $in: toBeDeleted.map((module2) => `resources.${module2}`)
69
+ }
70
+ }
71
+ });
72
+ }
73
+ }
@@ -0,0 +1,15 @@
1
+ import PluginUISchemaStorageServer from '@nocobase/plugin-ui-schema-storage';
2
+ import { InstallOptions, Plugin } from '@nocobase/server';
3
+ import Resources from './resources';
4
+ export declare class PluginLocalizationServer extends Plugin {
5
+ resources: Resources;
6
+ registerUISchemahook(plugin?: PluginUISchemaStorageServer): void;
7
+ afterAdd(): void;
8
+ beforeLoad(): void;
9
+ load(): Promise<void>;
10
+ install(options?: InstallOptions): Promise<void>;
11
+ afterEnable(): Promise<void>;
12
+ afterDisable(): Promise<void>;
13
+ remove(): Promise<void>;
14
+ }
15
+ export default PluginLocalizationServer;
@@ -0,0 +1,168 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var plugin_exports = {};
29
+ __export(plugin_exports, {
30
+ PluginLocalizationServer: () => PluginLocalizationServer,
31
+ default: () => plugin_default
32
+ });
33
+ module.exports = __toCommonJS(plugin_exports);
34
+ var import_server = require("@nocobase/server");
35
+ var import_deepmerge = __toESM(require("deepmerge"));
36
+ var import_path = require("path");
37
+ var import_localization = __toESM(require("./actions/localization"));
38
+ var import_localizationTexts = __toESM(require("./actions/localizationTexts"));
39
+ var import_resources = __toESM(require("./resources"));
40
+ var import_utils = require("./utils");
41
+ var import_constans = require("./constans");
42
+ class PluginLocalizationServer extends import_server.Plugin {
43
+ resources;
44
+ registerUISchemahook(plugin) {
45
+ const uiSchemaStoragePlugin = plugin || this.app.getPlugin("ui-schema-storage");
46
+ if (!uiSchemaStoragePlugin) {
47
+ return;
48
+ }
49
+ uiSchemaStoragePlugin.serverHooks.register("onSelfSave", "extractTextToLocale", async ({ schemaInstance }) => {
50
+ var _a;
51
+ const module2 = `resources.${import_constans.NAMESPACE_MENUS}`;
52
+ const schema = schemaInstance.get("schema");
53
+ const title = (schema == null ? void 0 : schema.title) || ((_a = schema == null ? void 0 : schema["x-component-props"]) == null ? void 0 : _a.title);
54
+ if (!title) {
55
+ return;
56
+ }
57
+ const result = await this.resources.filterExists([{ text: title, module: module2 }]);
58
+ if (!result.length) {
59
+ return;
60
+ }
61
+ this.db.getRepository("localizationTexts").create({
62
+ values: {
63
+ module: module2,
64
+ text: title
65
+ }
66
+ }).then((res) => this.resources.updateCacheTexts([res])).catch((err) => {
67
+ });
68
+ });
69
+ }
70
+ afterAdd() {
71
+ }
72
+ beforeLoad() {
73
+ }
74
+ async load() {
75
+ await this.importCollections((0, import_path.resolve)(__dirname, "collections"));
76
+ this.app.resource({
77
+ name: "localizationTexts",
78
+ actions: import_localizationTexts.default
79
+ });
80
+ this.app.resource({
81
+ name: "localization",
82
+ actions: import_localization.default
83
+ });
84
+ this.app.acl.registerSnippet({
85
+ name: `pm.${this.name}.localization`,
86
+ actions: ["localization:*", "localizationTexts:*", "localizationTranslations:*"]
87
+ });
88
+ this.db.on("afterSave", async (instance, options) => {
89
+ const module2 = `resources.${import_constans.NAMESPACE_COLLECTIONS}`;
90
+ const model = instance.constructor;
91
+ const collection = model.collection;
92
+ if (!collection) {
93
+ return;
94
+ }
95
+ let texts = [];
96
+ const fields = Array.from(collection.fields.values()).filter((field) => {
97
+ var _a;
98
+ return ((_a = field.options) == null ? void 0 : _a.translation) && instance["_changed"].has(field.name);
99
+ }).map((field) => field.name);
100
+ if (!fields.length) {
101
+ return;
102
+ }
103
+ const textsFromDB = (0, import_utils.getTextsFromDBRecord)(fields, instance);
104
+ textsFromDB.forEach((text) => {
105
+ texts.push({ text, module: module2 });
106
+ });
107
+ texts = await this.resources.filterExists(texts, options == null ? void 0 : options.transaction);
108
+ this.db.getModel("localizationTexts").bulkCreate(
109
+ texts.map(({ text, module: module3 }) => ({
110
+ module: module3,
111
+ text
112
+ })),
113
+ {
114
+ transaction: options == null ? void 0 : options.transaction
115
+ }
116
+ ).then((newTexts) => this.resources.updateCacheTexts(newTexts, options == null ? void 0 : options.transaction)).catch((err) => {
117
+ });
118
+ });
119
+ const cache = await this.app.cacheManager.createCache({
120
+ name: "localization",
121
+ prefix: "localization",
122
+ store: "memory"
123
+ });
124
+ this.resources = new import_resources.default(this.db, cache);
125
+ this.registerUISchemahook();
126
+ this.app.resourceManager.use(async (ctx, next) => {
127
+ await next();
128
+ const { resourceName, actionName } = ctx.action.params;
129
+ if (resourceName === "app" && actionName === "getLang") {
130
+ const custom = await this.resources.getResources(ctx.body.lang || "en-US");
131
+ const appLang = ctx.body;
132
+ const resources = { ...appLang.resources };
133
+ Object.keys(custom).forEach((key) => {
134
+ const module2 = key.replace("resources.", "");
135
+ const resource = appLang.resources[module2];
136
+ const customResource = custom[key];
137
+ resources[module2] = resource ? (0, import_deepmerge.default)(resource, customResource) : customResource;
138
+ const pkgName = `${import_server.OFFICIAL_PLUGIN_PREFIX}${module2}`;
139
+ if (appLang.resources[pkgName]) {
140
+ resources[pkgName] = { ...resources[module2] };
141
+ }
142
+ });
143
+ ctx.body = {
144
+ ...appLang,
145
+ resources
146
+ };
147
+ }
148
+ });
149
+ }
150
+ async install(options) {
151
+ }
152
+ async afterEnable() {
153
+ }
154
+ async afterDisable() {
155
+ const uiSchemaStoragePlugin = this.app.getPlugin("ui-schema-storage");
156
+ if (!uiSchemaStoragePlugin) {
157
+ return;
158
+ }
159
+ uiSchemaStoragePlugin.serverHooks.remove("onSelfSave", "extractTextToLocale");
160
+ }
161
+ async remove() {
162
+ }
163
+ }
164
+ var plugin_default = PluginLocalizationServer;
165
+ // Annotate the CommonJS export names for ESM import in node:
166
+ 0 && (module.exports = {
167
+ PluginLocalizationServer
168
+ });
@@ -0,0 +1,19 @@
1
+ import { Cache } from '@nocobase/cache';
2
+ import { Database, Transaction } from '@nocobase/database';
3
+ export default class Resources {
4
+ cache: Cache;
5
+ db: Database;
6
+ constructor(db: Database, cache: Cache);
7
+ getTexts(transaction?: Transaction): Promise<any>;
8
+ getTranslations(locale: string): Promise<any>;
9
+ getResources(locale: string): Promise<{}>;
10
+ filterExists(texts: {
11
+ text: string;
12
+ module: string;
13
+ }[], transaction?: Transaction): Promise<{
14
+ text: string;
15
+ module: string;
16
+ }[]>;
17
+ updateCacheTexts(texts: any[], transaction?: Transaction): Promise<void>;
18
+ resetCache(locale: string): Promise<void>;
19
+ }
@@ -0,0 +1,86 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var resources_exports = {};
19
+ __export(resources_exports, {
20
+ default: () => Resources
21
+ });
22
+ module.exports = __toCommonJS(resources_exports);
23
+ class Resources {
24
+ cache;
25
+ db;
26
+ constructor(db, cache) {
27
+ this.cache = cache;
28
+ this.db = db;
29
+ }
30
+ async getTexts(transaction) {
31
+ return await this.cache.wrap(`texts`, async () => {
32
+ return await this.db.getRepository("localizationTexts").find({
33
+ fields: ["id", "module", "text"],
34
+ raw: true,
35
+ transaction
36
+ });
37
+ });
38
+ }
39
+ async getTranslations(locale) {
40
+ return await this.cache.wrap(`translations:${locale}`, async () => {
41
+ return await this.db.getRepository("localizationTranslations").find({
42
+ fields: ["textId", "translation"],
43
+ filter: { locale },
44
+ raw: true
45
+ });
46
+ });
47
+ }
48
+ async getResources(locale) {
49
+ const [texts, translations] = await Promise.all([this.getTexts(), this.getTranslations(locale)]);
50
+ const resources = {};
51
+ const textsMap = texts.reduce((map, item) => {
52
+ map[item.id] = item;
53
+ return map;
54
+ }, {});
55
+ translations.forEach((item) => {
56
+ const text = textsMap[item.textId];
57
+ if (!text) {
58
+ return;
59
+ }
60
+ const module2 = text.module;
61
+ if (!resources[module2]) {
62
+ resources[module2] = {};
63
+ }
64
+ resources[module2][text.text] = item.translation;
65
+ });
66
+ return resources;
67
+ }
68
+ async filterExists(texts, transaction) {
69
+ const existTexts = await this.getTexts(transaction);
70
+ return texts.filter((text) => {
71
+ return !existTexts.find((item) => item.text === text.text && item.module === text.module);
72
+ });
73
+ }
74
+ async updateCacheTexts(texts, transaction) {
75
+ const newTexts = texts.map((text) => ({
76
+ id: text.id,
77
+ module: text.module,
78
+ text: text.text
79
+ }));
80
+ const existTexts = await this.getTexts(transaction);
81
+ await this.cache.set(`texts`, [...existTexts, ...newTexts]);
82
+ }
83
+ async resetCache(locale) {
84
+ await this.cache.del(`translations:${locale}`);
85
+ }
86
+ }
@@ -0,0 +1,3 @@
1
+ export declare const compile: (title: string) => string;
2
+ export declare const getTextsFromUISchema: (schema: any) => any[];
3
+ export declare const getTextsFromDBRecord: (fields: string[], record: any) => any[];
@@ -0,0 +1,80 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var utils_exports = {};
19
+ __export(utils_exports, {
20
+ compile: () => compile,
21
+ getTextsFromDBRecord: () => getTextsFromDBRecord,
22
+ getTextsFromUISchema: () => getTextsFromUISchema
23
+ });
24
+ module.exports = __toCommonJS(utils_exports);
25
+ const compile = (title) => (title || "").replace(/{{\s*t\(["|'|`](.*)["|'|`]\)\s*}}/g, "$1");
26
+ /* istanbul ignore next -- @preserve */
27
+ const getTextsFromUISchema = (schema) => {
28
+ var _a, _b, _c, _d;
29
+ const texts = [];
30
+ const title = compile(schema.title);
31
+ const componentPropsTitle = compile((_a = schema["x-component-props"]) == null ? void 0 : _a.title);
32
+ const decoratorPropsTitle = compile((_b = schema["x-decorator-props"]) == null ? void 0 : _b.title);
33
+ if (title) {
34
+ texts.push(title);
35
+ }
36
+ if (componentPropsTitle) {
37
+ texts.push(componentPropsTitle);
38
+ }
39
+ if (decoratorPropsTitle) {
40
+ texts.push(decoratorPropsTitle);
41
+ }
42
+ if ((_d = (_c = schema["x-data-templates"]) == null ? void 0 : _c.items) == null ? void 0 : _d.length) {
43
+ schema["x-data-templates"].items.forEach((item) => {
44
+ const title2 = compile(item.title);
45
+ if (title2) {
46
+ texts.push(title2);
47
+ }
48
+ });
49
+ }
50
+ return texts;
51
+ };
52
+ const getTextsFromDBRecord = (fields, record) => {
53
+ const texts = [];
54
+ fields.forEach((field) => {
55
+ var _a, _b;
56
+ const value = record[field];
57
+ if (typeof value === "string") {
58
+ texts.push(compile(value));
59
+ }
60
+ if (typeof value === "object") {
61
+ if ((_a = value == null ? void 0 : value.uiSchema) == null ? void 0 : _a.title) {
62
+ texts.push(compile(value.uiSchema.title));
63
+ }
64
+ if ((_b = value == null ? void 0 : value.uiSchema) == null ? void 0 : _b.enum) {
65
+ value.uiSchema.enum.forEach((item) => {
66
+ if (item == null ? void 0 : item.label) {
67
+ texts.push(compile(item.label));
68
+ }
69
+ });
70
+ }
71
+ }
72
+ });
73
+ return texts;
74
+ };
75
+ // Annotate the CommonJS export names for ESM import in node:
76
+ 0 && (module.exports = {
77
+ compile,
78
+ getTextsFromDBRecord,
79
+ getTextsFromUISchema
80
+ });