@verbaly/compiler 0.24.0 → 0.25.0

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/dist/index.d.ts CHANGED
@@ -120,6 +120,7 @@ declare class MessageRegistry {
120
120
  remove(file: string): void;
121
121
  messages(): Map<string, TaggedMessage>;
122
122
  usedKeys(): Map<string, string[]>;
123
+ origins(): Map<string, string[]>;
123
124
  }
124
125
  //#endregion
125
126
  //#region src/check.d.ts
@@ -197,6 +198,7 @@ interface ExportOptions {
197
198
  format?: ExportFormat;
198
199
  out?: string;
199
200
  missing?: boolean;
201
+ origins?: Record<string, string[]>;
200
202
  }
201
203
  interface ExportedFile {
202
204
  locale: string;
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import { parse } from "@babel/parser";
2
2
  import { createHash } from "node:crypto";
3
3
  import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, watch, writeFileSync } from "node:fs";
4
4
  import { basename, dirname, join, relative, resolve } from "node:path";
5
- import { RICH_TAGS, createVerbaly, normalizeLink, parse as parse$1, parseTags, safeAttribute } from "verbaly";
5
+ import { RICH_TAGS, createVerbaly, localeDirection, normalizeLink, parse as parse$1, parseTags, safeAttribute } from "verbaly";
6
6
  import { pathToFileURL } from "node:url";
7
7
  import MagicString from "magic-string";
8
8
  import { glob } from "tinyglobby";
@@ -918,6 +918,15 @@ var MessageRegistry = class {
918
918
  }
919
919
  return out;
920
920
  }
921
+ origins() {
922
+ const out = this.usedKeys();
923
+ for (const analysis of this.files.values()) for (const msg of analysis.tagged) {
924
+ const files = out.get(msg.key) ?? [];
925
+ if (!files.includes(msg.file)) files.push(msg.file);
926
+ out.set(msg.key, files);
927
+ }
928
+ return out;
929
+ }
921
930
  };
922
931
  //#endregion
923
932
  //#region src/extract.ts
@@ -1248,7 +1257,8 @@ function exportCatalogs(cfg, catalogs, options = {}) {
1248
1257
  const all = Object.keys(source).filter((key) => source[key]).sort().map((key) => ({
1249
1258
  key,
1250
1259
  source: source[key],
1251
- target: catalog[key] ?? ""
1260
+ target: catalog[key] ?? "",
1261
+ location: options.origins?.[key]
1252
1262
  }));
1253
1263
  const untranslated = all.filter((entry) => !entry.target);
1254
1264
  const entries = options.missing ? untranslated : all;
@@ -1344,8 +1354,13 @@ function parseExchangeFile(file, localeOverride) {
1344
1354
  throw new Error(`[verbaly] ${file}: unsupported format, expected .xlf, .xliff or .csv.`);
1345
1355
  }
1346
1356
  function toXliff(sourceLocale, locale, entries) {
1347
- const units = entries.map(({ key, source, target }) => [
1357
+ const units = entries.map(({ key, source, target, location }) => [
1348
1358
  ` <unit id="${escapeXml(key)}">`,
1359
+ ...location?.length ? [
1360
+ " <notes>",
1361
+ ...location.map((file) => ` <note category="location">${escapeXml(file)}</note>`),
1362
+ " </notes>"
1363
+ ] : [],
1349
1364
  ` <segment state="${target ? "translated" : "initial"}">`,
1350
1365
  ` <source>${escapeXml(source)}</source>`,
1351
1366
  ` <target>${escapeXml(target)}</target>`,
@@ -1395,8 +1410,8 @@ function unescapeXml(text) {
1395
1410
  }
1396
1411
  function toCsv(entries) {
1397
1412
  return [
1398
- "key,source,target",
1399
- ...entries.map(({ key, source, target }) => `${csvField(key)},${csvField(source)},${csvField(target)}`),
1413
+ "key,source,target,location",
1414
+ ...entries.map(({ key, source, target, location }) => `${csvField(key)},${csvField(source)},${csvField(target)},${csvField(location?.join("; ") ?? "")}`),
1400
1415
  ""
1401
1416
  ].join("\r\n");
1402
1417
  }
@@ -1545,6 +1560,7 @@ function renderHtml(html, options) {
1545
1560
  const chunkStart = m.index + 1 + rawName.length;
1546
1561
  if (tagName === "html" && options.setLang !== false) {
1547
1562
  setAttribute(ms, html, chunkStart, openEnd, attrChunk, "lang", options.locale);
1563
+ setAttribute(ms, html, chunkStart, openEnd, attrChunk, "dir", localeDirection(options.locale));
1548
1564
  continue;
1549
1565
  }
1550
1566
  const attrs = parseAttrs(attrChunk);