Skip to content

Commit

Permalink
simplify translation process
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Sep 12, 2023
1 parent 6df88cd commit a9efe81
Show file tree
Hide file tree
Showing 25 changed files with 186 additions and 224 deletions.
9 changes: 2 additions & 7 deletions generators/angular/entity-files-angular.mts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,11 @@ export const angularFiles = {
],
};

export async function writeEntitiesFiles(
this: CoreGenerator,
{ application, entities, control }: GeneratorDefinition['writingEntitiesTaskParam'],
) {
await control.loadClientTranslations?.();

export async function writeEntitiesFiles(this: CoreGenerator, { application, entities }: GeneratorDefinition['writingEntitiesTaskParam']) {
for (const entity of entities.filter(entity => !entity.skipClient && !entity.builtIn)) {
await this.writeFiles({
sections: angularFiles,
context: { ...application, ...entity, getWebappTranslation: control.getWebappTranslation },
context: { ...application, ...entity },
});
}
}
Expand Down
9 changes: 2 additions & 7 deletions generators/angular/files-angular.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -464,16 +464,11 @@ export const files = {
],
};

export async function writeFiles({ application, control }) {
export async function writeFiles({ application }) {
if (!application.clientFrameworkAngular) return;

await control.loadClientTranslations?.();

await this.writeFiles({
sections: files,
context: {
...application,
getWebappTranslation: control.getWebappTranslation,
},
context: application,
});
}
15 changes: 2 additions & 13 deletions generators/angular/generator.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { shouldSupportFeatures, testBlueprintSupport } from '../../test/support/
import Generator from './index.mjs';
import { clientFrameworkTypes } from '../../jdl/jhipster/index.mjs';
import { CLIENT_MAIN_SRC_DIR } from '../generator-constants.mjs';
import BaseApplicationGenerator from '../base-application/index.mjs';
import { GENERATOR_ANGULAR } from '../generator-list.mjs';

const { snakeCase } = lodash;
Expand Down Expand Up @@ -77,16 +76,6 @@ const clientAdminFiles = clientSrcDir => [
`${clientSrcDir}app/admin/metrics/metrics.service.spec.ts`,
];

class MockedLanguagesGenerator extends BaseApplicationGenerator<any> {
get [BaseApplicationGenerator.PREPARING]() {
return {
mockTranslations({ control }) {
control.getWebappTranslation = () => 'translations';
},
};
}
}

describe(`generator - ${clientFramework}`, () => {
it('generator-list constant matches folder name', async () => {
await expect((await import('../generator-list.mjs'))[`GENERATOR_${snakeCase(generator).toUpperCase()}`]).toBe(generator);
Expand All @@ -108,8 +97,8 @@ describe(`generator - ${clientFramework}`, () => {
runResult = await helpers
.run(generatorFile)
.withJHipsterConfig(sampleConfig, entities)
.withGenerators([[MockedLanguagesGenerator, 'jhipster:languages']])
.withMockedGenerators(['jhipster:common']);
.withControl({ getWebappTranslation: () => 'translations' })
.withMockedGenerators(['jhipster:common', 'jhipster:languages']);
});

after(() => runResult.cleanup());
Expand Down
8 changes: 6 additions & 2 deletions generators/angular/support/translate-angular.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function replaceErrorMessage(getWebappTranslation, content) {
* @this {import('../generator-base.js')}
*/
export const createTranslationReplacer = (getWebappTranslation, enableTranslation) => {
const htmlJhiTranslateReplacer = createJhiTransformTranslateReplacer(getWebappTranslation, { escapeHtml: true });
const htmlJhiTranslateReplacer = createJhiTransformTranslateReplacer(getWebappTranslation);
const htmlJhiTranslateStringifyReplacer = createJhiTransformTranslateStringifyReplacer(getWebappTranslation);
return function replaceAngularTranslations(content, filePath) {
if (/\.html$/.test(filePath)) {
Expand All @@ -116,6 +116,10 @@ export const createTranslationReplacer = (getWebappTranslation, enableTranslatio
content = htmlJhiTranslateReplacer(content);
content = htmlJhiTranslateStringifyReplacer(content);
}
if (/\.ts$/.test(filePath)) {
content = htmlJhiTranslateReplacer(content);
content = htmlJhiTranslateStringifyReplacer(content);
}
if (!enableTranslation) {
if (/(:?route|module)\.ts$/.test(filePath)) {
content = replacePageTitles(getWebappTranslation, content);
Expand All @@ -128,7 +132,7 @@ export const createTranslationReplacer = (getWebappTranslation, enableTranslatio
};
};

const minimatch = new Minimatch('**/*{.html,.route.ts,.module.ts}');
const minimatch = new Minimatch('**/*{.html,.component.ts,.route.ts,.module.ts}');
export const isTranslatedAngularFile = file => minimatch.match(file.path);

const translateAngularFilesTransform = (getWebappTranslation, enableTranslation) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div>
<div class="d-flex justify-content-center">
<div class="col-md-8" *ngIf="account$ | async as account">
<h2 <%= jhiPrefix %>Translate="password.title" [translateValues]="{ username: account.login }">__jhiTransformTranslate__('password.title', { username: '{{ account.login }}' })</h2>
<h2 <%= jhiPrefix %>Translate="password.title" [translateValues]="{ username: account.login }">__jhiTransformTranslate__('password.title', { "username": "{{ account.login }}" })</h2>

<div class="alert alert-success" *ngIf="success" <%= jhiPrefix %>Translate="password.messages.success">__jhiTransformTranslate__('password.messages.success')</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
-%>
<div>
<h2 id="session-page-heading" <%= jhiPrefix %>Translate="sessions.title" [translateValues]="{ username: account.login }" *ngIf="account">__jhiTransformTranslate__('sessions.title', { username: '{{ account.login }}' })</h2>
<h2 id="session-page-heading" <%= jhiPrefix %>Translate="sessions.title" [translateValues]="{ username: account.login }" *ngIf="account">__jhiTransformTranslate__('sessions.title', { "username": "{{ account.login }}" })</h2>

<div class="alert alert-success" *ngIf="success" <%= jhiPrefix %>Translate="sessions.messages.success">__jhiTransformTranslate__('sessions.messages.success')</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div>
<div class="d-flex justify-content-center">
<div class="col-md-8">
<h2 <%= jhiPrefix %>Translate="settings.title" [translateValues]="{ username: settingsForm.value.login }" *ngIf="settingsForm.value.login">__jhiTransformTranslate__('settings.title', { username: '{{ settingsForm.value.login }}' })</h2>
<h2 <%= jhiPrefix %>Translate="settings.title" [translateValues]="{ username: settingsForm.value.login }" *ngIf="settingsForm.value.login">__jhiTransformTranslate__('settings.title', { "username": "{{ settingsForm.value.login }}" })</h2>

<div class="alert alert-success" *ngIf="success" <%= jhiPrefix %>Translate="settings.messages.success">__jhiTransformTranslate__('settings.messages.success')</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<div class="spinner-border"></div>
</div>

<p <%= jhiPrefix %>Translate="logs.nbloggers" [translateValues]="{ total: loggers.length }">__jhiTransformTranslate__('logs.nbloggers', { total: '{{ loggers.length }}' })</p>
<p <%= jhiPrefix %>Translate="logs.nbloggers" [translateValues]="{ total: loggers.length }">__jhiTransformTranslate__('logs.nbloggers', { "total": "{{ loggers.length }}" })</p>

<span <%= jhiPrefix %>Translate="logs.filter">__jhiTransformTranslate__('logs.filter')</span>
<input type="text" [(ngModel)]="filter" (ngModelChange)="filterAndSort()" class="form-control" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<div class="modal-body">
<<%= jhiPrefixDashed %>-alert-error></<%= jhiPrefixDashed %>-alert-error>

<p <%= jhiPrefix %>Translate="userManagement.delete.question" [translateValues]="{ login: user.login }">__jhiTransformTranslate__('userManagement.delete.question', { login: '{{ user.login }}' })</p>
<p <%= jhiPrefix %>Translate="userManagement.delete.question" [translateValues]="{ login: user.login }">__jhiTransformTranslate__('userManagement.delete.question', { "login": "{{ user.login }}" })</p>
</div>

<div class="modal-footer">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*ngIf="editForm.get('login')?.errors?.maxlength"
<%= jhiPrefix %>Translate="entity.validation.maxlength"
[translateValues]="{ max: 50 }"
>__jhiTransformTranslate__('entity.validation.maxlength', { max: '50' })</small>
>__jhiTransformTranslate__('entity.validation.maxlength', { "max": 50 })</small>

<small
class="form-text text-danger"
Expand All @@ -64,7 +64,7 @@
*ngIf="editForm.get('firstName')?.errors?.maxlength"
<%= jhiPrefix %>Translate="entity.validation.maxlength"
[translateValues]="{ max: 50 }"
>__jhiTransformTranslate__('entity.validation.maxlength', { max: '50' })</small>
>__jhiTransformTranslate__('entity.validation.maxlength', { "max": 50 })</small>
</div>
</div>

Expand All @@ -78,7 +78,7 @@
*ngIf="editForm.get('lastName')?.errors?.maxlength"
<%= jhiPrefix %>Translate="entity.validation.maxlength"
[translateValues]="{ max: 50 }"
>__jhiTransformTranslate__('entity.validation.maxlength', { max: '50' })</small>
>__jhiTransformTranslate__('entity.validation.maxlength', { "max": 50 })</small>
</div>
</div>

Expand All @@ -98,14 +98,14 @@
*ngIf="editForm.get('email')?.errors?.maxlength"
<%= jhiPrefix %>Translate="entity.validation.maxlength"
[translateValues]="{ max: 100 }"
>__jhiTransformTranslate__('entity.validation.maxlength', { max: '100' })</small>
>__jhiTransformTranslate__('entity.validation.maxlength', { "max": 100 })</small>

<small
class="form-text text-danger"
*ngIf="editForm.get('email')?.errors?.minlength"
<%= jhiPrefix %>Translate="entity.validation.minlength"
[translateValues]="{ min: 5 }"
>__jhiTransformTranslate__('entity.validation.minlength', { min: '5' })</small>
>__jhiTransformTranslate__('entity.validation.minlength', { "min": 5 })</small>

<small
class="form-text text-danger"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<div class="modal-body">
<<%= jhiPrefixDashed %>-alert-error></<%= jhiPrefixDashed %>-alert-error>
<p id="<%= jhiPrefixDashed %>-delete-<%= entityInstance %>-heading" <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.delete.question" [translateValues]="{ id: <%= entityInstance %>.<%= primaryKey.name %> }">__jhiTransformTranslate__(i18nKeyPrefix + '.delete.question', { id: '{{ ' + entityInstance + '.' + primaryKey.name + ' }}' })</p>
<p id="<%= jhiPrefixDashed %>-delete-<%= entityInstance %>-heading" <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.delete.question" [translateValues]="{ id: <%= entityInstance %>.<%= primaryKey.name %> }">__jhiTransformTranslate__('<%- i18nKeyPrefix %>.delete.question', { "id": "{{ <%- entityInstance%>.<%- primaryKey.name%> }}" })</p>
</div>

<div class="modal-footer">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="d-flex justify-content-center">
<div class="col-8">
<div *ngIf="<%= entityInstance %>">
<h2 data-cy="<%= entityInstance %>DetailsHeading"><span <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.detail.title">__jhiTransformTranslate__(i18nKeyPrefix + '.detail.title')</span></h2>
<h2 data-cy="<%= entityInstance %>DetailsHeading"><span <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.detail.title">__jhiTransformTranslate__('<%- i18nKeyPrefix %>.detail.title')</span></h2>

<hr>

Expand All @@ -32,7 +32,7 @@
const fieldName = field.fieldName;
const fieldType = field.fieldType;
_%>
<dt><span <%= jhiPrefix %>Translate="<%= field.fieldTranslationKey %>"<% if (field.javadoc) { if (enableTranslation) { %> [ngbTooltip]="'<%= i18nKeyPrefix %>.help.<%= fieldName %>' | translate"<% } else { %> ngbTooltip="<%= field.javadoc %>"<% } } %>>__jhiTransformTranslate__(field.fieldTranslationKey)</span></dt>
<dt><span <%= jhiPrefix %>Translate="<%= field.fieldTranslationKey %>"<% if (field.javadoc) { if (enableTranslation) { %> [ngbTooltip]="'<%= i18nKeyPrefix %>.help.<%= fieldName %>' | translate"<% } else { %> ngbTooltip="<%= field.javadoc %>"<% } } %>>__jhiTransformTranslate__('<%- field.fieldTranslationKey %>')</span></dt>
<dd>
<%_ if (field.fieldIsEnum) { _%>
<%# TODO: import enum and use its key as label _%>
Expand Down Expand Up @@ -71,7 +71,7 @@ for (const relationship of relationships.filter(rel => !rel.otherEntityIsEmbedde
const otherEntityField = relationship.otherEntityField;
if (ownerSide) {
_%>
<dt><span <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.<%= relationshipName %>">__jhiTransformTranslate__(i18nKeyPrefix + '.' + relationshipName)</span></dt>
<dt><span <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.<%= relationshipName %>">__jhiTransformTranslate__('<%- i18nKeyPrefix %>.<%- relationshipName %>')</span></dt>
<dd>
<%_ if (relationship.otherEntityUser) { _%>
<%_ if (relationship.collection) { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
-%>
<div>
<h2 id="page-heading" data-cy="<%= entityClass %>Heading">
<span <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.home.title">__jhiTransformTranslate__(i18nKeyPrefix + '.home.title')</span>
<span <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.home.title">__jhiTransformTranslate__('<%- i18nKeyPrefix %>.home.title')</span>

<div class="d-flex justify-content-end">
<button class="btn btn-info me-2" (click)="load()" [disabled]="isLoading">
<fa-icon icon="sync" [spin]="isLoading"></fa-icon>
<span <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.home.refreshListLabel">__jhiTransformTranslate__(i18nKeyPrefix + '.home.refreshListLabel')</span>
<span <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.home.refreshListLabel">__jhiTransformTranslate__('<%- i18nKeyPrefix %>.home.refreshListLabel')</span>
</button>
<%_ if (!readOnly) { _%>
<button id="jh-create-entity" data-cy="entityCreateButton" class="btn btn-primary jh-create-entity create-<%= entityUrl %>" [routerLink]="['/<%= entityPage %>/new']">
<fa-icon icon="plus"></fa-icon>
<span <% if (searchEngineAny) { %>class="hidden-sm-down" <% } %> <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.home.createLabel">__jhiTransformTranslate__(i18nKeyPrefix + '.home.createLabel')</span>
<span <% if (searchEngineAny) { %>class="hidden-sm-down" <% } %> <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.home.createLabel">__jhiTransformTranslate__('<%- i18nKeyPrefix %>.home.createLabel')</span>
</button>
<%_ } _%>
</div>
Expand All @@ -43,7 +43,7 @@
<form name="searchForm" class="row row-cols-sm-auto align-items-center">
<div class="col-sm-12">
<div class="input-group w-100 mt-3">
<label class="visually-hidden" for="currentSearch" <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.home.search">__jhiTransformTranslate__(i18nKeyPrefix + '.home.search')</label>
<label class="visually-hidden" for="currentSearch" <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.home.search">__jhiTransformTranslate__('<%- i18nKeyPrefix %>.home.search')</label>
<input type="text" class="form-control" [(ngModel)]="currentSearch" id="currentSearch" name="currentSearch" placeholder="<% if (enableTranslation) { %>{{ '<%= i18nKeyPrefix %>.home.search' | translate }}<% } else { %>Query<% } %>">
<button class="btn btn-info" (click)="search(currentSearch)">
Expand All @@ -63,7 +63,7 @@
<%_ } _%>

<div class="alert alert-warning" id="no-result" *ngIf="<%= entityInstancePlural %>?.length === 0">
<span <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.home.notFound">__jhiTransformTranslate__(i18nKeyPrefix + '.home.notFound')</span>
<span <%= jhiPrefix %>Translate="<%= i18nKeyPrefix %>.home.notFound">__jhiTransformTranslate__('<%- i18nKeyPrefix %>.home.notFound')</span>
</div>

<div class="table-responsive table-entities" id="entities" *ngIf="<%= entityInstancePlural %> && <%= entityInstancePlural %>.length > 0">
Expand All @@ -73,7 +73,7 @@
<%_ for (const field of fields.filter(field => !field.hidden)) { _%>
<th scope="col" <%= jhiPrefix %>SortBy="<%= field.fieldName %>">
<div class="d-flex">
<span <%= jhiPrefix %>Translate="<%= field.fieldTranslationKey %>">__jhiTransformTranslate__(field.fieldTranslationKey)</span>
<span <%= jhiPrefix %>Translate="<%= field.fieldTranslationKey %>">__jhiTransformTranslate__('<%- field.fieldTranslationKey %>')</span>
<%_ if (!field.transient) { _%>
<fa-icon class="p-1" <% if (searchEngineAny && !field.fieldTypeBoolean && !field.fieldTypeNumeric && !field.fieldTypeTemporal) { %>*ngIf="!currentSearch" <% } %>icon="sort"></fa-icon>
<%_ } _%>
Expand All @@ -88,7 +88,7 @@
_%>
<th scope="col" <%= jhiPrefix %>SortBy="<%= relationship.relationshipName + (fieldName) %>">
<div class="d-flex">
<span <%= jhiPrefix %>Translate="<%= `${i18nKeyPrefix}.${relationship.relationshipName}` %>">__jhiTransformTranslate__(i18nKeyPrefix + '.' + relationship.relationshipName)</span>
<span <%= jhiPrefix %>Translate="<%= `${i18nKeyPrefix}.${relationship.relationshipName}` %>">__jhiTransformTranslate__('<%- i18nKeyPrefix %>.<%- relationship.relationshipName %>')</span>
<fa-icon class="p-1" icon="sort"></fa-icon>
</div>
</th>
Expand Down Expand Up @@ -175,7 +175,7 @@ _%>
class="btn btn-info btn-sm"
data-cy="filterOtherEntityButton">
<fa-icon icon="eye"></fa-icon>
<span class="d-none d-md-inline" <%= jhiPrefix %>Translate="entity.action.show" [translateValues]="{ otherEntity: ('<%= otherEntityTranslationKey %>' | translate) }">__jhiTransformTranslate__('entity.action.show', {'otherEntity' : relationship.otherEntity.entityClassHumanized })</span>
<span class="d-none d-md-inline" <%= jhiPrefix %>Translate="entity.action.show" [translateValues]="{ otherEntity: ('<%= otherEntityTranslationKey %>' | translate) }">__jhiTransformTranslate__('entity.action.show', { "otherEntity": "<%- relationship.otherEntity.entityClassHumanized %>" })</span>
</button>
<%_ } _%>
<%_ } _%>
Expand Down
Loading

0 comments on commit a9efe81

Please sign in to comment.