Skip to content

Commit cd99d4c

Browse files
authored
feat: locale middleware (#626)
* feat: locale middl eware added * feat: middleware changed from documents to plugin
1 parent 9efc19d commit cd99d4c

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

server/src/bootstrap.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ const bootstrap = async (context: { strapi: Core.Strapi }) => {
1515

1616
await strapi.service('plugin::navigation.migrate').migrateRelatedIdToDocumentId();
1717

18-
strapi.db.lifecycles.subscribe({
19-
models: ['plugin::i18n.locale'],
20-
async afterCreate(event) {
21-
const adminService = getPluginService(context, 'admin');
22-
await adminService.refreshNavigationLocale(event.result?.code);
23-
},
24-
});
18+
2519
};
2620
export default bootstrap;

server/src/middlewares/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export default {};
1+
import { localeMiddleware } from './middleware';
2+
3+
export default {
4+
localeMiddleware,
5+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Core } from '@strapi/types';
2+
import type { Context, Next } from 'koa';
3+
4+
export const localeMiddleware = ({ strapi }: { strapi: Core.Strapi }) => {
5+
const adminService = strapi.plugin('navigation').service('admin');
6+
7+
return async (ctx: Context, next: Next) => {
8+
await next();
9+
10+
const isCreateLocaleRoute = ctx.method === 'POST' && ctx.path === '/i18n/locales';
11+
if (!isCreateLocaleRoute) return;
12+
13+
const locale = (ctx.body as { code?: string })?.code;
14+
if (!locale || typeof locale !== 'string') return;
15+
16+
try {
17+
await adminService.refreshNavigationLocale(locale);
18+
} catch (error) {
19+
strapi.log.error(`Failed to refresh navigation for new locale ${locale}`, error);
20+
}
21+
};
22+
};
23+
24+
export default localeMiddleware;

server/src/register.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Core } from '@strapi/types';
2+
import middlewares from './middlewares';
23

34
const register = ({ strapi }: { strapi: Core.Strapi }) => {
4-
// register phase
5+
strapi.server.use(middlewares.localeMiddleware({ strapi }));
56
};
6-
77
export default register;

0 commit comments

Comments
 (0)