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

prefixPluginTranslations is essential in plugin SDK output (v5) #63

Open
alex-strapi opened this issue Oct 11, 2024 · 3 comments · May be fixed by #66
Open

prefixPluginTranslations is essential in plugin SDK output (v5) #63

alex-strapi opened this issue Oct 11, 2024 · 3 comments · May be fixed by #66
Labels
issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product

Comments

@alex-strapi
Copy link

Bug report

Required System information

  • Node.js version: v20.17.0
  • NPM version: v10.8.2
  • Strapi version: v5.0.4
  • Database: sqlite
  • Operating system: Linux
  • Is your project Javascript or Typescript: TypeScript

Describe the bug

When generating a plugin, by default registerTrads uses 'getTranslations' for data, which results in:

  [
      {
          "data": "strapi-redirects.[object Object]",
          "locale": "en"
      }
  ]

This translations by default do not work, they only ever use the default in the translation object where ever used.
With prefixPluginTranslations as a util used in registerTrads the correct data structure is output - translation files can be properly read. Either a change is needed to the getTranslations output or the addition of the original prefixPluginTranslations is needed.

Steps to reproduce the behavior

  1. Generate a plugin
  2. Try use a translation
  3. Add the prefixPluginTranslations util in registerTrads
  4. See correct output for plugin translations

Expected behavior

I think this helper function is needed by default or an update is needed in getTranslations, something like this?

import { PLUGIN_ID } from '../pluginId';

const getTranslation = (translations: Record<string, string>): Record<string, string> => {
  const prefixedTranslations: Record<string, string> = {};

  Object.keys(translations).forEach((key) => {
    prefixedTranslations[`${PLUGIN_ID}.${key}`] = translations[key];
  });

  return prefixedTranslations;
};

export { getTranslation };

Code snippets

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: getTranslations(data),
              locale,
            };
          })
          .catch(() => {
            return {
              data: {},
              locale,
            };
          });
      })
    );

    console.log(importedTranslations);

    return importedTranslations;
  },

The console without prefix util:

[
    {
        "data": "strapi-redirects.[object Object]",
        "locale": "en"
    }
]

The console with prefix:

[
    {
        "data": {
            "strapi-redirects.plugin.name": "Example Plugin",
             ... translation objects as expected
        },
        "locale": "en"
    }
]
@derrickmehaffy derrickmehaffy transferred this issue from strapi/strapi Oct 12, 2024
@innerdvations
Copy link
Collaborator

Thanks for the report! This seems like a duplicate of #63 but I'll leave it open until we have time to confirm and fix the issue.

@innerdvations innerdvations added issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product labels Nov 28, 2024
@alex-strapi
Copy link
Author

Thanks for the report! This seems like a duplicate of #63 but I'll leave it open until we have time to confirm and fix the issue.

This is #63 :P

@Link2Twenty
Copy link

I've just submitted a PR without seeing this issue.

#66

I generally include the prefix in my translation file, is that not the norm?

@Link2Twenty Link2Twenty linked a pull request Dec 3, 2024 that will close this issue
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants