Skip to content

Commit

Permalink
Move src/locales/*.ts to src/locales/index.ts and change import.
Browse files Browse the repository at this point in the history
  • Loading branch information
Elweyn committed Mar 26, 2024
1 parent 805d515 commit 91f5b0b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion presenter/renderer/+onBeforeRoute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PageContext } from 'vike/types'

import { extractLocale } from '#src/locales/extractLocale'
import { extractLocale } from '#src/locales'

export function onBeforeRoute(pageContext: PageContext) {
const { urlWithoutLocale, locale } = extractLocale(pageContext.urlPathname)
Expand Down
18 changes: 0 additions & 18 deletions presenter/src/locales/extractLocale.ts

This file was deleted.

18 changes: 17 additions & 1 deletion presenter/src/locales/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { extractLocale } from './extractLocale'
export type LocaleCode = 'de' | 'en'
export const locales: LocaleCode[] = ['de', 'en']
export const localeDefault = 'de'
Expand All @@ -7,3 +6,20 @@ export const localizedLocale = [
{ locale: 'de', title: 'Deutsch' },
{ locale: 'en', title: 'English' },
]

export function extractLocale(url: string) {
const urlPaths = url.split('/')
let locale: LocaleCode
let urlWithoutLocale
// We remove the URL locale, for example `/de/about` => `/about`
const firstPath = urlPaths[1] as LocaleCode
if (locales.includes(firstPath)) {
locale = firstPath
urlWithoutLocale = '/' + urlPaths.slice(2).join('/')
} else {
locale = localeDefault
urlWithoutLocale = url
}

return { locale, urlWithoutLocale }
}

0 comments on commit 91f5b0b

Please sign in to comment.