Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(localization): add schema for localization.json files #1713

Merged
merged 1 commit into from
Apr 30, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export interface LocalizationMetadataAsMap {

/** Localization structure */
export interface LocalizationJsonValue {
/** Localtion Description */
/** Localization Description */
description: string;
/** Determine if the item can have multiple extensions */
dictionary?: boolean;
/** Determine if the value has to be overriden */
/** Determine if the value has to be overridden */
referenceData?: boolean;
/** Localization default value */
defaultValue?: string;
Expand All @@ -37,6 +37,10 @@ export interface LocalizationJsonFile {
[key: string]: LocalizationJsonValue;
}

type LocalizationJsonFileContent = LocalizationJsonFile & {
'$schema'?: string;
};

/** Localization file mapping */
export interface LocalizationFileMap {
[file: string]: {
Expand Down Expand Up @@ -142,16 +146,20 @@ export class LocalizationExtractor {
* Read a localization file
* @param locFile Path to the localization file
*/
private readLocalizationFile(locFile: string) {
return new Promise<LocalizationJsonFile>((resolve, reject) => fs.readFile(locFile, 'utf8', (err, data) => err ? reject(err) : resolve(JSON.parse(data))));
private async readLocalizationFile(locFile: string): Promise<LocalizationJsonFile> {
const content: LocalizationJsonFileContent = JSON.parse(await fs.promises.readFile(locFile, { encoding: 'utf-8' }));
if (content.$schema) {
delete content.$schema;
}
return content;
}

/**
* Read a metadata file
* @param metadataFile Path to the metadata file
*/
private readMetadataFile(metadataFile: string) {
return new Promise<LocalizationMetadata>((resolve, reject) => fs.readFile(metadataFile, 'utf8', (err, data) => err ? reject(err) : resolve(JSON.parse(data))));
private async readMetadataFile(metadataFile: string): Promise<LocalizationMetadata> {
return JSON.parse(await fs.promises.readFile(metadataFile, { encoding: 'utf-8' }));
}

/**
Expand Down
116 changes: 116 additions & 0 deletions packages/@o3r/localization/schemas/localization.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "LocalizationSchema",
"description": "Schema of Localization file",
"type": "object",
"additionalProperties": false,
"patternProperties": {
"\\$schema": true,
"^[a-zA-Z0-9.-]+": {
"type": "object",
"oneOf": [
kpanot marked this conversation as resolved.
Show resolved Hide resolved
{
"type": "object",
"additionalProperties": false,
"required": [
"description",
"defaultValue"
],
"properties": {
"defaultValue": {
"type": "string",
"description": "Localization default value"
},
"description": {
"type": "string",
"description": "Localization Description"
},
"dictionary": {
"type": "boolean",
"description": "Determine if the item can have multiple extensions",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this description really match what this field does ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just used the description from TS interface, it is not perfect but not wrong.
Basically it determines that the key can have a .<somthing> suffix.
If you have better, I take it :)

"const": false
},
"referenceData": {
"type": "boolean",
"description": "Determine if the value has to be overridden",
"default": false
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"description": "Tags used for filtering/categorizing"
}
}
},
{
"type": "object",
"additionalProperties": false,
"required": [
"description",
"dictionary"
],
"properties": {
"description": {
"type": "string",
"description": "Localization Description"
},
"dictionary": {
"type": "boolean",
"description": "Determine if the item can have multiple extensions",
"const": true
},
"referenceData": {
"type": "boolean",
"description": "Determine if the value has to be overridden",
"default": false
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"description": "Tags used for filtering/categorizing"
}
}
},
{
"type": "object",
"additionalProperties": false,
"required": [
"description",
"$ref"
],
"properties": {
"$ref": {
"type": "string",
"description": "Reference to other localization"
},
"description": {
"type": "string",
"description": "Localization Description"
},
"dictionary": {
"type": "boolean",
"description": "Determine if the item can have multiple extensions",
"default": false
},
"referenceData": {
"type": "boolean",
"description": "Determine if the value has to be overridden",
"default": false
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"description": "Tags used for filtering/categorizing"
}
}
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"$schema": "https://raw.githubusercontent.com/AmadeusITGroup/otter/main/packages/%40o3r/localization/schemas/localization.schema.json"
}
Loading