Skip to content

Simplify adding/modifying translations in blueprints for 9.0.0 #31801

@yelhouti

Description

@yelhouti
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:

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions