Skip to content

Commit

Permalink
Allow '*' for common namespaces declaration (#28)
Browse files Browse the repository at this point in the history
* Allow '*' for common namespaces declaration

* Update package.json
  • Loading branch information
aralroca authored Dec 29, 2019
1 parent e4aaf8e commit b40046d
Show file tree
Hide file tree
Showing 8 changed files with 5,863 additions and 127 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ For a static site you should add a configuration file `i18n.json` in the root of
"finalPagesDir": "pages",
"localesPath": "locales",
"pages": {
"/": ["common", "home"],
"/about": ["common", "about"]
"*": ["common"],
"/": ["home", "example"],
"/about": ["about"]
}
}
```
Expand Down Expand Up @@ -279,7 +280,7 @@ In order to use each translation in the project, use the _translation id_ compos
| `finalPagesDir` | A string with the directory that is going to be used to build the pages. Only "pages" and "src/pages" are possible. IT ONLY APPLIES in static sites. If you use the `appWithI18n` this configuration won't have any effect. | `string` | `"pages"` |
| `localesPath` | A string with the directory of JSONs locales. THIS ONLY WORKS with static sites. If you use the `appWithI18n` then you should use the `loadLocaleFrom` config. | `string` | `"locales"` |
| `loadLocaleFrom` | A function to return the dynamic import of each locale. IT ONLY WORKS with a server (`appWithI18n`). For static site use the `localesPath` instead. [See an example](#use-translations-in-your-pages-1) | `Function` | `null` |
| `pages` | An object that defines the namespaces used in each page. Example of object: `{"/": ["common", "home"]}`. This configuration is for both: static sites and with a server. | `Object<Array<string>>` | `{}` |
| `pages` | An object that defines the namespaces used in each page. Example of object: `{"/": ["home", "example"]}`. This configuration is for both: static sites and with a server. To add namespaces to all pages you should use the key `"*"`, ex: `{"*": ["common"]}`. | `Object<Array<string>>` | `{}` |

## 6. API

Expand Down
2 changes: 1 addition & 1 deletion cli/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function readPageNamespaces(langs) {
.replace(currentPagesDir, '')
.replace(/(\/index.js)|(\/index.jsx)|(\.js)|(\.jsx)/gm, '') || '/'

const namespaces = pages[pageId] || []
const namespaces = [...(pages['*'] || []), ...(pages[pageId] || [])]

if (!isNextInternal(page)) {
console.log(`🔨 ${pageId}`, namespaces)
Expand Down
6 changes: 3 additions & 3 deletions examples/static-site/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"allLanguages": ["en", "ca", "es"],
"defaultLanguage": "en",
"pages": {
"/": ["common", "home"],
"/more-examples": ["common", "more-examples"],
"/more-examples/dynamic-namespace": ["common"]
"*": ["common"],
"/": ["home"],
"/more-examples": ["more-examples"]
}
}
6 changes: 3 additions & 3 deletions examples/with-server/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module.exports = {
loadLocaleFrom: (lang, ns) =>
import(`./locales/${lang}/${ns}.json`).then(m => m.default),
pages: {
'/': ['common', 'home'],
'/more-examples': ['common', 'more-examples'],
'/more-examples/dynamic-namespace': ['common'],
'*': ['common'],
'/': ['home'],
'/more-examples': ['more-examples'],
},
}
5,727 changes: 5,727 additions & 0 deletions examples/with-server/yarn.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-translate",
"version": "0.1.8",
"version": "0.2.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,9 +38,9 @@
"next-translate": "./cli/builder.js"
},
"devDependencies": {
"@babel/cli": "7.7.5",
"@babel/core": "7.7.5",
"@babel/preset-env": "7.7.6",
"@babel/cli": "7.7.7",
"@babel/core": "7.7.7",
"@babel/preset-env": "7.7.7",
"@testing-library/react": "9.4.0",
"babel-jest": "24.9.0",
"babel-plugin-transform-es2015-modules-commonjs": "6.26.2",
Expand Down
11 changes: 7 additions & 4 deletions src/appWithI18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ export default function appWithI18n(AppToTranslate, config = {}) {
let appProps = { pageProps: {} }

if (AppToTranslate.getInitialProps) {
appProps = (await AppToTranslate.getInitialProps({
Component, ctx, lang
})) || {}
appProps =
(await AppToTranslate.getInitialProps({
Component,
ctx,
lang,
})) || {}
}
const page = removeTrailingSlash(ctx.pathname)
const { pages = {} } = config
const namespaces = pages[page] || []
const namespaces = [...(pages['*'] || []), ...(pages[page] || [])]
const pageNamespaces = await Promise.all(
namespaces.map(ns =>
typeof config.loadLocaleFrom === 'function'
Expand Down
Loading

0 comments on commit b40046d

Please sign in to comment.