pluginpal / strapi-plugin-config-sync

:recycle: CLI & GUI for continuous migration of config data across environments
https://www.pluginpal.io/plugin/config-sync
MIT License
256 stars 36 forks source link

fix: Fixing bug where Strapi UI is crashing when no locale is found #150

Closed michaelrazmgah closed 1 month ago

michaelrazmgah commented 1 month ago

What does it do?

Adding a try-catch if unable to load correct translations - will now fallback to english.

Why is it needed?

I have swedish locale 'sv' in my Strapi app, but it makes the app crash since it is not translated in the plugin

How to test it?

Change locale to 'sv' or another missing translation, it should load 'en' translations

Related issue(s)/PR(s)

Let us know if this is related to any issue/pull request

boazpoolman commented 1 month ago

Thanks for this PR @michaelrazmgah

I'll do some testing and then we can get it merged :)

TMSchipper commented 1 month ago

@michaelrazmgah I attempted to reproduce the bug you described, but I was unable to replicate the issue. Could you please provide more specific details or the exact steps you followed that led to the crash? This would help me better understand how to trigger the problem.

michaelrazmgah commented 1 month ago

ut I was unable to replicate the issue. Could you please provide more specific details or the exact steps you followed tha

My src/admin/app.tsx looks like this:

  config: {
    locales: ["sv"],
  },
  bootstrap() {},
};

This causes a crash when i try to load the strapi dashboard, where it tries to load the Swedish translation even though there is none.

Pasted Graphic

I can solve it by either changing my locale to "en" as follows, or by editing the file as in my PR to make it fallback to the english translation.

export default {
  config: {
    locales: ["en"],
  },
  bootstrap() {},
};
TMSchipper commented 1 month ago

@michaelrazmgah EDIT: I faced your problem, let me take another look! Thank you for your work.

michaelrazmgah commented 1 month ago
async registerTrads({ locales }) {
    const importedTrads = await Promise.all(
      locales.map(async (locale) => {
        try {
          // eslint-disable-next-line import/no-dynamic-require
          const data = require(`./translations/${locale}.json`);
          return {
            data: prefixPluginTranslations(data, pluginId),
            locale,
          };
        } catch {
          return {
            data: {},
            locale,
          };
        }
      }),
    );

    return Promise.resolve(importedTrads);
  },

Hi @michaelrazmgah, Could you try this? If it works for you as well, could you update your code with this example? Cheers!

Hi @TMSchipper, I can confirm your solution works and is a lot nicer, I've updated the PR! Thank you!

TMSchipper commented 1 month ago

@michaelrazmgah thank you for your support and effort! Have a good weekend!

mmb-michaelrazmgah commented 1 month ago

@TMSchipper thanks to you guys for this great plugin. Have a great weekend you too!

boazpoolman commented 1 month ago

Good work everyone 🙂