@spscommerce/i18n 4.8.0 → 4.10.1

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/lib/index.cjs.js CHANGED
@@ -5567,10 +5567,38 @@ function () {
5567
5567
  Browser.type = 'languageDetector';
5568
5568
 
5569
5569
  function localize(_a) {
5570
- var detectorConfig = _a.detectorConfig, translationPath = _a.translationPath, i18nConfig = __rest(_a, ["detectorConfig", "translationPath"]);
5570
+ var detectorConfig = _a.detectorConfig, translationPath = _a.translationPath, customDetectors = _a.customDetectors, i18nConfig = __rest(_a, ["detectorConfig", "translationPath", "customDetectors"]);
5571
+ var languageDetector = new Browser();
5572
+ // The default querystring detector does not support hash routing
5573
+ var hashRouterQuerystringDetector = {
5574
+ name: 'hashRouterQuerystringDetector',
5575
+ lookup: function (options) {
5576
+ try {
5577
+ var lookupQuerystring = (options && options.lookupQuerystring) || 'lang';
5578
+ var regex = new RegExp("[?&]" + lookupQuerystring + "(=([^&#]*)|&|#|$)");
5579
+ var results = regex.exec(window.location.href);
5580
+ if (results && results[2]) {
5581
+ var lang = decodeURIComponent(results[2].replace(/\+/g, ' '));
5582
+ if (lang) {
5583
+ return lang;
5584
+ }
5585
+ }
5586
+ }
5587
+ catch (err) {
5588
+ console.error(err);
5589
+ }
5590
+ return undefined;
5591
+ },
5592
+ };
5593
+ languageDetector.addDetector(hashRouterQuerystringDetector);
5594
+ if (Array.isArray(customDetectors)) {
5595
+ customDetectors.forEach(function (customDetector) {
5596
+ languageDetector.addDetector(customDetector);
5597
+ });
5598
+ }
5571
5599
  var detectionObject = detectorConfig || {
5572
5600
  // order and from where user language should be detected
5573
- order: ['querystring', 'cookie', 'localStorage'],
5601
+ order: ['querystring', 'hashRouterQuerystringDetector', 'cookie', 'localStorage'],
5574
5602
  // keys or params to lookup language from
5575
5603
  lookupQuerystring: 'lang',
5576
5604
  lookupCookie: 'lang',
@@ -5591,8 +5619,9 @@ function localize(_a) {
5591
5619
  }, interpolation: {
5592
5620
  escapeValue: false, // React takes care of interpolation for us
5593
5621
  }, detection: detectionObject }, i18nConfig);
5594
- i18next.use(Backend) // used for loading external translation files
5595
- .use(Browser) // used to detect langage via cookie, local storage, or query param
5622
+ return i18next
5623
+ .use(Backend) // used for loading external translation files
5624
+ .use(languageDetector) // used to detect langage via cookie, local storage, or query param
5596
5625
  .use(initReactI18next) // implement React-specefic libs to handle i18next chores
5597
5626
  .init(mergedConfig);
5598
5627
  }
package/lib/index.esm.js CHANGED
@@ -5534,10 +5534,38 @@ function () {
5534
5534
  Browser.type = 'languageDetector';
5535
5535
 
5536
5536
  function localize(_a) {
5537
- var detectorConfig = _a.detectorConfig, translationPath = _a.translationPath, i18nConfig = __rest(_a, ["detectorConfig", "translationPath"]);
5537
+ var detectorConfig = _a.detectorConfig, translationPath = _a.translationPath, customDetectors = _a.customDetectors, i18nConfig = __rest(_a, ["detectorConfig", "translationPath", "customDetectors"]);
5538
+ var languageDetector = new Browser();
5539
+ // The default querystring detector does not support hash routing
5540
+ var hashRouterQuerystringDetector = {
5541
+ name: 'hashRouterQuerystringDetector',
5542
+ lookup: function (options) {
5543
+ try {
5544
+ var lookupQuerystring = (options && options.lookupQuerystring) || 'lang';
5545
+ var regex = new RegExp("[?&]" + lookupQuerystring + "(=([^&#]*)|&|#|$)");
5546
+ var results = regex.exec(window.location.href);
5547
+ if (results && results[2]) {
5548
+ var lang = decodeURIComponent(results[2].replace(/\+/g, ' '));
5549
+ if (lang) {
5550
+ return lang;
5551
+ }
5552
+ }
5553
+ }
5554
+ catch (err) {
5555
+ console.error(err);
5556
+ }
5557
+ return undefined;
5558
+ },
5559
+ };
5560
+ languageDetector.addDetector(hashRouterQuerystringDetector);
5561
+ if (Array.isArray(customDetectors)) {
5562
+ customDetectors.forEach(function (customDetector) {
5563
+ languageDetector.addDetector(customDetector);
5564
+ });
5565
+ }
5538
5566
  var detectionObject = detectorConfig || {
5539
5567
  // order and from where user language should be detected
5540
- order: ['querystring', 'cookie', 'localStorage'],
5568
+ order: ['querystring', 'hashRouterQuerystringDetector', 'cookie', 'localStorage'],
5541
5569
  // keys or params to lookup language from
5542
5570
  lookupQuerystring: 'lang',
5543
5571
  lookupCookie: 'lang',
@@ -5558,8 +5586,9 @@ function localize(_a) {
5558
5586
  }, interpolation: {
5559
5587
  escapeValue: false, // React takes care of interpolation for us
5560
5588
  }, detection: detectionObject }, i18nConfig);
5561
- i18next.use(Backend) // used for loading external translation files
5562
- .use(Browser) // used to detect langage via cookie, local storage, or query param
5589
+ return i18next
5590
+ .use(Backend) // used for loading external translation files
5591
+ .use(languageDetector) // used to detect langage via cookie, local storage, or query param
5563
5592
  .use(initReactI18next) // implement React-specefic libs to handle i18next chores
5564
5593
  .init(mergedConfig);
5565
5594
  }
package/lib/localize.d.ts CHANGED
@@ -11,6 +11,11 @@ export interface IDetectorOptions {
11
11
  }
12
12
  export interface ILocalizeOptions extends InitOptions {
13
13
  detectorConfig?: IDetectorOptions;
14
+ customDetectors?: {
15
+ name: string;
16
+ lookup: (options: IDetectorOptions) => string | undefined;
17
+ cacheUserLanguage?: (lang: string, options: IDetectorOptions) => void;
18
+ }[];
14
19
  translationPath?: string;
15
20
  }
16
- export declare function localize({ detectorConfig, translationPath, ...i18nConfig }: ILocalizeOptions): void;
21
+ export declare function localize({ detectorConfig, translationPath, customDetectors, ...i18nConfig }: ILocalizeOptions): Promise<import("i18next").TFunction>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@spscommerce/i18n",
3
3
  "description": "SPS Design System React components",
4
- "version": "4.8.0",
4
+ "version": "4.10.1",
5
5
  "author": "SPS Commerce",
6
6
  "license": "UNLICENSED",
7
7
  "repository": "https://github.com/spscommerce/design-system/tree/main/packages/@spscommerce/i18n",