Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Conversation

michaelrazmgah
Copy link
Contributor

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
Copy link
Member

Thanks for this PR @michaelrazmgah

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

TMSchipper
TMSchipper previously approved these changes Oct 17, 2024
@TMSchipper
Copy link
Collaborator

@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.

@TMSchipper TMSchipper dismissed their stale review October 17, 2024 15:46

I cannot reproduce the bug

@michaelrazmgah
Copy link
Contributor Author

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
Copy link
Collaborator

TMSchipper commented Oct 17, 2024

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

Copy link
Collaborator

@TMSchipper TMSchipper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@michaelrazmgah
Copy link
Contributor Author

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
Copy link
Collaborator

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

@mmb-michaelrazmgah
Copy link

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

@boazpoolman
Copy link
Member

Good work everyone 🙂

@boazpoolman boazpoolman merged commit c022621 into pluginpal:master Oct 18, 2024
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants