@lwrjs/label-module-provider 0.11.0-alpha.0 → 0.11.0-alpha.10

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.
@@ -28,6 +28,7 @@ __export(exports, {
28
28
  });
29
29
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
30
30
  var import_utils = __toModule(require("./utils.cjs"));
31
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
31
32
  var DEFAULT_DIR = [
32
33
  {
33
34
  dir: "$rootDir/src/labels",
@@ -42,6 +43,11 @@ var LabelModuleProvider = class {
42
43
  this.version = lwrVersion;
43
44
  this.defaultLocale = defaultLocale;
44
45
  this.provideDefault = provideDefault;
46
+ this.i18n = config.i18n;
47
+ this.useI18nFallbacks = !!this.i18n.locales.find((f) => f.fallback);
48
+ if (!this.useI18nFallbacks && labelDirs.length > 1) {
49
+ import_diagnostics.logger.warn(`[label-module-provider] You are using the label provider with a deprecated fallback strategy. Please migrate to the new i18n app-level configuration.`);
50
+ }
45
51
  const {rootDir, contentDir, layoutsDir} = config;
46
52
  labelDirs.forEach((config2) => {
47
53
  const normalizedDir = (0, import_shared_utils.normalizeResourcePath)(config2.dir, {
@@ -56,7 +62,16 @@ var LabelModuleProvider = class {
56
62
  });
57
63
  });
58
64
  }
59
- async getLabelInfo(specifier, locale) {
65
+ async getLabelInfo(specifier, initialLocale) {
66
+ if (this.useI18nFallbacks) {
67
+ return (0, import_shared_utils.walkLocaleFallbacks)(initialLocale, this.i18n, async (locale) => {
68
+ return this.getLabelInfoForLocale(specifier, locale, false);
69
+ });
70
+ } else {
71
+ return this.getLabelInfoForLocale(specifier, initialLocale, true);
72
+ }
73
+ }
74
+ async getLabelInfoForLocale(specifier, locale, legacyFallbacks) {
60
75
  const labelInfo = (0, import_utils.parseSpecifier)(this.labelDirs, specifier);
61
76
  if (!labelInfo) {
62
77
  return void 0;
@@ -67,9 +82,9 @@ var LabelModuleProvider = class {
67
82
  if (this.labelJsonCache.has(cacheKey)) {
68
83
  labelJson = this.labelJsonCache.get(cacheKey);
69
84
  } else {
70
- let fsLabels = (0, import_utils.getLabels)(dir, locale);
71
- if (!fsLabels && locale !== this.defaultLocale) {
72
- fsLabels = (0, import_utils.getLabels)(dir, this.defaultLocale);
85
+ let fsLabels = (0, import_utils.getLabels)(dir, locale, legacyFallbacks);
86
+ if (legacyFallbacks && !fsLabels && locale !== this.defaultLocale) {
87
+ fsLabels = (0, import_utils.getLabels)(dir, this.defaultLocale, legacyFallbacks);
73
88
  }
74
89
  labelJson = fsLabels || labelJson;
75
90
  this.labelJsonCache.set(cacheKey, labelJson);
@@ -33,6 +33,7 @@ var import_fs = __toModule(require("fs"));
33
33
  var import_path = __toModule(require("path"));
34
34
  var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
35
35
  var import_validation = __toModule(require("./validation.cjs"));
36
+ var import_diagnostics2 = __toModule(require("@lwrjs/diagnostics"));
36
37
  function isValidLocaleFormat(locale) {
37
38
  return /^[A-Za-z]{2}([_-][A-Za-z]{2})?$/.test(locale);
38
39
  }
@@ -46,13 +47,13 @@ function parseSpecifier(allDirs, specifier) {
46
47
  const fileType = (file ? (0, import_path.extname)(file) : ".js").substr(1);
47
48
  return {labelReference, fileType, ...dirConfig};
48
49
  }
49
- function getLabels(labelDir, locale) {
50
+ function getLabels(labelDir, locale, legacyFallbacks) {
50
51
  const localePath = `${labelDir}/${locale}.json`;
51
52
  try {
52
53
  if ((0, import_fs.existsSync)(localePath)) {
53
54
  return (0, import_validation.validateAndParse)(localePath);
54
55
  }
55
- if (locale.length > 2) {
56
+ if (legacyFallbacks && locale.length > 2) {
56
57
  const lang = locale.substr(0, 2);
57
58
  const country = locale.substr(3);
58
59
  const altLocale = locale[2] === "-" ? `${lang}_${country}` : `${lang}-${country}`;
@@ -72,7 +73,7 @@ function getLabels(labelDir, locale) {
72
73
  description: import_diagnostics.descriptions.UNRESOLVABLE.LABEL_MODULE(localePath, e.message)
73
74
  }, import_diagnostics.LwrUnresolvableError);
74
75
  }
75
- console.error(diagnosticError);
76
+ import_diagnostics2.logger.error(diagnosticError);
76
77
  }
77
78
  return void 0;
78
79
  }
@@ -1,4 +1,4 @@
1
- import type { AbstractModuleId, ModuleCompiled, ModuleEntry, ModuleProvider, ProviderContext, RuntimeParams } from '@lwrjs/types';
1
+ import type { AbstractModuleId, I18NConfig, ModuleCompiled, ModuleEntry, ModuleProvider, ProviderContext, RuntimeParams } from '@lwrjs/types';
2
2
  import { LabelDirConfig, LabelProviderOptions } from './utils.js';
3
3
  export default class LabelModuleProvider implements ModuleProvider {
4
4
  name: string;
@@ -6,9 +6,12 @@ export default class LabelModuleProvider implements ModuleProvider {
6
6
  defaultLocale: string;
7
7
  provideDefault: boolean;
8
8
  labelDirs: LabelDirConfig[];
9
+ i18n: I18NConfig;
10
+ useI18nFallbacks: boolean;
9
11
  private labelJsonCache;
10
12
  constructor({ provideDefault, labelDirs }: LabelProviderOptions, { config, runtimeEnvironment: { defaultLocale, lwrVersion } }: ProviderContext);
11
13
  private getLabelInfo;
14
+ private getLabelInfoForLocale;
12
15
  getModuleEntry({ specifier }: AbstractModuleId, runtimeParams?: RuntimeParams): Promise<ModuleEntry | undefined>;
13
16
  getModule({ specifier, namespace, name }: AbstractModuleId, runtimeParams?: RuntimeParams): Promise<ModuleCompiled | undefined>;
14
17
  }
package/build/es/index.js CHANGED
@@ -1,5 +1,6 @@
1
- import { hashContent, normalizeResourcePath } from '@lwrjs/shared-utils';
2
- import { getLabels, generateModule, isValidLocaleFormat, parseSpecifier, } from './utils.js';
1
+ import { hashContent, normalizeResourcePath, walkLocaleFallbacks } from '@lwrjs/shared-utils';
2
+ import { generateModule, isValidLocaleFormat, parseSpecifier, getLabels, } from './utils.js';
3
+ import { logger } from '@lwrjs/diagnostics';
3
4
  const DEFAULT_DIR = [
4
5
  {
5
6
  dir: '$rootDir/src/labels',
@@ -14,6 +15,11 @@ export default class LabelModuleProvider {
14
15
  this.version = lwrVersion;
15
16
  this.defaultLocale = defaultLocale;
16
17
  this.provideDefault = provideDefault;
18
+ this.i18n = config.i18n;
19
+ this.useI18nFallbacks = !!this.i18n.locales.find((f) => f.fallback);
20
+ if (!this.useI18nFallbacks && labelDirs.length > 1) {
21
+ logger.warn(`[label-module-provider] You are using the label provider with a deprecated fallback strategy. Please migrate to the new i18n app-level configuration.`);
22
+ }
17
23
  // normalize the label directories
18
24
  const { rootDir, contentDir, layoutsDir } = config;
19
25
  labelDirs.forEach((config) => {
@@ -30,7 +36,19 @@ export default class LabelModuleProvider {
30
36
  });
31
37
  });
32
38
  }
33
- async getLabelInfo(specifier, locale) {
39
+ async getLabelInfo(specifier, initialLocale) {
40
+ // If the app has defined any fallbacks in the config. Assume this is the fallback precedence we should follow.
41
+ if (this.useI18nFallbacks) {
42
+ return walkLocaleFallbacks(initialLocale, this.i18n, async (locale) => {
43
+ return this.getLabelInfoForLocale(specifier, locale, false);
44
+ });
45
+ }
46
+ // Other wise use the default fallbacks for backward compatibility
47
+ else {
48
+ return this.getLabelInfoForLocale(specifier, initialLocale, true);
49
+ }
50
+ }
51
+ async getLabelInfoForLocale(specifier, locale, legacyFallbacks) {
34
52
  // Check if this provider handles given specifier package/prefix
35
53
  // Modules handled by this provider have specifiers in this form: "{package}/{labelReference}"
36
54
  const labelInfo = parseSpecifier(this.labelDirs, specifier);
@@ -47,10 +65,10 @@ export default class LabelModuleProvider {
47
65
  }
48
66
  else {
49
67
  // Cache miss, read from fs
50
- let fsLabels = getLabels(dir, locale);
68
+ let fsLabels = getLabels(dir, locale, legacyFallbacks);
51
69
  // Fallback to default locale
52
- if (!fsLabels && locale !== this.defaultLocale) {
53
- fsLabels = getLabels(dir, this.defaultLocale);
70
+ if (legacyFallbacks && !fsLabels && locale !== this.defaultLocale) {
71
+ fsLabels = getLabels(dir, this.defaultLocale, legacyFallbacks);
54
72
  }
55
73
  // Set cache
56
74
  labelJson = fsLabels || labelJson;
@@ -45,8 +45,9 @@ export declare function parseSpecifier(allDirs: LabelDirConfig[], specifier: str
45
45
  *
46
46
  * @param labelDir - The directory which contains the label JSON files
47
47
  * @param locale - The locale to retrieve
48
+ * @param legacyFallbacks - Use the legacy fallback logic
48
49
  */
49
- export declare function getLabels(labelDir: string, locale: string): LabelMap | undefined;
50
+ export declare function getLabels(labelDir: string, locale: string, legacyFallbacks: boolean): LabelMap | undefined;
50
51
  /**
51
52
  * Generate a module string which fulfills a label request
52
53
  *
package/build/es/utils.js CHANGED
@@ -2,6 +2,7 @@ import { existsSync } from 'fs';
2
2
  import { extname } from 'path';
3
3
  import { createSingleDiagnosticError, descriptions, LwrUnresolvableError } from '@lwrjs/diagnostics';
4
4
  import { validateAndParse } from './validation.js';
5
+ import { logger } from '@lwrjs/diagnostics';
5
6
  /**
6
7
  * Validate a locale string against these rules:
7
8
  * - first 2 characters are a language code
@@ -42,8 +43,9 @@ export function parseSpecifier(allDirs, specifier) {
42
43
  *
43
44
  * @param labelDir - The directory which contains the label JSON files
44
45
  * @param locale - The locale to retrieve
46
+ * @param legacyFallbacks - Use the legacy fallback logic
45
47
  */
46
- export function getLabels(labelDir, locale) {
48
+ export function getLabels(labelDir, locale, legacyFallbacks) {
47
49
  // Check for locale JSON file (eg: en_US.json)
48
50
  const localePath = `${labelDir}/${locale}.json`;
49
51
  try {
@@ -51,7 +53,7 @@ export function getLabels(labelDir, locale) {
51
53
  return validateAndParse(localePath);
52
54
  }
53
55
  // If the locale contains a country code, try some fallbacks
54
- if (locale.length > 2) {
56
+ if (legacyFallbacks && locale.length > 2) {
55
57
  const lang = locale.substr(0, 2);
56
58
  const country = locale.substr(3);
57
59
  // Check for a different language/country separator (_ vs -)
@@ -74,7 +76,7 @@ export function getLabels(labelDir, locale) {
74
76
  description: descriptions.UNRESOLVABLE.LABEL_MODULE(localePath, e.message),
75
77
  }, LwrUnresolvableError);
76
78
  }
77
- console.error(diagnosticError);
79
+ logger.error(diagnosticError);
78
80
  }
79
81
  return undefined;
80
82
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.11.0-alpha.0",
7
+ "version": "0.11.0-alpha.10",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -30,15 +30,15 @@
30
30
  "build/**/*.d.ts"
31
31
  ],
32
32
  "dependencies": {
33
- "@lwrjs/diagnostics": "0.11.0-alpha.0",
34
- "@lwrjs/shared-utils": "0.11.0-alpha.0",
33
+ "@lwrjs/diagnostics": "0.11.0-alpha.10",
34
+ "@lwrjs/shared-utils": "0.11.0-alpha.10",
35
35
  "ajv": "6.12.6"
36
36
  },
37
37
  "devDependencies": {
38
- "@lwrjs/types": "0.11.0-alpha.0"
38
+ "@lwrjs/types": "0.11.0-alpha.10"
39
39
  },
40
40
  "engines": {
41
41
  "node": ">=16.0.0"
42
42
  },
43
- "gitHead": "6917abefe32752eeed0f9ba98505ca2b76f1df9d"
43
+ "gitHead": "34b1e289e8de12531f5624b64512e870157195d4"
44
44
  }