@l10nmonster/helpers-json 3.0.0-alpha.7 → 3.0.0-alpha.8

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.
Files changed (2) hide show
  1. package/i18next.js +22 -7
  2. package/package.json +1 -1
package/i18next.js CHANGED
@@ -6,9 +6,9 @@ import { flatten, unflatten } from 'flat';
6
6
  import { regex } from '@l10nmonster/core';
7
7
  import { flattenAndSplitResources, ARB_ANNOTATION_MARKER, arbPlaceholderHandler } from './utils.js';
8
8
 
9
- const isArbAnnotations = e => e[0].split('.').slice(-2)[0].startsWith(ARB_ANNOTATION_MARKER);
9
+ const isArbAnnotations = e => e[0].split('.').some(segment => segment.startsWith(ARB_ANNOTATION_MARKER));
10
10
  const validPluralSuffixes = new Set(['one', 'other', 'zero', 'two', 'few', 'many']);
11
- const extractArbGroupsRegex = /(?<prefix>.+?\.)?@(?<key>\S+)\.(?<attribute>\S+)/;
11
+ const extractArbGroupsRegex = /(?<prefix>.+?\.)?@(?<key>[^.]+)\.(?<attribute>.+)/;
12
12
  const defaultArbAnnotationHandlers = {
13
13
  description: (_, data) => (data == null ? undefined : data),
14
14
  placeholders: (_, data) => (data == null ? undefined : arbPlaceholderHandler(data)),
@@ -117,7 +117,7 @@ export class I18nextFilter {
117
117
  for (const entry of Object.entries(flatResource)) {
118
118
  if (!this.enableArbAnnotations || !isArbAnnotations(entry)) {
119
119
  const translation = await translator(...entry);
120
- if (translation === undefined) {
120
+ if (translation === null) {
121
121
  delete flatResource[entry[0]];
122
122
  } else {
123
123
  flatResource[entry[0]] = translation;
@@ -127,10 +127,25 @@ export class I18nextFilter {
127
127
  }
128
128
  if (this.enableArbAnnotations) {
129
129
  for (const entry of Object.entries(flatResource).filter(entry => isArbAnnotations(entry))) {
130
- const arbGroups = extractArbGroupsRegex.exec(entry[0]).groups;
131
- const sid = `${arbGroups.prefix ?? ''}${arbGroups.key}`;
132
- if (!this.emitArbAnnotations || !flatResource[sid]) {
133
- delete flatResource[entry[0]];
130
+ const [key, value] = entry;
131
+
132
+ // Always delete if not emitting annotations
133
+ if (!this.emitArbAnnotations) {
134
+ delete flatResource[key];
135
+ continue;
136
+ }
137
+
138
+ // Only keep if regex matches and corresponding translation exists and is not null
139
+ const match = extractArbGroupsRegex.exec(key);
140
+ if (match?.groups) {
141
+ const { prefix = '', key: arbKey, attribute } = match.groups;
142
+ const sid = `${prefix}${arbKey}`;
143
+ if (!Object.prototype.hasOwnProperty.call(flatResource, sid) || flatResource[sid] == null) {
144
+ delete flatResource[key];
145
+ }
146
+ } else {
147
+ // No regex match, can't determine corresponding translation, so delete
148
+ delete flatResource[key];
134
149
  }
135
150
  }
136
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@l10nmonster/helpers-json",
3
- "version": "3.0.0-alpha.7",
3
+ "version": "3.0.0-alpha.8",
4
4
  "description": "Helpers to deal with JSON file formats",
5
5
  "type": "module",
6
6
  "main": "index.js",