Repository: strapi/strapi
File: packages/plugins/i18n/server/src/bootstrap.ts (TypeScript)

Code before:
          })
        : null;

    // Get the result of the document service action
    const result = (await next()) as any;

    // We may not have received a result with everything populated that we need
    // Use the id and populate built from non localized fields to get the full
    // result
    let resultID;
    // TODO: fix bug where an empty array can be returned
    if (Array.isArray(result?.entries) && result.entries[0]?.id) {
      resultID = result.entries[0].id;
    } else if (result?.id) {
      resultID = result.id;
    } else {
      return result;
    }

    if (attributesToPopulate.length > 0) {
      const populatedResult = await strapi.db
        .query(schema.uid)
        .findOne({ where: { id: resultID }, populate: attributesToPopulate });

      const currentFields = copyNonLocalizedAttributes(schema, populatedResult);
      const originalFields = copyNonLocalizedAttributes(schema, originalData);

      // Only sync if there are actual changes to non-localized fields
      const shouldSync =
        !originalData ||
        Object.keys(currentFields).some(
          (key) => JSON.stringify(currentFields[key]) !== JSON.stringify(originalFields[key])
        );

      if (shouldSync) {
        await getService('localizations').syncNonLocalizedAttributes(populatedResult, schema);
      }
    }

    return result;
  });
};

export default async () => {
  const { sendDidInitializeEvent } = getService('metrics');
  const { initDefaultLocale } = getService('locales');
  const { sectionsBuilder, actions, engine } = getService('permissions');

  // Data
  await initDefaultLocale();


Review comment (suggestion):
I haven't really looked at what this does or what's going on here in the i18n plugin but just a heads up that I noticed at a lower level in the document service we also have a function `copyNonLocalizedFields` I wonder what's the difference between the two / how they work together:

https://github.com/strapi/strapi/blob/47fec47d39711511b8558211f9692e544ec67a36/packages/core/core/src/services/document-service/internationalization.ts#L95-L127

https://github.com/strapi/strapi/blob/47fec47d39711511b8558211f9692e544ec67a36/packages/core/core/src/services/document-service/repository.ts#L240-L257

Code after:
            populate: attributesToPopulate,
          })
        : null;

    // Get the result of the document service action
    const result = (await next()) as any;

    // We may not have received a result with everything populated that we need
    // Use the id and populate built from non localized fields to get the full
    // result
    let resultID;
    // TODO: fix bug where an empty array can be returned
    if (Array.isArray(result?.entries) && result.entries[0]?.id) {
      resultID = result.entries[0].id;
    } else if (result?.id) {
      resultID = result.id;
    } else {
      return result;
    }

    const populatedResult = await strapi.db.query(schema.uid).findOne({
      where: { id: resultID },
      populate: attributesToPopulate,
    });

    const originalFields = copyNonLocalizedAttributes(schema, originalData);
    const currentFields = copyNonLocalizedAttributes(schema, populatedResult);

    // Only sync if there are actual changes to non-localized fields
    const shouldSync =
      !originalData ||
      Object.keys(currentFields).some((key) => {
        return !isEqual(currentFields[key], originalFields[key]);
      });

    if (shouldSync) {
      await getService('localizations').syncNonLocalizedAttributes(populatedResult, schema);
    }

    return result;
  });
};

export default async () => {
  const { sendDidInitializeEvent } = getService('metrics');
  const { initDefaultLocale } = getService('locales');
  const { sectionsBuilder, actions, engine } = getService('permissions');

  // Data
  await initDefaultLocale();