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] Populating locales programatically breaks the admin #20253

Open
SalahAdDin opened this issue May 2, 2024 · 0 comments
Open

[BUG] Populating locales programatically breaks the admin #20253

SalahAdDin opened this issue May 2, 2024 · 0 comments
Assignees
Labels
issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product source: plugin:i18n Source is plugin/i18n package status: pending reproduction Waiting for free time to reproduce the issue, or more information version: 5

Comments

@SalahAdDin
Copy link

Bug report

Required System information

  • Environment: development
  • OS: linux-x64
  • Strapi Version: 5.0.0-beta.5
  • Node/Yarn Version: yarn/4.1.1 npm/? node/v20.12.2 linux x64
  • Edition: Community
  • Database: sqlite
  • Typescript

Describe the bug

We are using the following code to add locales to our project:

import { Core } from "@strapi/strapi";

type StrapiLocale = { code: string; name: string; isDefault?: boolean };

const locales: Array<StrapiLocale> = [
  { code: "es", name: "Spanish (es)" },
  { code: "tr", name: "Turkish (tr)" },
];

async function addLocale(locale: StrapiLocale, strapi: Core.Strapi) {
  const { code, name, isDefault = false } = locale;

  try {
    const existingLocale = await strapi
      .plugin("i18n")
      .service("locales")
      .findByCode(code);

    if (!existingLocale) {
      await strapi.plugin("i18n").service("locales").create({
        name,
        code,
        isDefault,
      });
      console.log(`Locale ${code} added successfully.`);
    } else {
      console.log(`Locale ${code} already exists.`);
    }
  } catch (error) {
    console.error(`Error adding locale ${code}:\n`, error);
  }
}

export const addLocales = async (strapi: Core.Strapi) => {
  console.info("\nAdding given locales to the admin.\n");

  for (const locale of locales) {
    await addLocale(locale, strapi);
  }

  console.info("\nLocales have been set up!");
};

And the following goes on the index bootstrap function:

  async bootstrap({ strapi }: { strapi: Core.Strapi }) {
    await createMediaFolders(strapi);
    await addLocales(strapi);
  },

When running the first time it gives the following issue:

yarn develop                                                                                            ✔  20.12.2  
✔ Cleaning dist dir (23ms)
⠋ Loading Strapi⠋ Building build context
[INFO] Including the following ENV variables as part of the JS bundle:
   - ADMIN_PATH
   - STRAPI_ADMIN_BACKEND_URL
   - STRAPI_TELEMETRY_DISABLED
✔ Building build context (211ms)
✔ Creating admin (916ms)
⠧ Loading Strapi
Creating media folders based on given paths.

Folder '/posts' already exists.
Folder 'posts/hero' already exists.
Folder 'hero/images' already exists.
Folder '/posts' already exists.
Folder 'posts/hero' already exists.
Folder 'hero/videos' already exists.
Folder '/posts' already exists.
Folder 'posts/content' already exists.
Folder 'content/audios' already exists.
Folder '/posts' already exists.
Folder 'posts/content' already exists.
Folder 'content/files' already exists.
Folder '/posts' already exists.
Folder 'posts/content' already exists.
Folder 'content/images' already exists.
Folder '/posts' already exists.
Folder 'posts/content' already exists.
Folder 'content/videos' already exists.
Folder '/profiles' already exists.
Folder 'profiles/photos' already exists.

Media folders have been set up!

Adding given locales to the admin.

Error adding locale es:
ApplicationError: No action found with id "admin::marketplace.read"
   at Object.appliesToProperty (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/admin/server/src/domain/action/provider.ts:54:15)
   at <anonymous> (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/plugin-i18n/server/src/services/permissions/actions.ts:56:61)
   at Array.map (<anonymous>)
   at addAllLocalesToPermissions (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/plugin-i18n/server/src/services/permissions/actions.ts:53:17)
   at Object.syncSuperAdminPermissionsWithLocales (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/plugin-i18n/server/src/services/permissions/actions.ts:91:36)
   at Object.afterCreate (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/plugin-i18n/server/src/bootstrap.ts:9:7)
   at async Object.run (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/database/dist/index.js:6375:11)
   at async Object.create (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/database/dist/index.js:5221:7)
   at Object.create (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/plugin-i18n/server/src/services/locales.ts:16:18)
   at async addLocale (/home/luisalaguna/Projects/experiments/strapi-template/dist/src/functions/addLocales.js:16:13) {
 details: {}
}
Error adding locale tr:
ApplicationError: No action found with id "admin::marketplace.read"
   at Object.appliesToProperty (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/admin/server/src/domain/action/provider.ts:54:15)
   at <anonymous> (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/plugin-i18n/server/src/services/permissions/actions.ts:56:61)
   at Array.map (<anonymous>)
   at addAllLocalesToPermissions (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/plugin-i18n/server/src/services/permissions/actions.ts:53:17)
   at Object.syncSuperAdminPermissionsWithLocales (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/plugin-i18n/server/src/services/permissions/actions.ts:91:36)
   at Object.afterCreate (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/plugin-i18n/server/src/bootstrap.ts:9:7)
   at async Object.run (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/database/dist/index.js:6375:11)
   at async Object.create (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/database/dist/index.js:5221:7)
   at Object.create (/home/luisalaguna/Projects/experiments/strapi-template/node_modules/@strapi/plugin-i18n/server/src/services/locales.ts:16:18)
   at async addLocale (/home/luisalaguna/Projects/experiments/strapi-template/dist/src/functions/addLocales.js:16:13) {
 details: {}
}

Locales have been set up!
✔ Loading Strapi (4583ms)
✔ Generating types (1350ms)
✔ Cleaning dist dir (61ms)
✔ Compiling TS (7396ms)

Project information                                                          

┌────────────────────┬──────────────────────────────────────────────────┐
│ Time               │ Mon Apr 29 2024 00:15:04 GMT-0500 (Colombia Sta… │
│ Launched in        │ 13368 ms                                         │
│ Environment        │ development                                      │
│ Process PID        │ 3069582                                          │
│ Version            │ 5.0.0-beta.5 (node v20.12.2)                     │
│ Edition            │ Community                                        │
│ Database           │ sqlite                                           │
└────────────────────┴──────────────────────────────────────────────────┘

Actions available                                                            

Welcome back!
To access the server ⚡️, go to:
http://localhost:1337

[2024-04-29 00:15:04.060] info: Strapi started successfully
[2024-04-29 00:22:23.626] http: GET /admin (123 ms) 200
[2024-04-29 00:22:25.155] http: GET /admin/project-type (18 ms) 200
[2024-04-29 00:22:25.682] http: GET /admin/init (10 ms) 200
[2024-04-29 00:22:25.755] http: GET /admin/users/me (66 ms) 200
[2024-04-29 00:22:25.794] http: POST /admin/renew-token (35 ms) 200
[2024-04-29 00:22:25.873] http: GET /admin/users/me/permissions (71 ms) 200
[2024-04-29 00:22:26.031] http: GET /admin/information (87 ms) 200
[2024-04-29 00:22:26.036] http: GET /admin/telemetry-properties (46 ms) 200
[2024-04-29 00:22:26.251] http: GET /content-manager/content-types (41 ms) 200
... (7 lines left)

Despite this, the locales are added, but it breaks the admin when browsing on the settings section, see the video.

What's the better way to add locales to the admin programmatically?

Steps to reproduce the behavior

  1. Write the function.
  2. Import it on the index file in the bootstrap function.
  3. Run the development server.
  4. Check the logs.

Expected behavior

It should not give any error logs nor break the admin panel.

Screenshots

AdminBrokenAfterLoadingLocales

Code snippets

Additional context

In the latest Strapi v4 there is not such issue.

@SalahAdDin SalahAdDin changed the title Populating locales programatically breaks the admin [BUG] Populating locales programatically breaks the admin May 2, 2024
@joshuaellis joshuaellis added issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product status: pending reproduction Waiting for free time to reproduce the issue, or more information source: plugin:i18n Source is plugin/i18n package version: 5 labels May 14, 2024
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: plugin:i18n Source is plugin/i18n package status: pending reproduction Waiting for free time to reproduce the issue, or more information version: 5
Projects
Status: To triage
Status: To review
Development

No branches or pull requests

3 participants