mirror of
https://github.com/zen-browser/www.git
synced 2025-07-07 17:05:32 +02:00
refactor(i18n): handle locale better with returning path (#662)
This commit is contained in:
parent
3cdf4384ab
commit
26eb37a879
1 changed files with 13 additions and 1 deletions
|
@ -16,9 +16,21 @@ export type Locale = (typeof locales)[number]
|
||||||
export const getPath =
|
export const getPath =
|
||||||
(locale?: Locale): ((arg0: string) => string) =>
|
(locale?: Locale): ((arg0: string) => string) =>
|
||||||
(path: 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}`
|
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
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue