Skip to content

Commit

Permalink
initial draft of i18n string dump script
Browse files Browse the repository at this point in the history
  • Loading branch information
rsek committed Sep 1, 2022
1 parent c6ad6de commit 66ab1a1
Show file tree
Hide file tree
Showing 17 changed files with 16,392 additions and 221 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- ⚠️ This README has been generated from the file(s) "./src/templates/blueprint.md" ⚠️--><h1 align="center">Dataforged v1.5.0</h1>
<!-- ⚠️ This README has been generated from the file(s) "./src/templates/blueprint.md" ⚠️--><h1 align="center">Dataforged v2.0.0-0</h1>
<p align="center">
<a href="https://www.npmjs.com/package/dataforged"><img alt="undefined" src="https://img.shields.io/npm/v/dataforged?logo=npm" height="20"/></a>
<a href="https://www.npmjs.com/package/dataforged"><img alt="undefined" src="https://img.shields.io/npm/dm/dataforged?logo=npm" height="20"/></a>
Expand Down
2 changes: 1 addition & 1 deletion dataforged-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"deep-freeze-strict": "^1.1.1",
"fast-glob": "^3.2.11",
"fs-extra": "^10.0.1",
"jsonpath-plus": "^6.0.1",
"jsonpath-plus": "^7.1.0",
"lodash-es": "^4.17.21",
"prettier": "^2.3.0",
"reflect-metadata": "^0.1.13",
Expand Down
106 changes: 102 additions & 4 deletions dataforged-tools/src/localization/extractLocaleStrings.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,107 @@
import type { IOracleBase } from "@json_out/index.js";
/* eslint-disable no-restricted-imports */
import type { IHasId, IOracleBase } from "@json_out/index.js";
import { Gamespace } from "@json_out/index.js";
import { writeYaml } from "@utils/io/writeYaml.js";
import { formatIdFragment } from "@utils/toIdFragment.js";
import { EnumLike } from "@utils/types/EnumLike.js";
import yaml from "js-yaml";
import { JSONPath } from "jsonpath-plus";
import JsonPointer from "jsonpointer";
import _ from "lodash-es";
import fs from "fs";

export const localizableKeys = [
"Canonical",
"Short",
"Standard",
"Label",
"Aliases",
"Result",
"Summary",
"Description",
"Requirement",
"Text",
"Features",
"Drives",
"Tactics",
"Your Truth",
"Character"
];

function extractLocaleData(json: "") {
const localizationBlacklist = [""];
}
/**
* Crawls a json object for localizable strings an
* @param json - The json object to be crawled.
*/
export function extractLocalizableStrings<T extends Record<string,unknown>>(json: T) {
const stringHash: Record<string,string> = {};
JSONPath<Record<typeof localizableKeys[number], string|string[]>>({
json,
path: `$..*[${localizableKeys.join(",")}]`,
resultType: "all" ,
flatten: true ,
callback: ( payload: {parentProperty: string, value: string|string[], parent: IHasId}) => {
const baseId = `${payload.parent.$id}#${payload.parentProperty}`;
if (Array.isArray(payload.value)) {
if (typeof payload.value[0] === "string") {
payload.value.forEach((item, index) => {
stringHash[`${baseId}.${index}`] = item;
});
}
} else if (typeof payload.value === "string") {
stringHash[baseId] = payload.value;
}
}
});
return stringHash;
}

import datasworn from "../json/ironsworn/datasworn.json" assert {type: "json"};
import dataforged from "../json/starforged/dataforged.json" assert {type: "json"};

const dataswornItems = _.map(datasworn, (value, key) => ({ data: value, filename: "datasworn-i18n-"+_.kebabCase(key) }));
const dataforgedItems = _.map(dataforged, (value, key) => ({ data: value, filename: "dataforged-i18n-"+_.kebabCase(key) }));

const tableRowPattern = new RegExp(/^(.*?)\/([0-9]{1,3})(-[0-9]{1,3})?#[A-z_-]+$/, "");
const parentPattern = new RegExp(/^(.*?)#.*$/, "");

[ ...dataswornItems, ...dataforgedItems ].forEach(item => {
const yamlData = yaml.dump(extractLocalizableStrings(item.data as unknown as Record<string,unknown>), {
lineWidth: -1,
quotingType: "\"",
noRefs: false,
sortKeys: (a:string,b:string) => {
if (!a.includes("/Oracles/")) {
return a.localeCompare(b);
}
const patternA = a.match(tableRowPattern);
const patternB = b.match(tableRowPattern);
const parentA = a.match(parentPattern);
const parentB = b.match(parentPattern);
const numbersA = _.compact(patternA?.map(item => Math.abs(parseInt(item)))) as [number, number];
const numbersB = _.compact(patternB?.map(item => Math.abs(parseInt(item)))) as [number, number];
if (patternA && patternB && patternA[1] === patternB[1] ) {
// slice out
// simplest way to grab the optional second number without shit getting weird with the hyphen is to pretend its a negative then get absolute value

// 1: b, a
// -1: a, b
// 0: no change.
return numbersA[0] - numbersB[0];
} else if (typeof numbersB[0] === "number") {
if (parentB && parentA?.[1].startsWith( parentB?.[1])) { return -1; }
return 1;
} else if (typeof numbersA[0] === "number") {
if (parentA && parentB?.[1].startsWith( parentA?.[1])) { return 1; }
return -1;
} else if
(typeof parentA?.[1] === "string" && typeof parentB?.[1] === "string" && parentA[1].startsWith( parentB[1])) {
return 1;
} else if (typeof parentA?.[1] === "string" && typeof parentB?.[1] === "string" && parentB[1].startsWith( parentA[1])) {
return -1;
} else {
return a.localeCompare(b);
}
}
});
fs.writeFileSync(item.filename+".yaml", yamlData);
});
Loading

0 comments on commit 66ab1a1

Please sign in to comment.