Skip to content

Commit

Permalink
feat: Add encodeURI option to allow skipping encodeURI for custom pat…
Browse files Browse the repository at this point in the history
…hs (#199)

Fixes #191
  • Loading branch information
michaelwnyc authored and paulgv committed Jan 24, 2019
1 parent 09b4448 commit 00c89f1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/options-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ Here are all the options available when configuring the module and their default
// the pages option, refer to the "Routing" section for usage
pages: {},

// By default, custom paths will be encoded using encodeURI method.
// This does not work with regexp: "/foo/:slug-:id(\\d+)". If you want to use
// regexp in the path, then set this option to false, and make sure you process
// path encoding yourself.
encodeURI: true,

// Called right before app's locale changes
beforeLanguageSwitch: () => null,

Expand Down
27 changes: 21 additions & 6 deletions docs/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,32 +175,47 @@ You would need to set up your `pages` property as follows:
pages: {
about: {
en: '/about',
fr: '/a-propos',
fr: '/a-propos',
},
'services/index': {
en: '/services',
fr: '/offres',
fr: '/offres',
},
'services/development/index': {
en: '/services/development',
fr: '/offres/developement',
fr: '/offres/developement',
},
'services/development/app/index': {
en: '/services/development/app',
fr: '/offres/developement/app',
fr: '/offres/developement/app',
},
'services/development/website/index': {
en: '/services/development/website',
fr: '/offres/developement/site-web',
fr: '/offres/developement/site-web',
},
'services/coaching/index': {
en: '/services/coaching',
fr: '/offres/formation',
fr: '/offres/formation',
}
}
}]
```
### Regular Expression
By default, all custom paths are encoded to handle non-latin characters in the path. This will convert paths with regular expression like `/foo/:slug-:id(\\d+)` to `/foo/:slug-:id(%5Cd+)`.
If you would like to use regular expression in your custom paths, then you need to set the `encodeURI` option to false. Since no encoding will happen, you will have to make sure to pass in encoded paths yourself.
```js
// nuxt.config.js
['nuxt-i18n', {
encodeURI: false
}]
```
## Ignore routes
Expand Down
1 change: 1 addition & 0 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ exports.DEFAULT_OPTIONS = {
},
parsePages: true,
pages: {},
encodeURI: true,
beforeLanguageSwitch: () => null,
onLanguageSwitched: () => null
}
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports.makeRoutes = (baseRoutes, {
strategy,
parsePages,
pages,
encodeURI,
pagesDir,
differentDomains
}) => {
Expand Down Expand Up @@ -77,7 +78,7 @@ exports.makeRoutes = (baseRoutes, {

// Get custom path if any
if (componentOptions.paths && componentOptions.paths[locale]) {
path = encodeURI(componentOptions.paths[locale])
path = encodeURI ? encodeURI(componentOptions.paths[locale]) : componentOptions.paths[locale]
}

// Add route prefix if needed
Expand Down

0 comments on commit 00c89f1

Please sign in to comment.