next-translate 2.0.0 β†’ 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -240,7 +240,7 @@ In the configuration file you can use both the configuration that we specified h
240
240
  | `loggerEnvironment` | String to define if the logger should run in the browser, in node or both | `"node"` | `"browser"` | `"both"` | `"browser"` | |
241
241
  | `logBuild` | Each page has a log indicating: namespaces, current language and method used to load the namespaces. With this you can disable it. | `Boolean` | `true` |
242
242
  | `loader` | If you wish to disable the webpack loader and manually load the namespaces on each page, we give you the opportunity to do so by disabling this option. | `Boolean` | `true` |
243
- | `interpolation` | Change the delimeter that is used for interpolation. | `{prefix: string; suffix: string, formatter: function }` | `{prefix: '{{', suffix: '}}'}`
243
+ | `interpolation` | Change the delimiter that is used for interpolation. | `{prefix: string; suffix: string, formatter: function }` | `{prefix: '{{', suffix: '}}'}`
244
244
  | `keySeparator` | Change the separator that is used for nested keys. Set to `false` to disable keys nesting in JSON translation files. Can be useful if you want to use natural text as keys. | `string` | `false` | `'.'`
245
245
  | `nsSeparator` | char to split namespace from key. You should set it to `false` if you want to use natural text as keys. | `string` | `false` | `':'`
246
246
  | `defaultNS` | default namespace used if not passed to `useTranslation` or in the translation key. | `string` | `undefined`
@@ -445,7 +445,7 @@ Remember that `['dynamic']` namespace should **not** be listed on `pages` config
445
445
  Asynchronous function to load the `t` function outside components / pages. It works on both server-side and client-side.
446
446
 
447
447
  Unlike the useTranslation hook, we can use here any namespace, it doesn't have to be a namespace defined in the "pages" configuration. It downloads the namespace indicated as a parameter on runtime.
448
- You can load multiple namespaces by giving an array as a paramater, in this case the default namespace will be the fist one.
448
+ You can load multiple namespaces by giving an array as a parameter, in this case the default namespace will be the fist one.
449
449
 
450
450
  Example inside `getStaticProps`:
451
451
 
@@ -553,7 +553,7 @@ function MyApp({ Component, pageProps }) {
553
553
  return <Component {...pageProps} />
554
554
  }
555
555
 
