Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pathnames with trailing slash #25

Merged
merged 1 commit into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-translate",
"version": "0.1.7",
"version": "0.1.8",
"description": "Next.js utility to translate pages without the need of a server (static i18n pages generator).",
"license": "MIT",
"keywords": [
Expand Down
8 changes: 6 additions & 2 deletions src/appWithI18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function getLang(ctx, config) {
return startsWithLang ? asPath.split('/')[1] : config.defaultLanguage
}

function removeTrailingSlash(path = '') {
return path.length > 1 && path.endsWith('/') ? path.slice(0, -1) : path
}

export default function appWithI18n(AppToTranslate, config = {}) {
function AppWithTranslations(props) {
const { lang, namespaces } = props
Expand All @@ -33,9 +37,9 @@ export default function appWithI18n(AppToTranslate, config = {}) {
Component, ctx, lang
})) || {}
}

const page = removeTrailingSlash(ctx.pathname)
const { pages = {} } = config
const namespaces = pages[ctx.pathname] || []
const namespaces = pages[page] || []
const pageNamespaces = await Promise.all(
namespaces.map(ns =>
typeof config.loadLocaleFrom === 'function'
Expand Down