Skip to content

Commit

Permalink
Merge pull request #516 from VitNode/i18n_without_routing_when_lang_i…
Browse files Browse the repository at this point in the history
…s_one

feat(frontend): Add i18n without routing when languages is only one
  • Loading branch information
aXenDeveloper authored Sep 16, 2024
2 parents e242d2c + b72f99d commit 21245d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default getRequestConfig(async args => {
const config = await i18nConfig({
...args,
pathsToMessagesFromPlugins: async ({ plugin, locale }) => {
return import(`./plugins/${plugin}/langs/${locale}.json`);
return import(`@/plugins/${plugin}/langs/${locale}.json`);
},
});

Expand Down
25 changes: 16 additions & 9 deletions packages/backend/src/core/admin/languages/delete/delete.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { CustomError, NotFoundError } from '@/errors';
import { InternalDatabaseService } from '@/utils/database/internal_database.service';
import { Injectable } from '@nestjs/common';
import { eq } from 'drizzle-orm';
import * as fs from 'fs';
import { existsSync } from 'fs';
import { rm, unlink, writeFile } from 'fs/promises';
import { join } from 'path';

import {
Expand Down Expand Up @@ -47,15 +48,21 @@ export class DeleteAdminCoreLanguageService {
},
});

[...plugins, { code: 'core' }, { code: 'admin' }].forEach(plugin => {
fs.unlinkSync(
join(
await Promise.all(
[...plugins, { code: 'core' }, { code: 'admin' }].map(async plugin => {
const path = join(
ABSOLUTE_PATHS_BACKEND.plugin({ code: plugin.code }).frontend
.languages,
`${code}.json`,
),
);
});
);

if (!existsSync(path)) {
return;
}

await unlink(path);
}),
);

// Remove assets
const assetsPath = join(
Expand All @@ -64,12 +71,12 @@ export class DeleteAdminCoreLanguageService {
code,
);

fs.rmSync(assetsPath, { recursive: true });
await rm(assetsPath, { recursive: true });

// Update config file
const config: ConfigType = getConfigFile();
config.langs = config.langs.filter(lang => lang.code !== code);
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');
await writeFile(configPath, JSON.stringify(config, null, 2), 'utf-8');

await this.databaseService.db
.delete(core_languages)
Expand Down
5 changes: 4 additions & 1 deletion packages/frontend/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ const removeLocaleFromUrl = (urlPath: string, locales: string[]): string => {
export default function createMiddleware() {
return async function middleware(request: NextRequest) {
const i18n = await getI18n();
const handleI18nRouting = createIntlMiddleware(i18n);
const handleI18nRouting = createIntlMiddleware({
...i18n,
localePrefix: i18n.locales.length > 1 ? 'always' : 'as-needed',
});
const response = handleI18nRouting(request);
const pathname = removeLocaleFromUrl(
request.nextUrl.pathname,
Expand Down

0 comments on commit 21245d5

Please sign in to comment.