@strapi/i18n 5.0.0-rc.23 → 5.0.0-rc.24

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.
@@ -195,6 +195,350 @@ const getI18nLocaleArgPlugin = ({ nexus, typeRegistry }) => {
195
195
  }
196
196
  });
197
197
  };
198
+ const register = ({ strapi: strapi2 }) => {
199
+ extendContentTypes(strapi2);
200
+ addContentManagerLocaleMiddleware(strapi2);
201
+ };
202
+ const addContentManagerLocaleMiddleware = (strapi2) => {
203
+ strapi2.server.router.use("/content-manager/collection-types/:model", (ctx, next) => {
204
+ if (ctx.method === "POST" || ctx.method === "PUT") {
205
+ return validateLocaleCreation(ctx, next);
206
+ }
207
+ return next();
208
+ });
209
+ strapi2.server.router.use("/content-manager/single-types/:model", (ctx, next) => {
210
+ if (ctx.method === "POST" || ctx.method === "PUT") {
211
+ return validateLocaleCreation(ctx, next);
212
+ }
213
+ return next();
214
+ });
215
+ };
216
+ const extendContentTypes = (strapi2) => {
217
+ Object.values(strapi2.contentTypes).forEach((contentType) => {
218
+ const { attributes: attributes2 } = contentType;
219
+ ___default.default.set(attributes2, "locale", {
220
+ writable: true,
221
+ private: false,
222
+ configurable: false,
223
+ visible: false,
224
+ type: "string"
225
+ });
226
+ ___default.default.set(attributes2, "localizations", {
227
+ type: "relation",
228
+ relation: "oneToMany",
229
+ target: contentType.uid,
230
+ writable: false,
231
+ private: false,
232
+ configurable: false,
233
+ visible: false,
234
+ unstable_virtual: true,
235
+ joinColumn: {
236
+ name: "document_id",
237
+ referencedColumn: "document_id",
238
+ referencedTable: strapi2.db.metadata.identifiers.getTableName(contentType.collectionName),
239
+ // ensure the population will not include the results we already loaded
240
+ on({ results }) {
241
+ return {
242
+ id: {
243
+ $notIn: results.map((r) => r.id)
244
+ }
245
+ };
246
+ }
247
+ }
248
+ });
249
+ });
250
+ if (strapi2.plugin("graphql")) {
251
+ graphqlProvider({ strapi: strapi2 }).register();
252
+ }
253
+ };
254
+ const info = {
255
+ singularName: "locale",
256
+ pluralName: "locales",
257
+ collectionName: "locales",
258
+ displayName: "Locale",
259
+ description: ""
260
+ };
261
+ const options = {};
262
+ const pluginOptions = {
263
+ "content-manager": {
264
+ visible: false
265
+ },
266
+ "content-type-builder": {
267
+ visible: false
268
+ }
269
+ };
270
+ const attributes = {
271
+ name: {
272
+ type: "string",
273
+ min: 1,
274
+ max: 50,
275
+ configurable: false
276
+ },
277
+ code: {
278
+ type: "string",
279
+ unique: true,
280
+ configurable: false
281
+ }
282
+ };
283
+ const schema = {
284
+ info,
285
+ options,
286
+ pluginOptions,
287
+ attributes
288
+ };
289
+ const locale = {
290
+ schema
291
+ };
292
+ const contentTypes$1 = {
293
+ locale
294
+ };
295
+ const actions = [
296
+ {
297
+ section: "settings",
298
+ category: "Internationalization",
299
+ subCategory: "Locales",
300
+ pluginName: "i18n",
301
+ displayName: "Create",
302
+ uid: "locale.create"
303
+ },
304
+ {
305
+ section: "settings",
306
+ category: "Internationalization",
307
+ subCategory: "Locales",
308
+ pluginName: "i18n",
309
+ displayName: "Read",
310
+ uid: "locale.read",
311
+ aliases: [
312
+ { actionId: "plugin::content-manager.explorer.read", subjects: ["plugin::i18n.locale"] }
313
+ ]
314
+ },
315
+ {
316
+ section: "settings",
317
+ category: "Internationalization",
318
+ subCategory: "Locales",
319
+ pluginName: "i18n",
320
+ displayName: "Update",
321
+ uid: "locale.update"
322
+ },
323
+ {
324
+ section: "settings",
325
+ category: "Internationalization",
326
+ subCategory: "Locales",
327
+ pluginName: "i18n",
328
+ displayName: "Delete",
329
+ uid: "locale.delete"
330
+ }
331
+ ];
332
+ const addLocalesPropertyIfNeeded = ({ value: action }) => {
333
+ const {
334
+ section,
335
+ options: { applyToProperties }
336
+ } = action;
337
+ if (section !== "contentTypes") {
338
+ return;
339
+ }
340
+ if (fp.isArray(applyToProperties) && applyToProperties.includes("locales")) {
341
+ return;
342
+ }
343
+ action.options.applyToProperties = fp.isArray(applyToProperties) ? applyToProperties.concat("locales") : ["locales"];
344
+ };
345
+ const shouldApplyLocalesPropertyToSubject = ({ property, subject }) => {
346
+ if (property === "locales") {
347
+ const model = strapi.getModel(subject);
348
+ return getService("content-types").isLocalizedContentType(model);
349
+ }
350
+ return true;
351
+ };
352
+ const addAllLocalesToPermissions = async (permissions2) => {
353
+ const { actionProvider } = strapi.service("admin::permission");
354
+ const { find: findAllLocales } = getService("locales");
355
+ const allLocales = await findAllLocales();
356
+ const allLocalesCode = allLocales.map(fp.prop("code"));
357
+ return Promise.all(
358
+ permissions2.map(async (permission) => {
359
+ const { action, subject } = permission;
360
+ const appliesToLocalesProperty = await actionProvider.appliesToProperty(
361
+ "locales",
362
+ action,
363
+ subject
364
+ );
365
+ if (!appliesToLocalesProperty) {
366
+ return permission;
367
+ }
368
+ const oldPermissionProperties = fp.getOr({}, "properties", permission);
369
+ return { ...permission, properties: { ...oldPermissionProperties, locales: allLocalesCode } };
370
+ })
371
+ );
372
+ };
373
+ const syncSuperAdminPermissionsWithLocales = async () => {
374
+ const roleService = strapi.service("admin::role");
375
+ const permissionService = strapi.service("admin::permission");
376
+ const superAdminRole = await roleService.getSuperAdmin();
377
+ if (!superAdminRole) {
378
+ return;
379
+ }
380
+ const superAdminPermissions = await permissionService.findMany({
381
+ where: {
382
+ role: {
383
+ id: superAdminRole.id
384
+ }
385
+ }
386
+ });
387
+ const newSuperAdminPermissions = await addAllLocalesToPermissions(superAdminPermissions);
388
+ await roleService.assignPermissions(superAdminRole.id, newSuperAdminPermissions);
389
+ };
390
+ const registerI18nActions = async () => {
391
+ const { actionProvider } = strapi.service("admin::permission");
392
+ await actionProvider.registerMany(actions);
393
+ };
394
+ const registerI18nActionsHooks = () => {
395
+ const { actionProvider } = strapi.service("admin::permission");
396
+ const { hooks } = strapi.service("admin::role");
397
+ actionProvider.hooks.appliesPropertyToSubject.register(shouldApplyLocalesPropertyToSubject);
398
+ hooks.willResetSuperAdminPermissions.register(addAllLocalesToPermissions);
399
+ };
400
+ const updateActionsProperties = () => {
401
+ const { actionProvider } = strapi.service("admin::permission");
402
+ actionProvider.hooks.willRegister.register(addLocalesPropertyIfNeeded);
403
+ actionProvider.values().forEach((action) => addLocalesPropertyIfNeeded({ value: action }));
404
+ };
405
+ const i18nActionsService = {
406
+ actions,
407
+ registerI18nActions,
408
+ registerI18nActionsHooks,
409
+ updateActionsProperties,
410
+ syncSuperAdminPermissionsWithLocales
411
+ };
412
+ const localesPropertyHandler = async ({ action, section }) => {
413
+ const { actionProvider } = strapi.service("admin::permission");
414
+ const locales2 = await getService("locales").find();
415
+ if (fp.isEmpty(locales2)) {
416
+ return;
417
+ }
418
+ for (const subject of section.subjects) {
419
+ const applies = await actionProvider.appliesToProperty("locales", action.actionId, subject.uid);
420
+ const hasLocalesProperty = subject.properties.find(
421
+ (property) => property.value === "locales"
422
+ );
423
+ if (applies && !hasLocalesProperty) {
424
+ subject.properties.push({
425
+ label: "Locales",
426
+ value: "locales",
427
+ children: locales2.map(({ name, code }) => ({ label: name || code, value: code }))
428
+ });
429
+ }
430
+ }
431
+ };
432
+ const registerLocalesPropertyHandler = () => {
433
+ const { sectionsBuilder } = strapi.service("admin::permission");
434
+ sectionsBuilder.addHandler("singleTypes", localesPropertyHandler);
435
+ sectionsBuilder.addHandler("collectionTypes", localesPropertyHandler);
436
+ };
437
+ const sectionsBuilderService = {
438
+ localesPropertyHandler,
439
+ registerLocalesPropertyHandler
440
+ };
441
+ const willRegisterPermission = (context) => {
442
+ const { permission, condition, user } = context;
443
+ const { subject, properties } = permission;
444
+ const isSuperAdmin = strapi.service("admin::role").hasSuperAdminRole(user);
445
+ if (isSuperAdmin) {
446
+ return;
447
+ }
448
+ const { locales: locales2 } = properties || {};
449
+ const { isLocalizedContentType: isLocalizedContentType2 } = getService("content-types");
450
+ if (!subject) {
451
+ return;
452
+ }
453
+ const ct = strapi.contentTypes[subject];
454
+ if (!isLocalizedContentType2(ct)) {
455
+ return;
456
+ }
457
+ if (locales2 === null) {
458
+ return;
459
+ }
460
+ condition.and({
461
+ locale: {
462
+ $in: locales2 || []
463
+ }
464
+ });
465
+ };
466
+ const registerI18nPermissionsHandlers = () => {
467
+ const { engine } = strapi.service("admin::permission");
468
+ engine.hooks["before-register.permission"].register(willRegisterPermission);
469
+ };
470
+ const engineService = {
471
+ willRegisterPermission,
472
+ registerI18nPermissionsHandlers
473
+ };
474
+ const permissions = () => ({
475
+ actions: i18nActionsService,
476
+ sectionsBuilder: sectionsBuilderService,
477
+ engine: engineService
478
+ });
479
+ const sendDidInitializeEvent = async () => {
480
+ const { isLocalizedContentType: isLocalizedContentType2 } = getService("content-types");
481
+ const numberOfContentTypes = fp.reduce(
482
+ (sum, contentType) => isLocalizedContentType2(contentType) ? sum + 1 : sum,
483
+ 0
484
+ )(strapi.contentTypes);
485
+ await strapi.telemetry.send("didInitializeI18n", { groupProperties: { numberOfContentTypes } });
486
+ };
487
+ const sendDidUpdateI18nLocalesEvent = async () => {
488
+ const numberOfLocales = await getService("locales").count();
489
+ await strapi.telemetry.send("didUpdateI18nLocales", {
490
+ groupProperties: { numberOfLocales }
491
+ });
492
+ };
493
+ const metrics = () => ({
494
+ sendDidInitializeEvent,
495
+ sendDidUpdateI18nLocalesEvent
496
+ });
497
+ const syncNonLocalizedAttributes = async (sourceEntry, model) => {
498
+ const { copyNonLocalizedAttributes: copyNonLocalizedAttributes2 } = getService("content-types");
499
+ const nonLocalizedAttributes = copyNonLocalizedAttributes2(model, sourceEntry);
500
+ if (fp.isEmpty(nonLocalizedAttributes)) {
501
+ return;
502
+ }
503
+ const uid = model.uid;
504
+ const documentId = sourceEntry.documentId;
505
+ const locale2 = sourceEntry.locale;
506
+ const status = sourceEntry?.publishedAt ? "published" : "draft";
507
+ const localeEntriesToUpdate = await strapi.db.query(uid).findMany({
508
+ where: {
509
+ documentId,
510
+ publishedAt: status === "published" ? { $ne: null } : null,
511
+ locale: { $ne: locale2 }
512
+ },
513
+ select: ["locale", "id"]
514
+ });
515
+ const entryData = await strapi.documents(uid).omitComponentData(nonLocalizedAttributes);
516
+ await utils.async.map(localeEntriesToUpdate, async (entry) => {
517
+ const transformedData = await strapi.documents.utils.transformData(
518
+ fp.cloneDeep(nonLocalizedAttributes),
519
+ {
520
+ uid,
521
+ status,
522
+ locale: entry.locale,
523
+ allowMissingId: true
524
+ }
525
+ );
526
+ const componentData = await strapi.documents(uid).updateComponents(entry, transformedData);
527
+ await strapi.db.query(uid).update({
528
+ where: {
529
+ documentId,
530
+ publishedAt: status === "published" ? { $ne: null } : null,
531
+ locale: { $eq: entry.locale }
532
+ },
533
+ // The data we send to the update function is the entry data merged with
534
+ // the updated component data
535
+ data: Object.assign(fp.cloneDeep(entryData), componentData)
536
+ });
537
+ });
538
+ };
539
+ const localizations = () => ({
540
+ syncNonLocalizedAttributes
541
+ });
198
542
  const isoLocales = [
199
543
  {
200
544
  code: "af",
@@ -2586,440 +2930,42 @@ const isoLocales = [
2586
2930
  },
2587
2931
  {
2588
2932
  code: "yo-NG",
2589
- name: "Yoruba (Nigeria) (yo-NG)"
2590
- },
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 = [
2933
+ name: "Yoruba (Nigeria) (yo-NG)"
2934
+ },
2777
2935
  {
2778
- section: "settings",
2779
- category: "Internationalization",
2780
- subCategory: "Locales",
2781
- pluginName: "i18n",
2782
- displayName: "Create",
2783
- uid: "locale.create"
2936
+ code: "dje",
2937
+ name: "Zarma (dje)"
2784
2938
  },
2785
2939
  {
2786
- section: "settings",
2787
- category: "Internationalization",
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
- ]
2940
+ code: "dje-NE",
2941
+ name: "Zarma (Niger) (dje-NE)"
2795
2942
  },
2796
2943
  {
2797
- section: "settings",
2798
- category: "Internationalization",
2799
- subCategory: "Locales",
2800
- pluginName: "i18n",
2801
- displayName: "Update",
2802
- uid: "locale.update"
2944
+ code: "zu",
2945
+ name: "Zulu (zu)"
2803
2946
  },
2804
2947
  {
2805
- section: "settings",
2806
- category: "Internationalization",
2807
- subCategory: "Locales",
2808
- pluginName: "i18n",
2809
- displayName: "Delete",
2810
- uid: "locale.delete"
2948
+ code: "zu-ZA",
2949
+ name: "Zulu (South Africa) (zu-ZA)"
2811
2950
  }
2812
2951
  ];
