From 26eb37a8796cb1979767f883577f4da8f7dcba73 Mon Sep 17 00:00:00 2001 From: Shintaro Jokagi <61367823+taroj1205@users.noreply.github.com> Date: Fri, 6 Jun 2025 01:33:06 +1200 Subject: [PATCH] refactor(i18n): handle locale better with returning path (#662) --- src/utils/i18n.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/utils/i18n.ts b/src/utils/i18n.ts index 9d5864e..a995d24 100644 --- a/src/utils/i18n.ts +++ b/src/utils/i18n.ts @@ -16,9 +16,21 @@ export type Locale = (typeof locales)[number] export const getPath = (locale?: Locale): ((arg0: string) => string) => (path: string) => { - if (locale && locale !== CONSTANT.I18N.DEFAULT_LOCALE && !path.startsWith(`/${locale}`)) { + // Check if path already contains any locale prefix + const existingLocale = locales.find(l => path.startsWith(`/${l}/`)) + + if (locale && locale !== CONSTANT.I18N.DEFAULT_LOCALE) { + if (existingLocale) { + // Replace existing locale with new locale + return path.replace(`/${existingLocale}/`, `/${locale}/`) + } + // Add new locale prefix return `/${locale}${path.startsWith('/') ? '' : '/'}${path}` } + // Remove locale prefix if switching to default locale + if (existingLocale && locale === CONSTANT.I18N.DEFAULT_LOCALE) { + return path.replace(`/${existingLocale}/`, '/') + } return path }