@strapi/i18n 0.0.0-experimental.c592deb623aed3f74ef7fdacfad9757ed59d34f7 → 0.0.0-experimental.cb74730ce5154c26404d4dccca14976a22319002
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/_chunks/{SettingsPage-CnBFTsrq.mjs → SettingsPage-BAx9nmep.mjs} +4 -4
- package/dist/_chunks/SettingsPage-BAx9nmep.mjs.map +1 -0
- package/dist/_chunks/{SettingsPage-CJOMVQv5.js → SettingsPage-BTgjb2KS.js} +5 -6
- package/dist/_chunks/SettingsPage-BTgjb2KS.js.map +1 -0
- package/dist/_chunks/{en-Dk9At9_Z.js → en-BKBz3tro.js} +3 -3
- package/dist/_chunks/en-BKBz3tro.js.map +1 -0
- package/dist/_chunks/{en-BYRZFDBV.mjs → en-DlXfy6Gy.mjs} +3 -3
- package/dist/_chunks/en-DlXfy6Gy.mjs.map +1 -0
- package/dist/_chunks/{index-C5SImSYG.js → index-3yyF237r.js} +28 -15
- package/dist/_chunks/index-3yyF237r.js.map +1 -0
- package/dist/_chunks/{index-BFk3nfTb.mjs → index-B0NijiBB.mjs} +27 -13
- package/dist/_chunks/index-B0NijiBB.mjs.map +1 -0
- package/dist/admin/index.js +1 -1
- package/dist/admin/index.mjs +1 -1
- package/dist/admin/src/components/CMHeaderActions.d.ts +1 -1
- package/dist/admin/src/components/CreateLocale.d.ts +6 -6
- package/dist/server/index.js +390 -422
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +391 -422
- package/dist/server/index.mjs.map +1 -1
- package/dist/server/src/index.d.ts +6 -0
- package/dist/server/src/index.d.ts.map +1 -1
- package/dist/server/src/register.d.ts.map +1 -1
- package/dist/server/src/services/index.d.ts +6 -0
- package/dist/server/src/services/index.d.ts.map +1 -1
- package/dist/server/src/services/sanitize/index.d.ts +11 -0
- package/dist/server/src/services/sanitize/index.d.ts.map +1 -0
- package/dist/server/src/utils/index.d.ts +2 -0
- package/dist/server/src/utils/index.d.ts.map +1 -1
- package/package.json +13 -13
- package/dist/_chunks/SettingsPage-CJOMVQv5.js.map +0 -1
- package/dist/_chunks/SettingsPage-CnBFTsrq.mjs.map +0 -1
- package/dist/_chunks/en-BYRZFDBV.mjs.map +0 -1
- package/dist/_chunks/en-Dk9At9_Z.js.map +0 -1
- package/dist/_chunks/index-BFk3nfTb.mjs.map +0 -1
- package/dist/_chunks/index-C5SImSYG.js.map +0 -1
- package/dist/server/src/migrations/content-type/disable/index.d.ts +0 -3
- package/dist/server/src/migrations/content-type/disable/index.d.ts.map +0 -1
- package/dist/server/src/migrations/content-type/enable/index.d.ts +0 -3
- package/dist/server/src/migrations/content-type/enable/index.d.ts.map +0 -1
package/dist/server/index.js
CHANGED
@@ -4,8 +4,7 @@ const fp = require("lodash/fp");
|
|
4
4
|
const utils = require("@strapi/utils");
|
5
5
|
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
|
6
6
|
function _interopNamespace(e) {
|
7
|
-
if (e && e.__esModule)
|
8
|
-
return e;
|
7
|
+
if (e && e.__esModule) return e;
|
9
8
|
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
10
9
|
if (e) {
|
11
10
|
for (const k in e) {
|
@@ -195,6 +194,352 @@ const getI18nLocaleArgPlugin = ({ nexus, typeRegistry }) => {
|
|
195
194
|
}
|
196
195
|
});
|
197
196
|
};
|
197
|
+
const register = ({ strapi: strapi2 }) => {
|
198
|
+
extendContentTypes(strapi2);
|
199
|
+
addContentManagerLocaleMiddleware(strapi2);
|
200
|
+
};
|
201
|
+
const addContentManagerLocaleMiddleware = (strapi2) => {
|
202
|
+
strapi2.server.router.use("/content-manager/collection-types/:model", (ctx, next) => {
|
203
|
+
if (ctx.method === "POST" || ctx.method === "PUT") {
|
204
|
+
return validateLocaleCreation(ctx, next);
|
205
|
+
}
|
206
|
+
return next();
|
207
|
+
});
|
208
|
+
strapi2.server.router.use("/content-manager/single-types/:model", (ctx, next) => {
|
209
|
+
if (ctx.method === "POST" || ctx.method === "PUT") {
|
210
|
+
return validateLocaleCreation(ctx, next);
|
211
|
+
}
|
212
|
+
return next();
|
213
|
+
});
|
214
|
+
};
|
215
|
+
const extendContentTypes = (strapi2) => {
|
216
|
+
const { isLocalizedContentType: isLocalizedContentType2 } = getService("content-types");
|
217
|
+
Object.values(strapi2.contentTypes).forEach((contentType) => {
|
218
|
+
const { attributes: attributes2 } = contentType;
|
219
|
+
const isLocalized = isLocalizedContentType2(contentType);
|
220
|
+
___default.default.set(attributes2, "locale", {
|
221
|
+
writable: true,
|
222
|
+
private: !isLocalized,
|
223
|
+
configurable: false,
|
224
|
+
visible: false,
|
225
|
+
type: "string"
|
226
|
+
});
|
227
|
+
___default.default.set(attributes2, "localizations", {
|
228
|
+
type: "relation",
|
229
|
+
relation: "oneToMany",
|
230
|
+
target: contentType.uid,
|
231
|
+
writable: false,
|
232
|
+
private: !isLocalized,
|
233
|
+
configurable: false,
|
234
|
+
visible: false,
|
235
|
+
unstable_virtual: true,
|
236
|
+
joinColumn: {
|
237
|
+
name: "document_id",
|
238
|
+
referencedColumn: "document_id",
|
239
|
+
referencedTable: strapi2.db.metadata.identifiers.getTableName(contentType.collectionName),
|
240
|
+
// ensure the population will not include the results we already loaded
|
241
|
+
on({ results }) {
|
242
|
+
return {
|
243
|
+
id: {
|
244
|
+
$notIn: results.map((r) => r.id)
|
245
|
+
}
|
246
|
+
};
|
247
|
+
}
|
248
|
+
}
|
249
|
+
});
|
250
|
+
});
|
251
|
+
if (strapi2.plugin("graphql")) {
|
252
|
+
graphqlProvider({ strapi: strapi2 }).register();
|
253
|
+
}
|
254
|
+
};
|
255
|
+
const info = {
|
256
|
+
singularName: "locale",
|
257
|
+
pluralName: "locales",
|
258
|
+
collectionName: "locales",
|
259
|
+
displayName: "Locale",
|
260
|
+
description: ""
|
261
|
+
};
|
262
|
+
const options = {};
|
263
|
+
const pluginOptions = {
|
264
|
+
"content-manager": {
|
265
|
+
visible: false
|
266
|
+
},
|
267
|
+
"content-type-builder": {
|
268
|
+
visible: false
|
269
|
+
}
|
270
|
+
};
|
271
|
+
const attributes = {
|
272
|
+
name: {
|
273
|
+
type: "string",
|
274
|
+
min: 1,
|
275
|
+
max: 50,
|
276
|
+
configurable: false
|
277
|
+
},
|
278
|
+
code: {
|
279
|
+
type: "string",
|
280
|
+
unique: true,
|
281
|
+
configurable: false
|
282
|
+
}
|
283
|
+
};
|
284
|
+
const schema = {
|
285
|
+
info,
|
286
|
+
options,
|
287
|
+
pluginOptions,
|
288
|
+
attributes
|
289
|
+
};
|
290
|
+
const locale = {
|
291
|
+
schema
|
292
|
+
};
|
293
|
+
const contentTypes$1 = {
|
294
|
+
locale
|
295
|
+
};
|
296
|
+
const actions = [
|
297
|
+
{
|
298
|
+
section: "settings",
|
299
|
+
category: "Internationalization",
|
300
|
+
subCategory: "Locales",
|
301
|
+
pluginName: "i18n",
|
302
|
+
displayName: "Create",
|
303
|
+
uid: "locale.create"
|
304
|
+
},
|
305
|
+
{
|
306
|
+
section: "settings",
|
307
|
+
category: "Internationalization",
|
308
|
+
subCategory: "Locales",
|
309
|
+
pluginName: "i18n",
|
310
|
+
displayName: "Read",
|
311
|
+
uid: "locale.read",
|
312
|
+
aliases: [
|
313
|
+
{ actionId: "plugin::content-manager.explorer.read", subjects: ["plugin::i18n.locale"] }
|
314
|
+
]
|
315
|
+
},
|
316
|
+
{
|
317
|
+
section: "settings",
|
318
|
+
category: "Internationalization",
|
319
|
+
subCategory: "Locales",
|
320
|
+
pluginName: "i18n",
|
321
|
+
displayName: "Update",
|
322
|
+
uid: "locale.update"
|
323
|
+
},
|
324
|
+
{
|
325
|
+
section: "settings",
|
326
|
+
category: "Internationalization",
|
327
|
+
subCategory: "Locales",
|
328
|
+
pluginName: "i18n",
|
329
|
+
displayName: "Delete",
|
330
|
+
uid: "locale.delete"
|
331
|
+
}
|
332
|
+
];
|
333
|
+
const addLocalesPropertyIfNeeded = ({ value: action }) => {
|
334
|
+
const {
|
335
|
+
section,
|
336
|
+
options: { applyToProperties }
|
337
|
+
} = action;
|
338
|
+
if (section !== "contentTypes") {
|
339
|
+
return;
|
340
|
+
}
|
341
|
+
if (fp.isArray(applyToProperties) && applyToProperties.includes("locales")) {
|
342
|
+
return;
|
343
|
+
}
|
344
|
+
action.options.applyToProperties = fp.isArray(applyToProperties) ? applyToProperties.concat("locales") : ["locales"];
|
345
|
+
};
|
346
|
+
const shouldApplyLocalesPropertyToSubject = ({ property, subject }) => {
|
347
|
+
if (property === "locales") {
|
348
|
+
const model = strapi.getModel(subject);
|
349
|
+
return getService("content-types").isLocalizedContentType(model);
|
350
|
+
}
|
351
|
+
return true;
|
352
|
+
};
|
353
|
+
const addAllLocalesToPermissions = async (permissions2) => {
|
354
|
+
const { actionProvider } = strapi.service("admin::permission");
|
355
|
+
const { find: findAllLocales } = getService("locales");
|
356
|
+
const allLocales = await findAllLocales();
|
357
|
+
const allLocalesCode = allLocales.map(fp.prop("code"));
|
358
|
+
return Promise.all(
|
359
|
+
permissions2.map(async (permission) => {
|
360
|
+
const { action, subject } = permission;
|
361
|
+
const appliesToLocalesProperty = await actionProvider.appliesToProperty(
|
362
|
+
"locales",
|
363
|
+
action,
|
364
|
+
subject
|
365
|
+
);
|
366
|
+
if (!appliesToLocalesProperty) {
|
367
|
+
return permission;
|
368
|
+
}
|
369
|
+
const oldPermissionProperties = fp.getOr({}, "properties", permission);
|
370
|
+
return { ...permission, properties: { ...oldPermissionProperties, locales: allLocalesCode } };
|
371
|
+
})
|
372
|
+
);
|
373
|
+
};
|
374
|
+
const syncSuperAdminPermissionsWithLocales = async () => {
|
375
|
+
const roleService = strapi.service("admin::role");
|
376
|
+
const permissionService = strapi.service("admin::permission");
|
377
|
+
const superAdminRole = await roleService.getSuperAdmin();
|
378
|
+
if (!superAdminRole) {
|
379
|
+
return;
|
380
|
+
}
|
381
|
+
const superAdminPermissions = await permissionService.findMany({
|
382
|
+
where: {
|
383
|
+
role: {
|
384
|
+
id: superAdminRole.id
|
385
|
+
}
|
386
|
+
}
|
387
|
+
});
|
388
|
+
const newSuperAdminPermissions = await addAllLocalesToPermissions(superAdminPermissions);
|
389
|
+
await roleService.assignPermissions(superAdminRole.id, newSuperAdminPermissions);
|
390
|
+
};
|
391
|
+
const registerI18nActions = async () => {
|
392
|
+
const { actionProvider } = strapi.service("admin::permission");
|
393
|
+
await actionProvider.registerMany(actions);
|
394
|
+
};
|
395
|
+
const registerI18nActionsHooks = () => {
|
396
|
+
const { actionProvider } = strapi.service("admin::permission");
|
397
|
+
const { hooks } = strapi.service("admin::role");
|
398
|
+
actionProvider.hooks.appliesPropertyToSubject.register(shouldApplyLocalesPropertyToSubject);
|
399
|
+
hooks.willResetSuperAdminPermissions.register(addAllLocalesToPermissions);
|
400
|
+
};
|
401
|
+
const updateActionsProperties = () => {
|
402
|
+
const { actionProvider } = strapi.service("admin::permission");
|
403
|
+
actionProvider.hooks.willRegister.register(addLocalesPropertyIfNeeded);
|
404
|
+
actionProvider.values().forEach((action) => addLocalesPropertyIfNeeded({ value: action }));
|
405
|
+
};
|
406
|
+
const i18nActionsService = {
|
407
|
+
actions,
|
408
|
+
registerI18nActions,
|
409
|
+
registerI18nActionsHooks,
|
410
|
+
updateActionsProperties,
|
411
|
+
syncSuperAdminPermissionsWithLocales
|
412
|
+
};
|
413
|
+
const localesPropertyHandler = async ({ action, section }) => {
|
414
|
+
const { actionProvider } = strapi.service("admin::permission");
|
415
|
+
const locales2 = await getService("locales").find();
|
416
|
+
if (fp.isEmpty(locales2)) {
|
417
|
+
return;
|
418
|
+
}
|
419
|
+
for (const subject of section.subjects) {
|
420
|
+
const applies = await actionProvider.appliesToProperty("locales", action.actionId, subject.uid);
|
421
|
+
const hasLocalesProperty = subject.properties.find(
|
422
|
+
(property) => property.value === "locales"
|
423
|
+
);
|
424
|
+
if (applies && !hasLocalesProperty) {
|
425
|
+
subject.properties.push({
|
426
|
+
label: "Locales",
|
427
|
+
value: "locales",
|
428
|
+
children: locales2.map(({ name, code }) => ({ label: name || code, value: code }))
|
429
|
+
});
|
430
|
+
}
|
431
|
+
}
|
432
|
+
};
|
433
|
+
const registerLocalesPropertyHandler = () => {
|
434
|
+
const { sectionsBuilder } = strapi.service("admin::permission");
|
435
|
+
sectionsBuilder.addHandler("singleTypes", localesPropertyHandler);
|
436
|
+
sectionsBuilder.addHandler("collectionTypes", localesPropertyHandler);
|
437
|
+
};
|
438
|
+
const sectionsBuilderService = {
|
439
|
+
localesPropertyHandler,
|
440
|
+
registerLocalesPropertyHandler
|
441
|
+
};
|
442
|
+
const willRegisterPermission = (context) => {
|
443
|
+
const { permission, condition, user } = context;
|
444
|
+
const { subject, properties } = permission;
|
445
|
+
const isSuperAdmin = strapi.service("admin::role").hasSuperAdminRole(user);
|
446
|
+
if (isSuperAdmin) {
|
447
|
+
return;
|
448
|
+
}
|
449
|
+
const { locales: locales2 } = properties || {};
|
450
|
+
const { isLocalizedContentType: isLocalizedContentType2 } = getService("content-types");
|
451
|
+
if (!subject) {
|
452
|
+
return;
|
453
|
+
}
|
454
|
+
const ct = strapi.contentTypes[subject];
|
455
|
+
if (!isLocalizedContentType2(ct)) {
|
456
|
+
return;
|
457
|
+
}
|
458
|
+
if (locales2 === null) {
|
459
|
+
return;
|
460
|
+
}
|
461
|
+
condition.and({
|
462
|
+
locale: {
|
463
|
+
$in: locales2 || []
|
464
|
+
}
|
465
|
+
});
|
466
|
+
};
|
467
|
+
const registerI18nPermissionsHandlers = () => {
|
468
|
+
const { engine } = strapi.service("admin::permission");
|
469
|
+
engine.hooks["before-register.permission"].register(willRegisterPermission);
|
470
|
+
};
|
471
|
+
const engineService = {
|
472
|
+
willRegisterPermission,
|
473
|
+
registerI18nPermissionsHandlers
|
474
|
+
};
|
475
|
+
const permissions = () => ({
|
476
|
+
actions: i18nActionsService,
|
477
|
+
sectionsBuilder: sectionsBuilderService,
|
478
|
+
engine: engineService
|
479
|
+
});
|
480
|
+
const sendDidInitializeEvent = async () => {
|
481
|
+
const { isLocalizedContentType: isLocalizedContentType2 } = getService("content-types");
|
482
|
+
const numberOfContentTypes = fp.reduce(
|
483
|
+
(sum, contentType) => isLocalizedContentType2(contentType) ? sum + 1 : sum,
|
484
|
+
0
|
485
|
+
)(strapi.contentTypes);
|
486
|
+
await strapi.telemetry.send("didInitializeI18n", { groupProperties: { numberOfContentTypes } });
|
487
|
+
};
|
488
|
+
const sendDidUpdateI18nLocalesEvent = async () => {
|
489
|
+
const numberOfLocales = await getService("locales").count();
|
490
|
+
await strapi.telemetry.send("didUpdateI18nLocales", {
|
491
|
+
groupProperties: { numberOfLocales }
|
492
|
+
});
|
493
|
+
};
|
494
|
+
const metrics = () => ({
|
495
|
+
sendDidInitializeEvent,
|
496
|
+
sendDidUpdateI18nLocalesEvent
|
497
|
+
});
|
498
|
+
const syncNonLocalizedAttributes = async (sourceEntry, model) => {
|
499
|
+
const { copyNonLocalizedAttributes: copyNonLocalizedAttributes2 } = getService("content-types");
|
500
|
+
const nonLocalizedAttributes = copyNonLocalizedAttributes2(model, sourceEntry);
|
501
|
+
if (fp.isEmpty(nonLocalizedAttributes)) {
|
502
|
+
return;
|
503
|
+
}
|
504
|
+
const uid = model.uid;
|
505
|
+
const documentId = sourceEntry.documentId;
|
506
|
+
const locale2 = sourceEntry.locale;
|
507
|
+
const status = sourceEntry?.publishedAt ? "published" : "draft";
|
508
|
+
const localeEntriesToUpdate = await strapi.db.query(uid).findMany({
|
509
|
+
where: {
|
510
|
+
documentId,
|
511
|
+
publishedAt: status === "published" ? { $ne: null } : null,
|
512
|
+
locale: { $ne: locale2 }
|
513
|
+
},
|
514
|
+
select: ["locale", "id"]
|
515
|
+
});
|
516
|
+
const entryData = await strapi.documents(uid).omitComponentData(nonLocalizedAttributes);
|
517
|
+
await utils.async.map(localeEntriesToUpdate, async (entry) => {
|
518
|
+
const transformedData = await strapi.documents.utils.transformData(
|
519
|
+
fp.cloneDeep(nonLocalizedAttributes),
|
520
|
+
{
|
521
|
+
uid,
|
522
|
+
status,
|
523
|
+
locale: entry.locale,
|
524
|
+
allowMissingId: true
|
525
|
+
}
|
526
|
+
);
|
527
|
+
const componentData = await strapi.documents(uid).updateComponents(entry, transformedData);
|
528
|
+
await strapi.db.query(uid).update({
|
529
|
+
where: {
|
530
|
+
documentId,
|
531
|
+
publishedAt: status === "published" ? { $ne: null } : null,
|
532
|
+
locale: { $eq: entry.locale }
|
533
|
+
},
|
534
|
+
// The data we send to the update function is the entry data merged with
|
535
|
+
// the updated component data
|
536
|
+
data: Object.assign(fp.cloneDeep(entryData), componentData)
|
537
|
+
});
|
538
|
+
});
|
539
|
+
};
|
540
|
+
const localizations = () => ({
|
541
|
+
syncNonLocalizedAttributes
|
542
|
+
});
|
198
543
|
const isoLocales = [
|
199
544
|
{
|
200
545
|
code: "af",
|
@@ -2588,438 +2933,40 @@ const isoLocales = [
|
|
2588
2933
|
code: "yo-NG",
|
2589
2934
|
name: "Yoruba (Nigeria) (yo-NG)"
|
2590
2935
|
},
|
2591
|
-
{
|
2592
|
-
code: "dje",
|
2593
|
-
name: "Zarma (dje)"
|
2594
|
-
},
|
2595
|
-
{
|
2596
|
-
code: "dje-NE",
|
2597
|
-
name: "Zarma (Niger) (dje-NE)"
|
2598
|
-
},
|
2599
|
-
{
|
2600
|
-
code: "zu",
|
2601
|
-
name: "Zulu (zu)"
|
2602
|
-
},
|
2603
|
-
{
|
2604
|
-
code: "zu-ZA",
|
2605
|
-
name: "Zulu (South Africa) (zu-ZA)"
|
2606
|
-
}
|
2607
|
-
];
|
2608
|
-
const getInitLocale = () => {
|
2609
|
-
const envLocaleCode = process.env.STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE;
|
2610
|
-
if (envLocaleCode) {
|
2611
|
-
const matchingLocale = isoLocales.find(({ code }) => code === envLocaleCode);
|
2612
|
-
if (!matchingLocale) {
|
2613
|
-
throw new Error(
|
2614
|
-
"Unknown locale code provided in the environment variable STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE"
|
2615
|
-
);
|
2616
|
-
}
|
2617
|
-
return { ...matchingLocale };
|
2618
|
-
}
|
2619
|
-
return {
|
2620
|
-
code: "en",
|
2621
|
-
name: "English (en)"
|
2622
|
-
};
|
2623
|
-
};
|
2624
|
-
const DEFAULT_LOCALE = getInitLocale();
|
2625
|
-
const enableContentType = async ({ oldContentTypes, contentTypes: contentTypes2 }) => {
|
2626
|
-
const { isLocalizedContentType: isLocalizedContentType2 } = getService("content-types");
|
2627
|
-
const { getDefaultLocale: getDefaultLocale2 } = getService("locales");
|
2628
|
-
if (!oldContentTypes) {
|
2629
|
-
return;
|
2630
|
-
}
|
2631
|
-
for (const uid in contentTypes2) {
|
2632
|
-
if (!oldContentTypes[uid]) {
|
2633
|
-
continue;
|
2634
|
-
}
|
2635
|
-
const oldContentType = oldContentTypes[uid];
|
2636
|
-
const contentType = contentTypes2[uid];
|
2637
|
-
if (!isLocalizedContentType2(oldContentType) && isLocalizedContentType2(contentType)) {
|
2638
|
-
const defaultLocale = await getDefaultLocale2() || DEFAULT_LOCALE.code;
|
2639
|
-
await strapi.db.query(uid).updateMany({
|
2640
|
-
where: { locale: null },
|
2641
|
-
data: { locale: defaultLocale }
|
2642
|
-
});
|
2643
|
-
}
|
2644
|
-
}
|
2645
|
-
};
|
2646
|
-
const disableContentType = async ({ oldContentTypes, contentTypes: contentTypes2 }) => {
|
2647
|
-
const { isLocalizedContentType: isLocalizedContentType2 } = getService("content-types");
|
2648
|
-
const { getDefaultLocale: getDefaultLocale2 } = getService("locales");
|
2649
|
-
if (!oldContentTypes) {
|
2650
|
-
return;
|
2651
|
-
}
|
2652
|
-
for (const uid in contentTypes2) {
|
2653
|
-
if (!oldContentTypes[uid]) {
|
2654
|
-
continue;
|
2655
|
-
}
|
2656
|
-
const oldContentType = oldContentTypes[uid];
|
2657
|
-
const contentType = contentTypes2[uid];
|
2658
|
-
if (isLocalizedContentType2(oldContentType) && !isLocalizedContentType2(contentType)) {
|
2659
|
-
const defaultLocale = await getDefaultLocale2() || DEFAULT_LOCALE.code;
|
2660
|
-
await Promise.all([
|
2661
|
-
// Delete all entities that are not in the default locale
|
2662
|
-
strapi.db.query(uid).deleteMany({
|
2663
|
-
where: { locale: { $ne: defaultLocale } }
|
2664
|
-
}),
|
2665
|
-
// Set locale to null for the rest
|
2666
|
-
strapi.db.query(uid).updateMany({
|
2667
|
-
where: { locale: { $eq: defaultLocale } },
|
2668
|
-
data: { locale: null }
|
2669
|
-
})
|
2670
|
-
]);
|
2671
|
-
}
|
2672
|
-
}
|
2673
|
-
};
|
2674
|
-
const register = ({ strapi: strapi2 }) => {
|
2675
|
-
extendContentTypes(strapi2);
|
2676
|
-
addContentManagerLocaleMiddleware(strapi2);
|
2677
|
-
addContentTypeSyncHooks(strapi2);
|
2678
|
-
};
|
2679
|
-
const addContentManagerLocaleMiddleware = (strapi2) => {
|
2680
|
-
strapi2.server.router.use("/content-manager/collection-types/:model", (ctx, next) => {
|
2681
|
-
if (ctx.method === "POST" || ctx.method === "PUT") {
|
2682
|
-
return validateLocaleCreation(ctx, next);
|
2683
|
-
}
|
2684
|
-
return next();
|
2685
|
-
});
|
2686
|
-
strapi2.server.router.use("/content-manager/single-types/:model", (ctx, next) => {
|
2687
|
-
if (ctx.method === "POST" || ctx.method === "PUT") {
|
2688
|
-
return validateLocaleCreation(ctx, next);
|
2689
|
-
}
|
2690
|
-
return next();
|
2691
|
-
});
|
2692
|
-
};
|
2693
|
-
const addContentTypeSyncHooks = (strapi2) => {
|
2694
|
-
strapi2.hook("strapi::content-types.beforeSync").register(disableContentType);
|
2695
|
-
strapi2.hook("strapi::content-types.afterSync").register(enableContentType);
|
2696
|
-
};
|
2697
|
-
const extendContentTypes = (strapi2) => {
|
2698
|
-
Object.values(strapi2.contentTypes).forEach((contentType) => {
|
2699
|
-
const { attributes: attributes2 } = contentType;
|
2700
|
-
___default.default.set(attributes2, "locale", {
|
2701
|
-
writable: true,
|
2702
|
-
private: false,
|
2703
|
-
configurable: false,
|
2704
|
-
visible: false,
|
2705
|
-
type: "string"
|
2706
|
-
});
|
2707
|
-
___default.default.set(attributes2, "localizations", {
|
2708
|
-
type: "relation",
|
2709
|
-
relation: "oneToMany",
|
2710
|
-
target: contentType.uid,
|
2711
|
-
writable: false,
|
2712
|
-
private: false,
|
2713
|
-
configurable: false,
|
2714
|
-
visible: false,
|
2715
|
-
unstable_virtual: true,
|
2716
|
-
joinColumn: {
|
2717
|
-
name: "document_id",
|
2718
|
-
referencedColumn: "document_id",
|
2719
|
-
referencedTable: strapi2.db.metadata.identifiers.getTableName(contentType.collectionName),
|
2720
|
-
// ensure the population will not include the results we already loaded
|
2721
|
-
on({ results }) {
|
2722
|
-
return {
|
2723
|
-
id: {
|
2724
|
-
$notIn: results.map((r) => r.id)
|
2725
|
-
}
|
2726
|
-
};
|
2727
|
-
}
|
2728
|
-
}
|
2729
|
-
});
|
2730
|
-
});
|
2731
|
-
if (strapi2.plugin("graphql")) {
|
2732
|
-
graphqlProvider({ strapi: strapi2 }).register();
|
2733
|
-
}
|
2734
|
-
};
|
2735
|
-
const info = {
|
2736
|
-
singularName: "locale",
|
2737
|
-
pluralName: "locales",
|
2738
|
-
collectionName: "locales",
|
2739
|
-
displayName: "Locale",
|
2740
|
-
description: ""
|
2741
|
-
};
|
2742
|
-
const options = {};
|
2743
|
-
const pluginOptions = {
|
2744
|
-
"content-manager": {
|
2745
|
-
visible: false
|
2746
|
-
},
|
2747
|
-
"content-type-builder": {
|
2748
|
-
visible: false
|
2749
|
-
}
|
2750
|
-
};
|
2751
|
-
const attributes = {
|
2752
|
-
name: {
|
2753
|
-
type: "string",
|
2754
|
-
min: 1,
|
2755
|
-
max: 50,
|
2756
|
-
configurable: false
|
2757
|
-
},
|
2758
|
-
code: {
|
2759
|
-
type: "string",
|
2760
|
-
unique: true,
|
2761
|
-
configurable: false
|
2762
|
-
}
|
2763
|
-
};
|
2764
|
-
const schema = {
|
2765
|
-
info,
|
2766
|
-
options,
|
2767
|
-
pluginOptions,
|
2768
|
-
attributes
|
2769
|
-
};
|
2770
|
-
const locale = {
|
2771
|
-
schema
|
2772
|
-
};
|
2773
|
-
const contentTypes$1 = {
|
2774
|
-
locale
|
2775
|
-
};
|
2776
|
-
const actions = [
|
2777
|
-
{
|
2778
|
-
section: "settings",
|
2779
|
-
category: "Internationalization",
|
2780
|
-
subCategory: "Locales",
|
2781
|
-
pluginName: "i18n",
|
2782
|
-
displayName: "Create",
|
2783
|
-
uid: "locale.create"
|
2936
|
+
{
|
2937
|
+
code: "dje",
|
2938
|
+
name: "Zarma (dje)"
|
2784
2939
|
},
|
2785
2940
|
{
|
2786
|
-
|
2787
|
-
|
2788
|
-
subCategory: "Locales",
|
2789
|
-
pluginName: "i18n",
|
2790
|
-
displayName: "Read",
|
2791
|
-
uid: "locale.read",
|
2792
|
-
aliases: [
|
2793
|
-
{ actionId: "plugin::content-manager.explorer.read", subjects: ["plugin::i18n.locale"] }
|
2794
|
-
]
|
2941
|
+
code: "dje-NE",
|
2942
|
+
name: "Zarma (Niger) (dje-NE)"
|
2795
2943
|
},
|
2796
2944
|
{
|
2797
|
-
|
2798
|
-
|
2799
|
-
subCategory: "Locales",
|
2800
|
-
pluginName: "i18n",
|
2801
|
-
displayName: "Update",
|
2802
|
-
uid: "locale.update"
|
2945
|
+
code: "zu",
|
2946
|
+
name: "Zulu (zu)"
|
2803
2947
|
},
|
2804
2948
|
{
|
2805
|
-
|
2806
|
-
|
2807
|
-
subCategory: "Locales",
|
2808
|
-
pluginName: "i18n",
|
2809
|
-
displayName: "Delete",
|
2810
|
-
uid: "locale.delete"
|
2949
|
+
code: "zu-ZA",
|
2950
|
+
name: "Zulu (South Africa) (zu-ZA)"
|
2811
2951
|
}
|
2812
2952
|
];
|
2813
|
-
const
|
2814
|
-
const
|
2815
|
-
|
2816
|
-
|
2817
|
-
|
2818
|
-
|
2819
|
-
|
2820
|
-
}
|
2821
|
-
if (fp.isArray(applyToProperties) && applyToProperties.includes("locales")) {
|
2822
|
-
return;
|
2823
|
-
}
|
2824
|
-
action.options.applyToProperties = fp.isArray(applyToProperties) ? applyToProperties.concat("locales") : ["locales"];
|
2825
|
-
};
|
2826
|
-
const shouldApplyLocalesPropertyToSubject = ({ property, subject }) => {
|
2827
|
-
if (property === "locales") {
|
2828
|
-
const model = strapi.getModel(subject);
|
2829
|
-
return getService("content-types").isLocalizedContentType(model);
|
2830
|
-
}
|
2831
|
-
return true;
|
2832
|
-
};
|
2833
|
-
const addAllLocalesToPermissions = async (permissions2) => {
|
2834
|
-
const { actionProvider } = strapi.service("admin::permission");
|
2835
|
-
const { find: findAllLocales } = getService("locales");
|
2836
|
-
const allLocales = await findAllLocales();
|
2837
|
-
const allLocalesCode = allLocales.map(fp.prop("code"));
|
2838
|
-
return Promise.all(
|
2839
|
-
permissions2.map(async (permission) => {
|
2840
|
-
const { action, subject } = permission;
|
2841
|
-
const appliesToLocalesProperty = await actionProvider.appliesToProperty(
|
2842
|
-
"locales",
|
2843
|
-
action,
|
2844
|
-
subject
|
2953
|
+
const getInitLocale = () => {
|
2954
|
+
const envLocaleCode = process.env.STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE;
|
2955
|
+
if (envLocaleCode) {
|
2956
|
+
const matchingLocale = isoLocales.find(({ code }) => code === envLocaleCode);
|
2957
|
+
if (!matchingLocale) {
|
2958
|
+
throw new Error(
|
2959
|
+
"Unknown locale code provided in the environment variable STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE"
|
2845
2960
|
);
|
2846
|
-
if (!appliesToLocalesProperty) {
|
2847
|
-
return permission;
|
2848
|
-
}
|
2849
|
-
const oldPermissionProperties = fp.getOr({}, "properties", permission);
|
2850
|
-
return { ...permission, properties: { ...oldPermissionProperties, locales: allLocalesCode } };
|
2851
|
-
})
|
2852
|
-
);
|
2853
|
-
};
|
2854
|
-
const syncSuperAdminPermissionsWithLocales = async () => {
|
2855
|
-
const roleService = strapi.service("admin::role");
|
2856
|
-
const permissionService = strapi.service("admin::permission");
|
2857
|
-
const superAdminRole = await roleService.getSuperAdmin();
|
2858
|
-
if (!superAdminRole) {
|
2859
|
-
return;
|
2860
|
-
}
|
2861
|
-
const superAdminPermissions = await permissionService.findMany({
|
2862
|
-
where: {
|
2863
|
-
role: {
|
2864
|
-
id: superAdminRole.id
|
2865
|
-
}
|
2866
|
-
}
|
2867
|
-
});
|
2868
|
-
const newSuperAdminPermissions = await addAllLocalesToPermissions(superAdminPermissions);
|
2869
|
-
await roleService.assignPermissions(superAdminRole.id, newSuperAdminPermissions);
|
2870
|
-
};
|
2871
|
-
const registerI18nActions = async () => {
|
2872
|
-
const { actionProvider } = strapi.service("admin::permission");
|
2873
|
-
await actionProvider.registerMany(actions);
|
2874
|
-
};
|
2875
|
-
const registerI18nActionsHooks = () => {
|
2876
|
-
const { actionProvider } = strapi.service("admin::permission");
|
2877
|
-
const { hooks } = strapi.service("admin::role");
|
2878
|
-
actionProvider.hooks.appliesPropertyToSubject.register(shouldApplyLocalesPropertyToSubject);
|
2879
|
-
hooks.willResetSuperAdminPermissions.register(addAllLocalesToPermissions);
|
2880
|
-
};
|
2881
|
-
const updateActionsProperties = () => {
|
2882
|
-
const { actionProvider } = strapi.service("admin::permission");
|
2883
|
-
actionProvider.hooks.willRegister.register(addLocalesPropertyIfNeeded);
|
2884
|
-
actionProvider.values().forEach((action) => addLocalesPropertyIfNeeded({ value: action }));
|
2885
|
-
};
|
2886
|
-
const i18nActionsService = {
|
2887
|
-
actions,
|
2888
|
-
registerI18nActions,
|
2889
|
-
registerI18nActionsHooks,
|
2890
|
-
updateActionsProperties,
|
2891
|
-
syncSuperAdminPermissionsWithLocales
|
2892
|
-
};
|
2893
|
-
const localesPropertyHandler = async ({ action, section }) => {
|
2894
|
-
const { actionProvider } = strapi.service("admin::permission");
|
2895
|
-
const locales2 = await getService("locales").find();
|
2896
|
-
if (fp.isEmpty(locales2)) {
|
2897
|
-
return;
|
2898
|
-
}
|
2899
|
-
for (const subject of section.subjects) {
|
2900
|
-
const applies = await actionProvider.appliesToProperty("locales", action.actionId, subject.uid);
|
2901
|
-
const hasLocalesProperty = subject.properties.find(
|
2902
|
-
(property) => property.value === "locales"
|
2903
|
-
);
|
2904
|
-
if (applies && !hasLocalesProperty) {
|
2905
|
-
subject.properties.push({
|
2906
|
-
label: "Locales",
|
2907
|
-
value: "locales",
|
2908
|
-
children: locales2.map(({ name, code }) => ({ label: name || code, value: code }))
|
2909
|
-
});
|
2910
|
-
}
|
2911
|
-
}
|
2912
|
-
};
|
2913
|
-
const registerLocalesPropertyHandler = () => {
|
2914
|
-
const { sectionsBuilder } = strapi.service("admin::permission");
|
2915
|
-
sectionsBuilder.addHandler("singleTypes", localesPropertyHandler);
|
2916
|
-
sectionsBuilder.addHandler("collectionTypes", localesPropertyHandler);
|
2917
|
-
};
|
2918
|
-
const sectionsBuilderService = {
|
2919
|
-
localesPropertyHandler,
|
2920
|
-
registerLocalesPropertyHandler
|
2921
|
-
};
|
2922
|
-
const willRegisterPermission = (context) => {
|
2923
|
-
const { permission, condition, user } = context;
|
2924
|
-
const { subject, properties } = permission;
|
2925
|
-
const isSuperAdmin = strapi.service("admin::role").hasSuperAdminRole(user);
|
2926
|
-
if (isSuperAdmin) {
|
2927
|
-
return;
|
2928
|
-
}
|
2929
|
-
const { locales: locales2 } = properties || {};
|
2930
|
-
const { isLocalizedContentType: isLocalizedContentType2 } = getService("content-types");
|
2931
|
-
if (!subject) {
|
2932
|
-
return;
|
2933
|
-
}
|
2934
|
-
const ct = strapi.contentTypes[subject];
|
2935
|
-
if (!isLocalizedContentType2(ct)) {
|
2936
|
-
return;
|
2937
|
-
}
|
2938
|
-
if (locales2 === null) {
|
2939
|
-
return;
|
2940
|
-
}
|
2941
|
-
condition.and({
|
2942
|
-
locale: {
|
2943
|
-
$in: locales2 || []
|
2944
2961
|
}
|
2945
|
-
|
2946
|
-
};
|
2947
|
-
const registerI18nPermissionsHandlers = () => {
|
2948
|
-
const { engine } = strapi.service("admin::permission");
|
2949
|
-
engine.hooks["before-register.permission"].register(willRegisterPermission);
|
2950
|
-
};
|
2951
|
-
const engineService = {
|
2952
|
-
willRegisterPermission,
|
2953
|
-
registerI18nPermissionsHandlers
|
2954
|
-
};
|
2955
|
-
const permissions = () => ({
|
2956
|
-
actions: i18nActionsService,
|
2957
|
-
sectionsBuilder: sectionsBuilderService,
|
2958
|
-
engine: engineService
|
2959
|
-
});
|
2960
|
-
const sendDidInitializeEvent = async () => {
|
2961
|
-
const { isLocalizedContentType: isLocalizedContentType2 } = getService("content-types");
|
2962
|
-
const numberOfContentTypes = fp.reduce(
|
2963
|
-
(sum, contentType) => isLocalizedContentType2(contentType) ? sum + 1 : sum,
|
2964
|
-
0
|
2965
|
-
)(strapi.contentTypes);
|
2966
|
-
await strapi.telemetry.send("didInitializeI18n", { groupProperties: { numberOfContentTypes } });
|
2967
|
-
};
|
2968
|
-
const sendDidUpdateI18nLocalesEvent = async () => {
|
2969
|
-
const numberOfLocales = await getService("locales").count();
|
2970
|
-
await strapi.telemetry.send("didUpdateI18nLocales", {
|
2971
|
-
groupProperties: { numberOfLocales }
|
2972
|
-
});
|
2973
|
-
};
|
2974
|
-
const metrics = () => ({
|
2975
|
-
sendDidInitializeEvent,
|
2976
|
-
sendDidUpdateI18nLocalesEvent
|
2977
|
-
});
|
2978
|
-
const syncNonLocalizedAttributes = async (sourceEntry, model) => {
|
2979
|
-
const { copyNonLocalizedAttributes: copyNonLocalizedAttributes2 } = getService("content-types");
|
2980
|
-
const nonLocalizedAttributes = copyNonLocalizedAttributes2(model, sourceEntry);
|
2981
|
-
if (fp.isEmpty(nonLocalizedAttributes)) {
|
2982
|
-
return;
|
2962
|
+
return { ...matchingLocale };
|
2983
2963
|
}
|
2984
|
-
|
2985
|
-
|
2986
|
-
|
2987
|
-
|
2988
|
-
const localeEntriesToUpdate = await strapi.db.query(uid).findMany({
|
2989
|
-
where: {
|
2990
|
-
documentId,
|
2991
|
-
publishedAt: status === "published" ? { $ne: null } : null,
|
2992
|
-
locale: { $ne: locale2 }
|
2993
|
-
},
|
2994
|
-
select: ["locale", "id"]
|
2995
|
-
});
|
2996
|
-
const entryData = await strapi.documents(uid).omitComponentData(nonLocalizedAttributes);
|
2997
|
-
await utils.async.map(localeEntriesToUpdate, async (entry) => {
|
2998
|
-
const transformedData = await strapi.documents.utils.transformData(
|
2999
|
-
fp.cloneDeep(nonLocalizedAttributes),
|
3000
|
-
{
|
3001
|
-
uid,
|
3002
|
-
status,
|
3003
|
-
locale: entry.locale,
|
3004
|
-
allowMissingId: true
|
3005
|
-
}
|
3006
|
-
);
|
3007
|
-
const componentData = await strapi.documents(uid).updateComponents(entry, transformedData);
|
3008
|
-
await strapi.db.query(uid).update({
|
3009
|
-
where: {
|
3010
|
-
documentId,
|
3011
|
-
publishedAt: status === "published" ? { $ne: null } : null,
|
3012
|
-
locale: { $eq: entry.locale }
|
3013
|
-
},
|
3014
|
-
// The data we send to the update function is the entry data merged with
|
3015
|
-
// the updated component data
|
3016
|
-
data: Object.assign(fp.cloneDeep(entryData), componentData)
|
3017
|
-
});
|
3018
|
-
});
|
2964
|
+
return {
|
2965
|
+
code: "en",
|
2966
|
+
name: "English (en)"
|
2967
|
+
};
|
3019
2968
|
};
|
3020
|
-
const
|
3021
|
-
syncNonLocalizedAttributes
|
3022
|
-
});
|
2969
|
+
const DEFAULT_LOCALE = getInitLocale();
|
3023
2970
|
const find = (params = {}) => strapi.db.query("plugin::i18n.locale").findMany({ where: params });
|
3024
2971
|
const findById = (id) => strapi.db.query("plugin::i18n.locale").findOne({ where: { id } });
|
3025
2972
|
const findByCode = (code) => strapi.db.query("plugin::i18n.locale").findOne({ where: { code } });
|
@@ -3211,11 +3158,32 @@ const contentTypes = () => ({
|
|
3211
3158
|
fillNonLocalizedAttributes,
|
3212
3159
|
getNestedPopulateOfNonLocalizedAttributes
|
3213
3160
|
});
|
3161
|
+
const LOCALIZATION_FIELDS = ["locale", "localizations"];
|
3162
|
+
const sanitize = ({ strapi: strapi2 }) => {
|
3163
|
+
const { isLocalizedContentType: isLocalizedContentType2 } = getService("content-types");
|
3164
|
+
const sanitizeLocalizationFields = fp.curry(
|
3165
|
+
(schema2, entity) => utils.traverseEntity(
|
3166
|
+
({ key, schema: schema22 }, { remove }) => {
|
3167
|
+
const isLocalized = isLocalizedContentType2(schema22);
|
3168
|
+
const isLocalizationField = LOCALIZATION_FIELDS.includes(key);
|
3169
|
+
if (!isLocalized && isLocalizationField) {
|
3170
|
+
remove(key);
|
3171
|
+
}
|
3172
|
+
},
|
3173
|
+
{ schema: schema2, getModel: strapi2.getModel.bind(strapi2) },
|
3174
|
+
entity
|
3175
|
+
)
|
3176
|
+
);
|
3177
|
+
return {
|
3178
|
+
sanitizeLocalizationFields
|
3179
|
+
};
|
3180
|
+
};
|
3214
3181
|
const services = {
|
3215
3182
|
permissions,
|
3216
3183
|
metrics,
|
3217
3184
|
localizations,
|
3218
3185
|
locales,
|
3186
|
+
sanitize,
|
3219
3187
|
"iso-locales": isoLocalesService,
|
3220
3188
|
"content-types": contentTypes
|
3221
3189
|
};
|