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

[bug]: translation files not working #61

Closed
LuisRodriguezLD opened this issue Sep 20, 2024 · 1 comment
Closed

[bug]: translation files not working #61

LuisRodriguezLD opened this issue Sep 20, 2024 · 1 comment
Labels
issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product source: sdk-plugin

Comments

@LuisRodriguezLD
Copy link

LuisRodriguezLD commented Sep 20, 2024

What version of @strapi/sdk-plugin are you using?

  • NPM 10.5.0
  • NODE 20
  • @strapi/sdk-plugin 5.2.6

What's Wrong?

The translation files aren't picked up properly.
In the past, there was a helper function that converted the object into an array

const importedTrads = await Promise.all(
      locales.map((locale) => {
        return import(`./translations/${locale}.json`)
          .then(({ default: data }) => {
            return {
              data: prefixPluginTranslations(data, pluginId),  // <--- this helper
              locale,
            };
          })
          .catch(() => {
            return {
              data: {},
              locale,
            };
          });
      })
    );

but that is gone when creating a new plugin with this package

To Reproduce

  1. Create a new plugin
  2. Attempt to use the translation/en.json
  3. Link to a project
  4. Project will not pick the translations

Expected Behaviour

Translation files should work automatically, I tested using the helperFunction and it worked
https://github.com/strapi/strapi/blob/504076d2165a82f124620a3f1f3e3a6017e97f3f/packages/core/email/admin/src/utils/prefixPluginTranslations.ts

Complete working example:

import { getTranslation } from './utils/getTranslation';
import { PLUGIN_ID } from './pluginId';
import { Initializer } from './components/Initializer';
import { PluginIcon } from './components/PluginIcon';

export default {
  register(app: any) {
    app.addMenuLink({
      to: `plugins/${PLUGIN_ID}`,
      icon: PluginIcon,
      intlLabel: {
        id: `${PLUGIN_ID}.plugin.name`,
        defaultMessage: PLUGIN_ID,
      },
      Component: async () => {
        const { App } = await import('./pages/App');

        return App;
      },
    });

    app.registerPlugin({
      id: PLUGIN_ID,
      initializer: Initializer,
      isReady: false,
      name: PLUGIN_ID,
    });
  },

  async registerTrads(app: any) {
    const { locales } = app;

    const importedTranslations = await Promise.all(
      (locales as string[]).map((locale) => {
        return import(`./translations/${locale}.json`)
          .then(({ default: data }) => {
            return {
              data: prefixPluginTranslations(data, PLUGIN_ID),
              locale,
            };
          })
          .catch(() => {
            return {
              data: {},
              locale,
            };
          });
      })
    );

    return importedTranslations;
  },
};

type TradOptions = Record<string, string>;
const prefixPluginTranslations = (trad: TradOptions, pluginId: string): TradOptions => {
  if (!pluginId) {
    throw new TypeError("pluginId can't be empty");
  }
  return Object.keys(trad).reduce((acc, current) => {
    acc[`${pluginId}.${current}`] = trad[current];
    return acc;
  }, {} as TradOptions);
};

@LuisRodriguezLD LuisRodriguezLD added the issue: bug Issue reporting a bug label Sep 20, 2024
@LuisRodriguezLD LuisRodriguezLD changed the title [bug]: [bug]: translation files not working Sep 20, 2024
@innerdvations innerdvations added the severity: high If it breaks the basic use of the product label Nov 28, 2024
@github-project-automation github-project-automation bot moved this to To be reviewed (Open) in Developer Experience Squad Nov 28, 2024
@hanpaine hanpaine moved this from To be reviewed (Open) to Scheduled in sprint in Developer Experience Squad Nov 28, 2024
@hanpaine hanpaine moved this from Scheduled in sprint to Fixed/Shipped in Developer Experience Squad Dec 10, 2024
@hanpaine
Copy link

Resolved by #66

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product source: sdk-plugin
Projects
Status: Fixed/Shipped
Development

No branches or pull requests

3 participants