-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial draft of i18n string dump script
- Loading branch information
Showing
17 changed files
with
16,392 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 102 additions & 4 deletions
106
dataforged-tools/src/localization/extractLocaleStrings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.