Skip to content

Commit

Permalink
Support defaultLanguage as a function (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca authored Jan 24, 2020
1 parent b79f0c8 commit 89e2038
Show file tree
Hide file tree
Showing 10 changed files with 7,418 additions and 1,411 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ yarn-error.log*

# lib build
*.js
!src/*.js
!src/**/*.js
!__tests__/*.js
!examples/**/*.js
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ In order to use each translation in the project, use the _translation id_ compos

| Option | Description | Type | Default |
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- | -------------------------------------------------------------------------- |
| `defaultLanguage` | A string with the ISO locale ("en" as default). | `string` | `"en"` |
| `defaultLanguage` | A string with the ISO locale ("en" as default). Also you can pass it as a function to return the language depending the `req` param (in case to use a server). | `string|function` | `"en"` |
| `allLanguages` | An array with all the languages to use in the project. | `Array<string>` | `[]` |
| `ignoreRoutes` | An array with all the routes to ignore in the middleware. This config property only effect using a custom server with the `i18nMiddleware`. | `Array<string>` | `['/_next/', '/static/', '/favicon.ico', '/manifest.json', '/robots.txt']` |
| `redirectToDefaultLang` | When is set to `true` the route `/some-page` redirects to `/en/some-path` (if `en` is the default language). When is set to `false` entering to `/some-path` is rendering the page with the default language but without redirecting. IT ONLY APPLIES using a server with the `i18nMiddleware`. | `boolean` | `false` |
Expand Down
2 changes: 1 addition & 1 deletion __tests__/i18nMiddleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('i18nMiddleware', () => {
server2 = await loadServerWithMiddleware(
{
allLanguages,
defaultLanguage,
defaultLanguage: () => defaultLanguage,
redirectToDefaultLang: true,
},
3006
Expand Down
18 changes: 17 additions & 1 deletion examples/with-server/i18n.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
const defaultLangsFromHost = {
'uk.co': 'en',
cat: 'ca',
com: 'ca',
default: 'es', // localhost
}

function getDomain(host) {
const domain = host.split('.')
return domain[domain.length - 1]
}

module.exports = {
allLanguages: ['en', 'ca', 'es'],
defaultLanguage: 'es',
defaultLanguage: req => {
let host = req ? req.get('Host') : window.location.hostname
const domain = getDomain(host)
return defaultLangsFromHost[domain] || defaultLangsFromHost.default
},
redirectToDefaultLang: true,
loadLocaleFrom: (lang, ns) =>
import(`./locales/${lang}/${ns}.json`).then(m => m.default),
Expand Down
5,784 changes: 5,784 additions & 0 deletions examples/with-server/yarn.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-translate",
"version": "0.3.1",
"version": "0.4.0",
"description": "Next.js utility to translate pages without the need of a server (static i18n pages generator).",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -38,16 +38,16 @@
"next-translate": "./cli/builder.js"
},
"devDependencies": {
"@babel/cli": "7.7.7",
"@babel/core": "7.7.7",
"@babel/preset-env": "7.7.7",
"@babel/cli": "7.8.3",
"@babel/core": "7.8.3",
"@babel/preset-env": "7.8.3",
"@testing-library/react": "9.4.0",
"babel-jest": "24.9.0",
"babel-jest": "25.1.0",
"babel-plugin-transform-es2015-modules-commonjs": "6.26.2",
"babel-preset-minify": "0.5.1",
"express": "4.17.1",
"husky": "4.0.1",
"jest": "24.9.0",
"husky": "4.2.1",
"jest": "25.1.0",
"next": "9.1.7",
"prettier": "1.19.1",
"pretty-quick": "2.0.1",
Expand Down
6 changes: 6 additions & 0 deletions src/_helpers/getDefaultLang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function getDefaultLang(req, config) {
if (typeof config.defaultLanguage === 'function') {
return config.defaultLanguage(req)
}
return config.defaultLanguage
}
5 changes: 3 additions & 2 deletions src/appWithI18n.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import React from 'react'
import I18nProvider from './I18nProvider'
import getDefaultLang from './_helpers/getDefaultLang'

function getLang(ctx, config) {
const { req, asPath = '' } = ctx

if (req) return req.query.lang || config.defaultLanguage
if (req) return req.query.lang || getDefaultLang(req, config)

const startsWithLang = config.allLanguages.some(l =>
asPath.startsWith(`/${l}`)
)

return startsWithLang ? asPath.split('/')[1] : config.defaultLanguage
return startsWithLang ? asPath.split('/')[1] : getDefaultLang(req, config)
}

function removeTrailingSlash(path = '') {
Expand Down
5 changes: 4 additions & 1 deletion src/i18nMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import getDefaultLang from './_helpers/getDefaultLang'

export default function i18nMiddleware(config = {}) {
const {
ignoreRoutes = [
Expand All @@ -7,7 +9,6 @@ export default function i18nMiddleware(config = {}) {
'/manifest.json',
'/robots.txt',
],
defaultLanguage = 'en',
allLanguages = [],
redirectToDefaultLang = false,
} = config
Expand All @@ -26,6 +27,8 @@ export default function i18nMiddleware(config = {}) {
* Redirect or add lang without redirecting (depending the config)
*/
if (!startsWithLang) {
const defaultLanguage = getDefaultLang(req, config) || 'en'

if (redirectToDefaultLang) {
res.redirect(301, `/${defaultLanguage}${req.url}`)
return
Expand Down
Loading

0 comments on commit 89e2038

Please sign in to comment.