556
- // Wraping your _app.js
556
+ // Wrapping your _app.js
557
557
  export default appWithI18n(MyApp, {
558
558
  ...i18nConfig,
559
559
  // Set to false if you want to load all the namespaces on _app.js getInitialProps
@@ -907,6 +907,8 @@ If you want to change it you can use :
907
907
 
908
908
  When it comes to server components and client components, it can be challenging to load the same thing on different pages. To simplify this process, we have **extracted all the complexity** using the **[`next-translate-plugin`](https://github.com/aralroca/next-translate-plugin)**.
909
909
 
910
+ If you're interested in learning more about how Next-translate works with the new Next.js 13 app dir paradigm, check out **[this article](https://dev.to/aralroca/i18n-translations-in-nextjs-13s-app-dir-for-serverclient-components-4ek8)** for a detailed explanation.
911
+
910
912
  ### Regarding translations:
911
913
 
912
914
  If you use the "app" folder instead of the "pages" folder, the `next-translate-plugin` will automatically detect the change, and you won't need to touch any of the Next-translate configuration. The only difference is that the "pages" configuration property will reference the pages located within the "app" folder.
@@ -926,14 +928,66 @@ module.exports = {
926
928
 
927
929
  By simply changing the "pages" folder to "app," you can consume translations within your pages using the **`useTranslation`** hook or the **`Trans`** component. You will still see the log (if enabled) to know which namespaces are loaded on each page, and everything else should be the same.
928
930
 
931
+ **🌊 Server page/component (+0kb): `app/page.js`:**
932
+ ```js
933
+ import useTranslation from 'next-translate/useTranslation'
934
+
935
+ export default function HomePage() {
936
+ const { t, lang } = useTranslation('home')
937
+
938
+ return <h1>{t('title')}</h1>
939
+ }
940
+ ```
941
+
942
+ **🏝️ Client page/component (+498B): `app/checkout/page.js`**
943
+ ```js
944
+ "use client"
945
+ import useTranslation from 'next-translate/useTranslation'
946
+
947
+ export default function CheckoutPage() {
948
+ const { t, lang } = useTranslation('checkout')
949
+
950
+ return <h1>{t('title')}</h1>
951
+ }
952
+ ```
953
+
929
954
  ### Regarding routing:
930
955
 
931
- The routing is part of the core of Next.js _(not from this library)_, but direct routing support is not yet available with the beta version of Next 13's app directory. As a workaround, Next.js recommends configuring it as described here:
932
- - https://beta.nextjs.org/docs/guides/internationalization.
956
+ Next.js 10 introduced [i18n routing](https://nextjs.org/docs/advanced-features/i18n-routing) support, allowing pages to be rendered by navigating to `/es/page-name`, where the page `pages/page-name.js` was accessed using the `useRouter` hook to obtain the `locale`.
933
957
 
934
- The [`next-translate-plugin`](https://github.com/aralroca/next-translate-plugin) automatically detects the **"lang" parameter**. So, without any rewrite, you can test if your translations work by entering your page with the "lang" parameter. For example: `/some-page?lang=en`.
958
+ However, since the pages have been moved from the `pages` dir to the **app dir**, this i18n routing **no longer works correctly**.
935
959
 
936
- With this in mind, you can do any rewrite as described in the Next documentation, and if the final page has this parameter, everything should work without any additional manual changes. For example, if you rewrite `/en-US/some-page` to `/some-page?lang=en-US`, then the `useTranslation` will look for translations in `en-US` without needing to pass this parameter.
960
+ At Next-translate, we have chosen not to re-implement this functionality, as we aim to be a library for translating pages, rather than routing them. We hope that in the future, this feature will be implemented in the `app` directory, as it is still in beta and many features still need to be supported.
961
+
962
+ However, all the support currently available is with the `lang` parameter. That is, `/es/page-name?lang=es` renders the page `app/page-name/page.js`, where we have internal access to the `lang` parameter, and you **do not need** to do **anything extra** other than using the `useTranslation` hook to consume your translations.
963
+
964
+ All the same, if you wish to use the language as a subpath `/es/page-name` without the param, you can utilize middleware to append the `lang` parameter and perform a rewrite:
965
+
966
+ ```js
967
+ // middleware.ts
968
+ import { NextResponse } from 'next/server'
969
+ import type { NextRequest } from 'next/server'
970
+ import i18n from './i18n'
971
+
972
+ // /es/page-name -> rewrites to -> /es/page-name?lang=es
973
+ export function middleware(request: NextRequest) {
974
+ const locale = request.nextUrl.locale || i18n.defaultLocale
975
+ request.nextUrl.searchParams.set('lang', locale)
976
+ return NextResponse.rewrite(request.nextUrl)
977
+ }
978
+ ```
979
+
980
+ Here in the middleware, we are not adding the locale as a subpath, but rather eliminating the need to manually add the `lang` parameter. By default, the **subpath still exists**, but you **cannot use `useRouter`** from `next/router` to access the `locale` within the components of the `app` directory. Therefore, we **still need the parameter**, even if we hide it from view.
981
+
982
+ And to navigate:
983
+
984
+ ```js
985
+ <Link href={`/?lang=${locale}`} as={`/${locale}`}>{locale}</Link>
986
+ ```
987
+
988
+ If you need more i18n routing features like automatic locale detection you can follow these steps from the Next.js documentation:
989
+
990
+ - https://beta.nextjs.org/docs/guides/internationalization.
937
991
 
938
992
  ## 15. Demos
939
993
 
@@ -1059,11 +1113,12 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
1059
1113
  <td align="center" valign="top" width="14.28%"><a href="https://github.com/wuifdesign"><img src="https://avatars.githubusercontent.com/u/5678318?v=4?s=100" width="100px;" alt="Wuif"/><br /><sub><b>Wuif</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=wuifdesign" title="Code">πŸ’»</a></td>
1060
1114
  </tr>
1061
1115
  <tr>
1062
- <td align="center" valign="top" width="14.28%"><a href="https://github.com/marcesengel"><img src="https://avatars.githubusercontent.com/u/6208890?v=4?s=100" width="100px;" alt="Marces Engel"/><br /><sub><b>Marces Engel</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=marcesengel" title="Code">πŸ’»</a></td>
1063
1116
  <td align="center" valign="top" width="14.28%"><a href="https://michal.bar"><img src="https://avatars.githubusercontent.com/u/9134970?v=4?s=100" width="100px;" alt="MichaΕ‚ Bar"/><br /><sub><b>MichaΕ‚ Bar</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=MrPumpking" title="Code">πŸ’»</a></td>
1064
1117
  <td align="center" valign="top" width="14.28%"><a href="https://github.com/Dragate"><img src="https://avatars.githubusercontent.com/u/28112929?v=4?s=100" width="100px;" alt="Dragate"/><br /><sub><b>Dragate</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=Dragate" title="Code">πŸ’»</a></td>
1118
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/marcesengel"><img src="https://avatars.githubusercontent.com/u/6208890?v=4?s=100" width="100px;" alt="Marces Engel"/><br /><sub><b>Marces Engel</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=marcesengel" title="Code">πŸ’»</a></td>
1065
1119
  <td align="center" valign="top" width="14.28%"><a href="https://github.com/vascosilvaa"><img src="https://avatars.githubusercontent.com/u/16561642?v=4?s=100" width="100px;" alt="Vasco Silva"/><br /><sub><b>Vasco Silva</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=vascosilvaa" title="Code">πŸ’»</a></td>
1066
1120
  <td align="center" valign="top" width="14.28%"><a href="https://github.com/StLyn4"><img src="https://avatars.githubusercontent.com/u/73965070?v=4?s=100" width="100px;" alt="Vsevolod Volkov"/><br /><sub><b>Vsevolod Volkov</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=StLyn4" title="Code">πŸ’»</a></td>
1121
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/felixonmars"><img src="https://avatars.githubusercontent.com/u/1006477?v=4?s=100" width="100px;" alt="Felix Yan"/><br /><sub><b>Felix Yan</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=felixonmars" title="Documentation">πŸ“–</a></td>
1067
1122
  </tr>
1068
1123
  </tbody>
1069
1124
  </table>
@@ -53,10 +53,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
53
53
  exports.log = void 0;
54
54
  var getConfig_1 = __importDefault(require("./getConfig"));
55
55
  var getPageNamespaces_1 = __importDefault(require("./getPageNamespaces"));
56
- var colorEnabled = process.env.NODE_DISABLE_COLORS == null &&
57
- process.env.NO_COLOR == null &&
58
- process.env.TERM !== 'dumb' &&
59
- process.env.FORCE_COLOR !== '0';
60
56
  function loadNamespaces(config) {
61
57
  var _a, _b;
62
58
  if (config === void 0) { config = {}; }
@@ -113,7 +109,11 @@ function removeTrailingSlash(path) {
113
109
  function log(conf, _a) {
114
110
  var page = _a.page, lang = _a.lang, namespaces = _a.namespaces;
115
111
  if (conf.logBuild !== false && typeof window === 'undefined') {
116
- var color = function (c) { return (colorEnabled ? "\u001B[36m".concat(c, "\u001B[0m") : c); };
112
+ var colorEnabled_1 = process.env.NODE_DISABLE_COLORS == null &&
113
+ process.env.NO_COLOR == null &&
114
+ process.env.TERM !== 'dumb' &&
115
+ process.env.FORCE_COLOR !== '0';
116
+ var color = function (c) { return (colorEnabled_1 ? "\u001B[36m".concat(c, "\u001B[0m") : c); };
117
117
  console.log(color('next-translate'), "- compiled page:", color(page), '- locale:', color(lang), '- namespaces:', color(namespaces.join(', ')), '- used loader:', color(conf.loaderName || '-'));
118
118
  }
119
119
  }
@@ -47,10 +47,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
47
47
  };
48
48
  import getConfig from './getConfig';
49
49
  import getPageNamespaces from './getPageNamespaces';
50
- var colorEnabled = process.env.NODE_DISABLE_COLORS == null &&
51
- process.env.NO_COLOR == null &&
52
- process.env.TERM !== 'dumb' &&
53
- process.env.FORCE_COLOR !== '0';
54
50
  export default function loadNamespaces(config) {
55
51
  var _a, _b;
56
52
  if (config === void 0) { config = {}; }
@@ -106,7 +102,11 @@ function removeTrailingSlash(path) {
106
102
  export function log(conf, _a) {
107
103
  var page = _a.page, lang = _a.lang, namespaces = _a.namespaces;
108
104
  if (conf.logBuild !== false && typeof window === 'undefined') {
109
- var color = function (c) { return (colorEnabled ? "\u001B[36m".concat(c, "\u001B[0m") : c); };
105
+ var colorEnabled_1 = process.env.NODE_DISABLE_COLORS == null &&
106
+ process.env.NO_COLOR == null &&
107
+ process.env.TERM !== 'dumb' &&
108
+ process.env.FORCE_COLOR !== '0';
109
+ var color = function (c) { return (colorEnabled_1 ? "\u001B[36m".concat(c, "\u001B[0m") : c); };
110
110
  console.log(color('next-translate'), "- compiled page:", color(page), '- locale:', color(lang), '- namespaces:', color(namespaces.join(', ')), '- used loader:', color(conf.loaderName || '-'));
111
111
  }
112
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-translate",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.",
5
5
  "license": "MIT",
6
6
  "keywords": [