next-translate 3.1.1 → 3.1.3

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
@@ -23,9 +23,14 @@
23
23
 
24
24
  </div>
25
25
 
26
- > [!NOTE]
27
- >
28
- > We are working with version **3.0.0** of Next-translate. In recent months we have been very focused on **[Brisa](https://brisa.build/)**. So it has been a long time since the last release, but we have not forgotten about Next-translate. We are working on a new version that will bring many improvements and new features. We are very excited to share it with all of you.
26
+ <p align="center">
27
+ <b>Sponsored by:</b>
28
+ <br />
29
+ <a href="https://kitmul.com/en">
30
+ <img src="images/kitmul-logo.svg" width="200" alt="Kitmul" />
31
+ </a>
32
+ </p>
33
+
29
34
 
30
35
  - [1. About next-translate](#1-about-next-translate)
31
36
  - [How are translations loaded?](#how-are-translations-loaded)
@@ -41,6 +41,7 @@ exports.InternalContext = void 0;
41
41
  var react_1 = __importStar(require("react"));
42
42
  var router_1 = require("next/router");
43
43
  var context_1 = __importDefault(require("./context"));
44
+ var safePluralRules_1 = __importDefault(require("./safePluralRules"));
44
45
  var transCore_1 = __importDefault(require("./transCore"));
45
46
  var useTranslation_1 = __importDefault(require("./useTranslation"));
46
47
  exports.InternalContext = (0, react_1.createContext)({ ns: {}, config: {} });
@@ -54,7 +55,7 @@ function I18nProvider(_a) {
54
55
  var config = __assign(__assign({}, internal.config), newConfig);
55
56
  var localesToIgnore = config.localesToIgnore || ['default'];
56
57
  var ignoreLang = !lang || localesToIgnore.includes(lang);
57
- var pluralRules = (0, react_1.useMemo)(function () { return new Intl.PluralRules(ignoreLang ? undefined : lang); }, [ignoreLang, lang]);
58
+ var pluralRules = (0, react_1.useMemo)(function () { return (0, safePluralRules_1.default)(ignoreLang ? undefined : lang); }, [ignoreLang, lang]);
58
59
  var t = (0, react_1.useMemo)(function () { return (0, transCore_1.default)({ config: config, allNamespaces: allNamespaces, pluralRules: pluralRules, lang: lang }); }, [config, allNamespaces, pluralRules, lang]);
59
60
  return (react_1.default.createElement(context_1.default.Provider, { value: { lang: lang, t: t } },
60
61
  react_1.default.createElement(exports.InternalContext.Provider, { value: { ns: allNamespaces, config: config } }, children)));
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  var react_1 = require("react");
7
7
  var isServer_1 = __importDefault(require("./isServer"));
8
+ var safePluralRules_1 = __importDefault(require("./safePluralRules"));
8
9
  var transCore_1 = __importDefault(require("./transCore"));
9
10
  var wrapTWithDefaultNs_1 = __importDefault(require("./wrapTWithDefaultNs"));
10
11
  function createTranslation(defaultNS) {
@@ -16,7 +17,7 @@ function createTranslation(defaultNS) {
16
17
  var t = (0, transCore_1.default)({
17
18
  config: config,
18
19
  allNamespaces: namespaces,
19
- pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang),
20
+ pluralRules: (0, safePluralRules_1.default)(ignoreLang ? undefined : lang),
20
21
  lang: lang,
21
22
  });
22
23
  return (0, wrapTWithDefaultNs_1.default)(t, defaultNS);
@@ -48,10 +48,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
48
48
  function flat(a) {
49
49
  return a.reduce(function (b, c) { return b.concat(c); }, []);
50
50
  }
51
+ function stripRouteGroups(path) {
52
+ return path.replace(/\/\([^)]+\)/g, '');
53
+ }
51
54
  function getPageNamespaces(_a, page, ctx) {
52
55
  var _b = _a.pages, pages = _b === void 0 ? {} : _b;
53
56
  return __awaiter(this, void 0, void 0, function () {
54
- var rgx, getNs, rgxs, _c, _d, _e, _f;
57
+ var rgx, getNs, normalizedPage, rgxs, pageNs, match, _c, _d, _e, _f;
55
58
  var _this = this;
56
59
  return __generator(this, function (_g) {
57
60
  switch (_g.label) {
@@ -60,6 +63,7 @@ function getPageNamespaces(_a, page, ctx) {
60
63
  getNs = function (ns) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
61
64
  return [2, typeof ns === 'function' ? ns(ctx) : ns || []];
62
65
  }); }); };
66
+ normalizedPage = stripRouteGroups(page);
63
67
  rgxs = Object.keys(pages).reduce(function (arr, p) {
64
68
  if (p.substring(0, rgx.length) === rgx &&
65
69
  new RegExp(p.replace(rgx, '')).test(page)) {
@@ -67,11 +71,21 @@ function getPageNamespaces(_a, page, ctx) {
67
71
  }
68
72
  return arr;
69
73
  }, []);
74
+ pageNs = pages[page];
75
+ if (!pageNs) {
76
+ match = Object.keys(pages).find(function (p) {
77
+ return p !== '*' &&
78
+ !p.startsWith(rgx) &&
79
+ stripRouteGroups(p) === normalizedPage;
80
+ });
81
+ if (match)
82
+ pageNs = pages[match];
83
+ }
70
84
  _c = [[]];
71
85
  return [4, getNs(pages['*'])];
72
86
  case 1:
73
87
  _d = [__spreadArray.apply(void 0, _c.concat([(_g.sent()), true]))];
74
- return [4, getNs(pages[page])];
88
+ return [4, getNs(pageNs)];
75
89
  case 2:
76
90
  _e = [__spreadArray.apply(void 0, _d.concat([(_g.sent()), true]))];
77
91
  _f = flat;
package/lib/cjs/getT.js CHANGED
@@ -40,6 +40,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
42
  var getConfig_1 = __importDefault(require("./getConfig"));
43
+ var safePluralRules_1 = __importDefault(require("./safePluralRules"));
43
44
  var transCore_1 = __importDefault(require("./transCore"));
44
45
  var wrapTWithDefaultNs_1 = __importDefault(require("./wrapTWithDefaultNs"));
45
46
  function getT(locale, namespace) {
@@ -79,7 +80,7 @@ function getT(locale, namespace) {
79
80
  _b.sent();
80
81
  localesToIgnore = config.localesToIgnore || ['default'];
81
82
  ignoreLang = localesToIgnore.includes(lang);
82
- pluralRules = new Intl.PluralRules(ignoreLang ? undefined : lang);
83
+ pluralRules = (0, safePluralRules_1.default)(ignoreLang ? undefined : lang);
83
84
  t = (0, transCore_1.default)({ config: config, allNamespaces: allNamespaces, pluralRules: pluralRules, lang: lang });
84
85
  defaultNamespace = namespaces[0];
85
86
  return [2, (0, wrapTWithDefaultNs_1.default)(t, defaultNamespace)];
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function safePluralRules(locale) {
4
+ try {
5
+ return new Intl.PluralRules(locale);
6
+ }
7
+ catch (_e) {
8
+ return new Intl.PluralRules();
9
+ }
10
+ }
11
+ exports.default = safePluralRules;
@@ -12,6 +12,7 @@ var __assign = (this && this.__assign) || function () {
12
12
  import React, { createContext, useContext, useMemo } from 'react';
13
13
  import { useRouter } from 'next/router';
14
14
  import I18nContext from './context';
15
+ import safePluralRules from './safePluralRules';
15
16
  import transCore from './transCore';
16
17
  import useTranslation from './useTranslation';
17
18
  export var InternalContext = createContext({ ns: {}, config: {} });
@@ -25,7 +26,7 @@ export default function I18nProvider(_a) {
25
26
  var config = __assign(__assign({}, internal.config), newConfig);
26
27
  var localesToIgnore = config.localesToIgnore || ['default'];
27
28
  var ignoreLang = !lang || localesToIgnore.includes(lang);
28
- var pluralRules = useMemo(function () { return new Intl.PluralRules(ignoreLang ? undefined : lang); }, [ignoreLang, lang]);
29
+ var pluralRules = useMemo(function () { return safePluralRules(ignoreLang ? undefined : lang); }, [ignoreLang, lang]);
29
30
  var t = useMemo(function () { return transCore({ config: config, allNamespaces: allNamespaces, pluralRules: pluralRules, lang: lang }); }, [config, allNamespaces, pluralRules, lang]);
30
31
  return (React.createElement(I18nContext.Provider, { value: { lang: lang, t: t } },
31
32
  React.createElement(InternalContext.Provider, { value: { ns: allNamespaces, config: config } }, children)));
@@ -1,5 +1,6 @@
1
1
  import { useMemo } from 'react';
2
2
  import isServer from './isServer';
3
+ import safePluralRules from './safePluralRules';
3
4
  import transCore from './transCore';
4
5
  import wrapTWithDefaultNs from './wrapTWithDefaultNs';
5
6
  export default function createTranslation(defaultNS) {
@@ -11,7 +12,7 @@ export default function createTranslation(defaultNS) {
11
12
  var t = transCore({
12
13
  config: config,
13
14
  allNamespaces: namespaces,
14
- pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang),
15
+ pluralRules: safePluralRules(ignoreLang ? undefined : lang),
15
16
  lang: lang,
16
17
  });
17
18
  return wrapTWithDefaultNs(t, defaultNS);
@@ -46,10 +46,13 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
46
46
  function flat(a) {
47
47
  return a.reduce(function (b, c) { return b.concat(c); }, []);
48
48
  }
49
+ function stripRouteGroups(path) {
50
+ return path.replace(/\/\([^)]+\)/g, '');
51
+ }
49
52
  export default function getPageNamespaces(_a, page, ctx) {
50
53
  var _b = _a.pages, pages = _b === void 0 ? {} : _b;
51
54
  return __awaiter(this, void 0, void 0, function () {
52
- var rgx, getNs, rgxs, _c, _d, _e, _f;
55
+ var rgx, getNs, normalizedPage, rgxs, pageNs, match, _c, _d, _e, _f;
53
56
  var _this = this;
54
57
  return __generator(this, function (_g) {
55
58
  switch (_g.label) {
@@ -58,6 +61,7 @@ export default function getPageNamespaces(_a, page, ctx) {
58
61
  getNs = function (ns) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
59
62
  return [2, typeof ns === 'function' ? ns(ctx) : ns || []];
60
63
  }); }); };
64
+ normalizedPage = stripRouteGroups(page);
61
65
  rgxs = Object.keys(pages).reduce(function (arr, p) {
62
66
  if (p.substring(0, rgx.length) === rgx &&
63
67
  new RegExp(p.replace(rgx, '')).test(page)) {
@@ -65,11 +69,21 @@ export default function getPageNamespaces(_a, page, ctx) {
65
69
  }
66
70
  return arr;
67
71
  }, []);
72
+ pageNs = pages[page];
73
+ if (!pageNs) {
74
+ match = Object.keys(pages).find(function (p) {
75
+ return p !== '*' &&
76
+ !p.startsWith(rgx) &&
77
+ stripRouteGroups(p) === normalizedPage;
78
+ });
79
+ if (match)
80
+ pageNs = pages[match];
81
+ }
68
82
  _c = [[]];
69
83
  return [4, getNs(pages['*'])];
70
84
  case 1:
71
85
  _d = [__spreadArray.apply(void 0, _c.concat([(_g.sent()), true]))];
72
- return [4, getNs(pages[page])];
86
+ return [4, getNs(pageNs)];
73
87
  case 2:
74
88
  _e = [__spreadArray.apply(void 0, _d.concat([(_g.sent()), true]))];
75
89
  _f = flat;
package/lib/esm/getT.js CHANGED
@@ -35,6 +35,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  }
36
36
  };
37
37
  import getConfig from './getConfig';
38
+ import safePluralRules from './safePluralRules';
38
39
  import transCore from './transCore';
39
40
  import wrapTWithDefaultNs from './wrapTWithDefaultNs';
40
41
  export default function getT(locale, namespace) {
@@ -74,7 +75,7 @@ export default function getT(locale, namespace) {
74
75
  _b.sent();
75
76
  localesToIgnore = config.localesToIgnore || ['default'];
76
77
  ignoreLang = localesToIgnore.includes(lang);
77
- pluralRules = new Intl.PluralRules(ignoreLang ? undefined : lang);
78
+ pluralRules = safePluralRules(ignoreLang ? undefined : lang);
78
79
  t = transCore({ config: config, allNamespaces: allNamespaces, pluralRules: pluralRules, lang: lang });
79
80
  defaultNamespace = namespaces[0];
80
81
  return [2, wrapTWithDefaultNs(t, defaultNamespace)];
@@ -0,0 +1,8 @@
1
+ export default function safePluralRules(locale) {
2
+ try {
3
+ return new Intl.PluralRules(locale);
4
+ }
5
+ catch (_e) {
6
+ return new Intl.PluralRules();
7
+ }
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-translate",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -135,7 +135,7 @@
135
135
  "scripts": {
136
136
  "build": "yarn clean && cross-env NODE_ENV=production && yarn tsc",
137
137
  "clean": "yarn clean:build && yarn clean:examples",
138
- "clean:build": "del lib appWith* Dynamic* I18n* i18nStore* i18nRequestStore* index context loadNa* setLang* Trans* useT* withT* getP* getC* *.d.ts getT transC* wrapT* types formatElements isServer AppDirI18nProvider* createTrans*",
138
+ "clean:build": "del lib appWith* Dynamic* I18n* i18nStore* i18nRequestStore* index context loadNa* setLang* Trans* useT* withT* getP* getC* *.d.ts getT transC* wrapT* types formatElements isServer AppDirI18nProvider* createTrans* safePluralRules*",
139
139
  "clean:examples": "del examples/**/.next examples/**/node_modules examples/**/yarn.lock",
140
140
  "example": "yarn example:complex",
141
141
  "example:basic": "yarn build && yarn --cwd examples/basic && yarn --cwd examples/basic dev",
@@ -0,0 +1 @@
1
+ export default function safePluralRules(locale?: string): Intl.PluralRules;