Skip to content

Commit 33268b1

Browse files
antoineawsBrian Mycroft
authored andcommitted
Automated Docs Fix - Same Properties
1 parent ba2912f commit 33268b1

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

src/lib/docs-gen/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"gen-i18n-from-json-schema-en": "pnpx ts-node scripts/generate-translation-file.ts en ./output-schema/schema-en.json ./output-translations/translation-en.json",
1616
"gen-i18n-from-json-schema-fr": "pnpx ts-node scripts/generate-translation-file.ts fr ./output-schema/schema-fr.json ./output-translations/translation-fr.json",
1717
"gen-typedoc-all": "pnpm run gen-typedoc-en && pnpm run gen-typedoc-fr",
18-
"gen-typedoc-en": "rm -r ./output-docs/en; typedoc ./output-ts/en --name 'AWS SEA Config Docs' --hideGenerator --excludePrivate --localize en --templateStrings ./output-translations/translation-en.json --theme ./src/typedoc-theme --out ./output-docs/en --readme ./src/README-en.md",
19-
"gen-typedoc-fr": "rm -r ./output-docs/fr; typedoc ./output-ts/fr --name 'Documents Config AWS SEA' --hideGenerator --excludePrivate --localize fr --templateStrings ./output-translations/translation-fr.json --theme ./src/typedoc-theme --out ./output-docs/fr --readme ./src/README-fr.md",
18+
"gen-typedoc-en": "rm -r ./output-docs/en; lang=en typedoc ./output-ts/en --name 'AWS SEA Config Docs' --hideGenerator --excludePrivate --localize en --templateStrings ./output-translations/translation-en.json --theme ./src/typedoc-theme --out ./output-docs/en --readme ./src/README-en.md",
19+
"gen-typedoc-fr": "rm -r ./output-docs/fr; lang=fr typedoc ./output-ts/fr --name 'Documents Config AWS SEA' --hideGenerator --excludePrivate --localize fr --templateStrings ./output-translations/translation-fr.json --theme ./src/typedoc-theme --out ./output-docs/fr --readme ./src/README-fr.md",
2020
"gen-docs": "pnpm run gen-json-schema-all && pnpm run gen-ts-from-json-schema-all && pnpm run gen-i18n-from-json-schema-all && pnpm run gen-typedoc-all",
2121
"build": "pnpm run gen-docs"
2222
},

src/lib/docs-gen/scripts/generate-translation-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let baseTranslationParsed = JSON.parse(baseTranslationRaw);
2424

2525
// Extract translations
2626
const translationHelper = new TranslationExtractHelper(languageCode);
27-
translationHelper.iterate(schemaParsed);
27+
translationHelper.iterate(schemaParsed, schemaParsed.title);
2828

2929
// Merge base and extracted translations
3030
for(const prop in baseTranslationParsed[languageCode]){

src/lib/docs-gen/src/helpers/translation-extract-helper.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,30 @@ export class TranslationExtractHelper {
99
this.typeDocTranslation[`${selectedLanguage}`] = {};
1010
}
1111

12-
public iterate(obj: any) {
12+
public iterate(obj: any, parentProperty: string) {
1313
for (const property in obj.fields) {
1414
const propertyObject = obj.fields[property];
1515

1616
const titleTranslated = this.translate('Title', this.selectedLanguage);
1717
const titleField = this.generateTitle(propertyObject.title, property);
1818
const descriptionTranslated = this.translate('Description', this.selectedLanguage);
19-
this.typeDocTranslation[`${this.selectedLanguage}`]["i18n-" + property] =
19+
const computedProperty = this.generateProperty(property, parentProperty);
20+
this.typeDocTranslation[`${this.selectedLanguage}`][`${computedProperty}`] =
2021
`<b>${titleTranslated}:</b> ${titleField}<br/><b>${descriptionTranslated}:</b> ${propertyObject.description}`;
2122
}
2223
if (obj.properties) {
2324
for (const property in obj.properties) {
24-
this.iterate(obj.properties[property]);
25+
this.iterate(obj.properties[property], obj.properties[property].title);
2526
}
2627
}
2728
if (obj.additionalProperties) {
28-
this.iterate(obj.additionalProperties);
29+
this.iterate(obj.additionalProperties, obj.additionalProperties.title);
2930
}
3031

3132
}
3233
private generateTitle(title: string, propertyName: string) {
3334
if (title && title.trim().length > 0) return title;
34-
35+
3536
const newTitle = `${propertyName}`
3637
.replace(new RegExp(/[^\w]/, 'g'), ' ') //Remove non-word characters such as '-'
3738
.replace(
@@ -47,4 +48,14 @@ export class TranslationExtractHelper {
4748
if (selectedLanguage === 'fr') return frenchDict.fr[key];
4849
else return englishDict.en[key];
4950
}
51+
private generateProperty(property: string, parentProperty: string) {
52+
let i18nProperty = "";
53+
if (!parentProperty) i18nProperty = `i18n-${property}`;
54+
else {
55+
const parentPropertyLowerCased =
56+
parentProperty.replace(/[\W_]+/g, "").toLowerCase(); // Replace non-alphanumeric
57+
i18nProperty = `i18n-${parentPropertyLowerCased}-${property}`;
58+
}
59+
return i18nProperty;
60+
}
5061
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
module.exports = {
4+
customLocalize: function (parent, property) {
5+
const selectedLanguage = process.env.lang;
6+
const languageDictionary = require(`../../../output-translations/translation-${selectedLanguage}.json`);
7+
let parentCleansed = "";
8+
let parentExists = false;
9+
if (parent) {
10+
parentExists = true;
11+
parentCleansed = parent.replace(/[\W_]+/g, "").toLowerCase(); // Replace non-alphanumeric
12+
13+
/**
14+
* The json-schema-to-typescript library generates duplicate classes such as ALBConfig and ALBConfig1
15+
* These classes are the same, so when retrieving translations from translation-en.json,
16+
* when parent key is ALBConfig1/2/3/etc , replace it with ALBConfig so the translation can
17+
* be retrieved. Below statements remove the last digit.
18+
*/
19+
const isLastCharacterANumber = !isNaN(parentCleansed.charAt(parentCleansed.length - 1));
20+
if (isLastCharacterANumber) parentCleansed = parentCleansed.slice(0, -1);
21+
}
22+
23+
let translation = "";
24+
if (parentExists) {
25+
translation = languageDictionary[selectedLanguage][`i18n-${parentCleansed}-${property}`];
26+
} else translation = englishDict.en[`i18n-${property}`];
27+
return translation;
28+
}
29+
}

src/lib/docs-gen/src/typedoc-theme/partials/member.declaration.hbs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020

2121
{{> member.sources}}
2222

23-
{{!-- <p>{{name}}</p> --}}
24-
{{#localize}}i18n-{{name}}{{/localize}}
23+
{{#customLocalize parent.name name}}{{/customLocalize}}
24+
{{!-- {{#localize}}i18n-{{name}}{{/localize}}--}}
2525
{{> comment}}
26-
{{!-- {{parent.name}} --}}
2726

2827
{{#if typeParameters}}
2928
<h4 class="tsd-type-parameters-title">{{#localize}}Type parameters{{/localize}}</h4>

0 commit comments

Comments
 (0)