2813
- const addLocalesPropertyIfNeeded = ({ value: action }) => {
2814
- const {
2815
- section,
2816
- options: { applyToProperties }
2817
- } = action;
2818
- if (section !== "contentTypes") {
2819
- return;
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
2952
+ const getInitLocale = () => {
2953
+ const envLocaleCode = process.env.STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE;
2954
+ if (envLocaleCode) {
2955
+ const matchingLocale = isoLocales.find(({ code }) => code === envLocaleCode);
2956
+ if (!matchingLocale) {
2957
+ throw new Error(
2958
+ "Unknown locale code provided in the environment variable STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE"
2845
2959
  );
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
2960
  }
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;
2961
+ return { ...matchingLocale };
2983
2962
  }
2984
- const uid = model.uid;
2985
- const documentId = sourceEntry.documentId;
2986
- const locale2 = sourceEntry.locale;
2987
- const status = sourceEntry?.publishedAt ? "published" : "draft";
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
- });
2963
+ return {
2964
+ code: "en",
2965
+ name: "English (en)"
2966
+ };
3019
2967
  };
3020
- const localizations = () => ({
3021
- syncNonLocalizedAttributes
3022
- });
2968
+ const DEFAULT_LOCALE = getInitLocale();
3023
2969
  const find = (params = {}) => strapi.db.query("plugin::i18n.locale").findMany({ where: params });
3024
2970
  const findById = (id) => strapi.db.query("plugin::i18n.locale").findOne({ where: { id } });
3025
2971
  const findByCode = (code) => strapi.db.query("plugin::i18n.locale").findOne({ where: { code } });