Skip to content

Commit

Permalink
fix: handle error when no defaultLocale set #127
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgarciaesgi committed Dec 4, 2023
1 parent ccea8b4 commit 9155dec
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineNuxtConfig({
},
srcDir: './src',
i18n: {
defaultLocale: 'de',
// defaultLocale: 'de',
// dynamicRouteParams: true,
locales: [
{
Expand Down
12 changes: 11 additions & 1 deletion src/core/config/moduleOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import path from 'path';
import { defu } from 'defu';
import type { NuxtI18nOptions } from '@nuxtjs/i18n/dist/module';
import type { ModuleOptions, StrictOptions } from '../../types';
import logSymbols from 'log-symbols';
interface CustomNuxtConfigOptions {
autoImport?: boolean;
rootDir?: string;
buildDir?: string;
srcDir?: string;
i18n?: boolean;
i18nOptions?: NuxtI18nOptions | null;
isDocumentDriven: boolean;
isDocumentDriven?: boolean;
}

class ModuleOptionsStore {
Expand Down Expand Up @@ -39,6 +40,15 @@ class ModuleOptionsStore {
this.i18nOptions = defu(options.i18nOptions, {
strategy: 'prefix_except_default',
} satisfies Partial<NuxtI18nOptions>);
if (
this.i18nOptions.strategy === 'prefix_except_default' &&
!options.i18nOptions.defaultLocale
) {
console.error(
logSymbols.error,
"You have not set 'i18n.defaultLocale', it's required when using 'prefix_except_default' mode (default one)"
);
}
if (options.i18nOptions.locales) {
this.i18nLocales = options.i18nOptions.locales.map((l) => {
if (typeof l === 'string') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export function createRoutesNamedLocationsResolvedExport(routesParams: RoutePara
{
name: RoutesNamesList;
params: unknown;
} & (
} ${
routesParams.length
? `& (
${routesParams
.map(
({ name, params }) =>
Expand All @@ -28,6 +30,8 @@ export function createRoutesNamedLocationsResolvedExport(routesParams: RoutePara
}}`
)
.join('|\n')}
)
)`
: ''
}
`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ export function createRoutesNamedLocationsExport(routesParams: RouteParamsDecl[]
* It's used for programmatic navigation like router.push or <NuxtLink/>
* */
export type RoutesNamedLocations =
${routesParams
.map(
({ name, params }) =>
`{name: "${name}" ${
params.length
? `, params${params.some((s) => s.required) ? '' : '?'}: {
${
routesParams.length
? routesParams
.map(
({ name, params }) =>
`{name: "${name}" ${
params.length
? `, params${params.some((s) => s.required) ? '' : '?'}: {
${params
.map(
({ key, required, catchAll }) =>
`"${key}"${required ? '' : '?'}: (string | number)${catchAll ? '[]' : ''}`
)
.join(',\n')}
}`
: ''
}}`
)
.join('|\n')}
: ''
}}`
)
.join('|\n')
: "''"
}
`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ export function createRoutesNamesListExport(routesList: string[]): string {
/**
* Exhaustive list of all the available route names in the app
* */
export type RoutesNamesList = ${routesList.map((m) => `'${m}'`).join('|\n')}`;
export type RoutesNamesList = ${
routesList.length ? routesList.map((m) => `'${m}'`).join('|\n') : '""'
}`;
}
4 changes: 3 additions & 1 deletion src/core/parser/i18n.modifiers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NuxtPage } from '@nuxt/schema';
import { moduleOptionStore } from '../config';
import type { RoutePathsDecl } from '../../types';
import logSymbols from 'log-symbols';

const specialCharacterRegxp = /([^a-zA-Z0-9_])/gm;

Expand Down Expand Up @@ -41,7 +42,8 @@ export function modifyRoutePrefixDefaultIfI18n(route: NuxtPage) {
} else if (i18nOptions?.strategy === 'prefix_except_default') {
let defaultLocale = i18nLocales.find((f) => f === i18nOptions.defaultLocale)
? i18nOptions.defaultLocale?.replace(specialCharacterRegxp, '\\$&')
: '';
: undefined;

const routeDefaultNameRegXp = new RegExp(`^([a-zA-Z0-9-]+)${separator}${defaultLocale}`, 'g');
const match = routeDefaultNameRegXp.exec(route.name);
if (match) {
Expand Down

0 comments on commit 9155dec

Please sign in to comment.