diff --git a/readme.md b/readme.md index 1236002..091292b 100644 --- a/readme.md +++ b/readme.md @@ -160,7 +160,16 @@ route1: { } ``` -The name of a parent routes' path param should be set to `language` in order for this pattern to work. +The name of a parent routes' path param should be set to `language` in order for this pattern to work. If you want to use a different name you need to provide this as configuration to the route map: + +```js +asRouteMap( + { + ... + }, + { languageParamName: 'locale' } +) +``` --- ### Reverse route diff --git a/src/routeMap.js b/src/routeMap.js index a1ee764..d3b86dc 100644 --- a/src/routeMap.js +++ b/src/routeMap.js @@ -60,7 +60,8 @@ function interpolate(routePath, params) { function pickFromChildren(pathSegments, children, previousParams = {}) { const preparedChildren = children .map(route => { - const path = normalizePath(route.path, previousParams.language) + const { languageParamName = 'language' } = route[routeSymbol].config + const path = normalizePath(route.path, previousParams[languageParamName]) if (path === null) return null return { route, routeSegments: path.split('/') } @@ -163,12 +164,13 @@ function createRoute(config, name, path, data, children, getParent) { get parent() { return getParent() }, children, name, + config, }, }) } function withReverseRoute(config, route) { - const { trailingSlash = false } = config + const { trailingSlash = false, languageParamName = 'language' } = config return Object.assign(reverseRoute, route) function reverseRoute(params = {}) { @@ -176,7 +178,7 @@ function withReverseRoute(config, route) { const resolvedPath = [...parentPaths, route.path].reduce( (base, path) => { - const { language } = params + const { [languageParamName]: language } = params const normalizedPath = normalizePath(path, language) if (normalizedPath === null) throwError(`Could not determine path from ${JSON.stringify(path)} with language ${language}`) return resolve(normalizedPath, base, params) diff --git a/src/routeMap.test.js b/src/routeMap.test.js index c1744d8..5f2290f 100644 --- a/src/routeMap.test.js +++ b/src/routeMap.test.js @@ -115,6 +115,27 @@ describe('asRouteMap', () => { expect(map.language.a({ language: 'en' })).toBe('/en/b') expect(pick('nl/b', [map, (_, x) => x])).toBe(null) }) + test('language - custom language param name', () => { + const map = asRouteMap( + { + language: { + path: ':locale', + + a: { path: { nl: 'a', en: 'b' } } }, + }, + { + languageParamName: 'locale' + } + ) + + expect(pick('nl/a', [map, (_, x) => x])).toBe(map.language.a) + expect(map.language.a({ locale: 'nl' })).toBe('/nl/a') + expect(pick('en/a', [map, (_, x) => x])).toBe(null) + + expect(pick('en/b', [map, (_, x) => x])).toBe(map.language.a) + expect(map.language.a({ locale: 'en' })).toBe('/en/b') + expect(pick('nl/b', [map, (_, x) => x])).toBe(null) + }) }) describe('reverse routing', () => { test('path static', () => { diff --git a/src/types.ts b/src/types.ts index cde2b46..e8268b0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -60,4 +60,5 @@ type LanguageSupport = { language?: string } export type Config = { trailingSlash?: boolean + languageParamName?: string }