Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run build
- run: npm run lint
- run: npm run test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ npm-debug.log
/tests/workspace

transifex.auth
schemas/generated
34 changes: 31 additions & 3 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,25 @@ const defaultsSchema = require('../schemas/preset_defaults.json');
const deprecatedSchema = require('../schemas/deprecated.json');
const discardedSchema = require('../schemas/discarded.json');

/** @import { TranslationOptions } from "./translations.js" */

/** @typedef {{
inDirectory: string;
interimDirectory: string;
outDirectory: string;
sourceLocale: string;
taginfoProjectInfo: unknown,
processCategories: null | unknown;
processFields: null | unknown;
processPresets: null | unknown;
listReusedIcons: boolean;
}} BuildOptions */

/** @typedef {Partial<BuildOptions & TranslationOptions>} Options */

let _currBuild = null;

/** @param {Options} options */
function validateData(options) {
const START = '🔬 ' + styleText('yellow', 'Validating schema...');
const END = '👍 ' + styleText('green', 'schema okay');
Expand All @@ -34,6 +51,7 @@ function validateData(options) {
process.stdout.write('\n');
}

/** @param {Options} options */
function buildDev(options) {

if (_currBuild) return _currBuild;
Expand All @@ -51,6 +69,7 @@ function buildDev(options) {
process.stdout.write('\n');
}

/** @param {Options} options */
function buildDist(options) {

if (_currBuild) return _currBuild;
Expand All @@ -76,7 +95,8 @@ function buildDist(options) {
});
}

function processData(options, type) {
/** @internal @param {Options} options @returns {Options} */
export function getDefaultOptions(options) {
if (!options) options = {};
options = Object.assign({
inDirectory: 'data',
Expand All @@ -89,7 +109,15 @@ function processData(options, type) {
processPresets: null,
listReusedIcons: false
}, options);
return options;
}

/**
* @param {Options} options
* @param {'build-interim' | 'build-dist' | 'validate'} type
*/
function processData(options, type) {
options = getDefaultOptions(options);
const dataDir = './' + options.inDirectory;

// Translation strings
Expand Down Expand Up @@ -238,8 +266,8 @@ function generateCategories(dataDir, tstrings) {
return categories;
}


function generateFields(dataDir, tstrings, searchableFieldIDs) {
/** @internal */
export function generateFields(dataDir, tstrings, searchableFieldIDs) {
let fields = {};

fs.globSync(dataDir + '/fields/**/*.json', {
Expand Down
14 changes: 14 additions & 0 deletions lib/translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@ import fs from 'fs';
import fetch from 'node-fetch';
import YAML from 'js-yaml';
import { transifexApi } from '@transifex/api';
import { getExternalTranslations } from './units.js';


/** @typedef {{
translOrgId: string;
translProjectId: string;
translResourceIds: string[];
translReviewedOnly: false | string[];
inDirectory: string;
outDirectory: string;
sourceLocale: string;
}} TranslationOptions */

/** @param {Partial<TranslationOptions>} options */
function fetchTranslations(options) {

// Transifex doesn't allow anonymous downloading
Expand Down Expand Up @@ -202,6 +214,8 @@ function fetchTranslations(options) {
for (let code in allStrings) {
let obj = {};
obj[code] = allStrings[code] || {};
Object.assign(obj[code], getExternalTranslations(code, options));

fs.writeFileSync(`${outDir}/${code}.json`, JSON.stringify(obj, null, 4));
fs.writeFileSync(`${outDir}/${code}.min.json`, JSON.stringify(obj));
}
Expand Down
60 changes: 60 additions & 0 deletions lib/units.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// @ts-check
import { createRequire } from 'node:module';
import { generateFields, getDefaultOptions } from './build.js';

const require = createRequire(import.meta.url);

let cachedFields;

/**
* @param {string} locale
* @param {Partial<import('./translations.js').TranslationOptions>} options
*/
export function getExternalTranslations(locale, options) {
options = getDefaultOptions(options);
const language = locale.split('-')[0];

cachedFields ||= generateFields(options.inDirectory, { fields: {} }, {});

let localeData;
let languageData;
try {
localeData = require(`cldr-units-full/main/${locale}/units.json`);
} catch {
// ignore
}
try {
languageData = require(`cldr-units-full/main/${language}/units.json`);
} catch {
// ignore
}

if (!localeData && !languageData) {
// eslint-disable-next-line no-console
console.warn(`No CLDR data for ${language}`);
}

const output = {};

for (const field of Object.values(cachedFields)) {
if (!field.measurement) continue;

const { dimension, units } = field.measurement;

for (const unit in units) {
for (const type of ['long', 'narrow']) {
const translation =
localeData?.main[locale].units[type][`${dimension}-${unit}`]
.displayName ||
languageData?.main[language].units[type][`${dimension}-${unit}`]
.displayName;

output[dimension] ||= {};
output[dimension][unit] ||= {};
output[dimension][unit][type] = translation;
}
}
}

return { units: output };
}
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"exports": "./lib/index.js",
"dependencies": {
"@transifex/api": "^7.1.0",
"cldr-core": "^47.0.0",
"cldr-units-full": "^47.0.0",
"js-yaml": "^4.0.0",
"jsonschema": "^1.1.0",
"marky": "^1.2.4",
Expand All @@ -28,6 +30,7 @@
"node": ">=22"
},
"scripts": {
"build": "node scripts/build-schema.js",
"lint": "eslint lib",
"lint:fix": "eslint lib --fix",
"test": "NODE_OPTIONS=--experimental-vm-modules jest schema-builder.test.js"
Expand Down
36 changes: 33 additions & 3 deletions schemas/field.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://github.com/ideditor/schema-builder/raw/main/schemas/field.json",
"$id": "https://cdn.jsdelivr.net/npm/@ideditor/schema-builder/schemas/field.json",
"title": "Field",
"description": "A reusable form element for presets",
"type": "object",
Expand Down Expand Up @@ -67,6 +67,7 @@
"lanes",
"localized",
"manyCombo",
"measurement",
"multiCombo",
"networkCombo",
"number",
Expand Down Expand Up @@ -334,11 +335,11 @@
},
"additionalProperties": false
},
"urlFormat": {
"urlFormat": {
"description": "Permalink URL for `identifier` fields. Must contain a {value} placeholder",
"type": "string"
},
"pattern": {
"pattern": {
"description": "Regular expression that a valid `identifier` value is expected to match",
"type": "string"
},
Expand All @@ -358,6 +359,35 @@
"iconsCrossReference": {
"description": "A field can reference icons of another by using that field's identifier contained in brackets, like {field}.",
"type": "string"
},
"measurement": {
"type": "object",
"description": "defines the units of measurement that this field uses. Only supported by the 'measurement' field type.",
"properties": {
"dimension": {
"type": "string",
"description": "The corresponding 'dimension' from CLDR",
"$ref": "./generated/dimension.json"
},
"usage": {
"type": "string",
"description": "The corresponding 'usage' from CLDR"
},
"units": {
"type": "object",
"description": "Defines the permitted units. The key is the ID used by CLDR. The value is the value used in the OSM tag. If there are multiple values, the first one will be preferred. Use an empty string if the unit is not included in the OSM tag.",
"additionalProperties": {
"type": "string"
},
"minProperties": 1
}
},
"allOf": [
{ "$ref": "./generated/usage.json" },
{ "$ref": "./generated/units.json" }
],
"additionalItems": false,
"required": ["dimension", "usage", "units"]
}
},
"additionalProperties": false,
Expand Down
67 changes: 67 additions & 0 deletions scripts/build-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// @ts-check
import { promises as fs } from 'node:fs';
import { join } from 'node:path';
import unitPreference from 'cldr-core/supplemental/unitPreferenceData.json' with { type: 'json' };
import unitTranslations from 'cldr-units-full/main/en/units.json' with { type: 'json' };

// this file auto-generates the files in schemas/generated/* based on npm dependencies

const dimension = {
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'https://cdn.jsdelivr.net/npm/@ideditor/schema-builder/schemas/generated/dimension.json',

enum: Object.keys(unitPreference.supplemental.unitPreferenceData),
};

const usage = {
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'https://cdn.jsdelivr.net/npm/@ideditor/schema-builder/schemas/generated/usage.json',

allOf: Object.entries(unitPreference.supplemental.unitPreferenceData).map(
([key, value]) => ({
if: { properties: { dimension: { const: key } } },
then: { properties: { usage: { enum: Object.keys(value) } } },
}),
),
};

const units = {
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'https://cdn.jsdelivr.net/npm/@ideditor/schema-builder/schemas/generated/units.json',

allOf: dimension.enum.map((dimension) => {
const values = Object.keys(unitTranslations.main.en.units.long)
.filter((key) => key.startsWith(`${dimension}-`))
.map((key) => key.split('-').slice(1).join('-'));

return {
if: { properties: { dimension: { const: dimension } } },
then: {
properties: {
units: {
additionalProperties: false,
properties: Object.fromEntries(
values.map((value) => [
value,
{ type: 'array', items: { type: 'string' }, minItems: 1 },
]),
),
},
},
},
};
}),
};

const files = { dimension, usage, units };

const generatedFolder = join(import.meta.dirname, '../schemas/generated');
await fs.mkdir(generatedFolder, { recursive: true });

for (const key in files) {
// eslint-disable-next-line no-await-in-loop
await fs.writeFile(
join(generatedFolder, `${key}.json`),
JSON.stringify(files[key], null, 4),
);
}