-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #335 from dreammall-earth/208-feature-implement-pa…
…ge-title-translation-and-vike-language feat(presenter): make i18n locale extractable from url
- Loading branch information
Showing
11 changed files
with
105 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { PageContext } from 'vike/types' | ||
|
||
import { extractLocale } from '#src/locales' | ||
|
||
export function onBeforeRoute(pageContext: PageContext) { | ||
const { urlWithoutLocale, locale } = extractLocale(pageContext.urlPathname) | ||
|
||
return { | ||
pageContext: { | ||
// Make `locale` available as `pageContext.locale` | ||
locale, | ||
// Vike's router will use pageContext.urlLogical instead of pageContext.urlOriginal | ||
urlLogical: urlWithoutLocale, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { PageContextServer } from 'vike/types' | ||
|
||
import { locales } from '#src/locales' | ||
|
||
export { onPrerenderStart } | ||
|
||
function onPrerenderStart(prerenderContext: { pageContexts: PageContextServer[] }): { | ||
prerenderContext: { | ||
pageContexts: PageContextServer[] | ||
} | ||
} { | ||
const storePageContexts: PageContextServer[] = [] | ||
prerenderContext.pageContexts.forEach((pageContext) => { | ||
// Duplicate pageContext for each locale | ||
locales.forEach((locale) => { | ||
// Localize URL | ||
let { urlOriginal } = pageContext | ||
urlOriginal = `/${locale}${urlOriginal}` | ||
storePageContexts.push({ | ||
...pageContext, | ||
urlOriginal, | ||
// Set pageContext.locale | ||
locale, | ||
}) | ||
}) | ||
}) | ||
return { | ||
prerenderContext: { | ||
pageContexts: storePageContexts, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ config.global.provide = { | |
routeParams: { | ||
code: 'my-code', | ||
}, | ||
locale: 'de', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export type LocaleCode = 'de' | 'en' | ||
export const locales: LocaleCode[] = ['de', 'en'] | ||
export const localeDefault = 'de' | ||
export const fallbackLocale = 'en' | ||
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters