-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Open
Description
Overview of the feature request
It should be much easier to handle new translations in blueprints.
With the current code, here is what I have to do:
import BaseApplicationGenerator from 'generator-jhipster/generators/base-application';
export default class extends BaseApplicationGenerator {
constructor(args, opts, features) {
super(args, opts, {
...features,
sbsBlueprint: true,
});
}
get [BaseApplicationGenerator.DEFAULT]() {
return this.asDefaultTaskGroup({
async updateTranslations({ application, entities }) {
const entitiesToWriteTranslationFor = entities.filter(entity => !entity.skipClient && !entity.builtInUser);
if (!application.userManagement?.skipClient) {
entitiesToWriteTranslationFor.push(application.userManagement);
}
const { enableTranslation, languages, frontendAppName } = application;
if (!enableTranslation) return;
this.queueTask({
method: async () => {
for (const entity of entitiesToWriteTranslationFor) {
const { entityTranslationKey, readOnly } = entity;
if (!readOnly) {
for (const lang of languages) {
this.editEntityTranslation( application.translations, frontendAppName, entityTranslationKey, lang);
}
}
}
},
taskName: 'updateTranslations',
queueName: 'jhipster:loadingTranslations',
once: true,
});
},
});
}
get [BaseApplicationGenerator.WRITING_ENTITIES]() {
return this.asWritingEntitiesTaskGroup({
async updateTranslations({ application, entities }) {
const entitiesToWriteTranslationFor = entities.filter(entity => !entity.skipClient && !entity.builtInUser);
if (!application.userManagement?.skipClient) {
entitiesToWriteTranslationFor.push(application.userManagement);
}
const { clientI18nDir, enableTranslation, languages, frontendAppName } = application;
if (!enableTranslation) return;
for (const entity of entitiesToWriteTranslationFor) {
const { entityTranslationKey, readOnly } = entity;
if (!readOnly) {
for (const lang of languages) {
const translationFile = `${clientI18nDir}${lang}/${entityTranslationKey}.json`;
this.editFile(translationFile, { assertModified: true }, content => {
const json = JSON.parse(content);
this.editEntityTranslation(json, frontendAppName, entityTranslationKey, lang);
return JSON.stringify(json, null, 2);
});
}
}
}
}
});
}
editEntityTranslation(json, frontendAppName, entityTranslationKey, lang) {
const home = json[frontendAppName][entityTranslationKey].home;
if (home.createOrEditLabel) {
if (lang === 'en') {
home.editLabel = home.createOrEditLabel.replace(/Create or edit/, 'Edit');
delete home.createOrEditLabel;
} else if (lang === 'fr') {
home.editLabel = home.createOrEditLabel.replace(/Créer ou éditer/, 'Editer');
delete home.createOrEditLabel;
}
}
}
}This is complicated because of the way translations are loaded:
generator-jhipster/generators/client/generators/i18n/generator.ts
Lines 112 to 118 in a0bd3c1
| const listener = (filePath: string): void => { | |
| if (filter(filePath)) { | |
| this.env.sharedFs.removeListener('change', listener); | |
| this.queueLoadLanguages({ clientI18nDir, enableTranslation, nativeLanguage, fallbackLanguage }); | |
| } | |
| }; | |
| this.env.sharedFs.on('change', listener); |
@mshima since you are the author, I am pinging you, hopping this doesn't waste other blueprints authors time. and Thanks a again for all the work you do on the project.
Motivation for or Use Case
Make translations for blueprints easier to implement.
Related issues or PR
- Checking this box is mandatory (this is just to show you read everything)