From 5d321f1a973f2c5284bf239d5953c1d5ca4aa465 Mon Sep 17 00:00:00 2001 From: Aral Roca Gomez Date: Thu, 12 Dec 2019 14:15:01 +0100 Subject: [PATCH] Fix pathnames with trailing slash (#25) --- package.json | 2 +- src/appWithI18n.js | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 6ef6bc07..29206a56 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/appWithI18n.js b/src/appWithI18n.js index 51c21b75..13c5cb3b 100644 --- a/src/appWithI18n.js +++ b/src/appWithI18n.js @@ -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 @@ -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'