Skip to content

Commit

Permalink
Merge pull request #23488 from mshima/skip_ci-angular-translate2
Browse files Browse the repository at this point in the history
Rework translation process.
  • Loading branch information
DanielFran authored Sep 12, 2023
2 parents 1551f52 + a9efe81 commit 26fc6d2
Show file tree
Hide file tree
Showing 55 changed files with 764 additions and 684 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,
});
}
10 changes: 4 additions & 6 deletions generators/angular/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,10 @@ export default class AngularGenerator extends BaseApplicationGenerator {
cleanupOldFilesTask,
writeFiles,
queueTranslateTransform({ control, application }) {
if (!application.enableTranslation) {
this.queueTransformStream(translateAngularFilesTransform(control.getWebappTranslation), {
name: 'translating webapp',
streamOptions: { filter: file => isFilePending(file) && isTranslatedAngularFile(file) },
});
}
this.queueTransformStream(translateAngularFilesTransform(control.getWebappTranslation, application.enableTranslation), {
name: 'translating webapp',
streamOptions: { filter: file => isFilePending(file) && isTranslatedAngularFile(file) },
});
},
});
}
Expand Down
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
38 changes: 26 additions & 12 deletions generators/angular/support/translate-angular.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import { passthrough } from '@yeoman/transform';
import { Minimatch } from 'minimatch';

import { createJhiTransformTranslateReplacer, createJhiTransformTranslateStringifyReplacer } from '../../languages/support/index.mjs';

const PLACEHOLDER_REGEX = /(?:placeholder|title)=['|"](\{\{\s?['|"]([a-zA-Z0-9.\-_]+)['|"]\s?\|\s?translate\s?\}\})['|"]/.source;

const JHI_TRANSLATE_REGEX = /(\n?\s*[a-z][a-zA-Z]*Translate="[a-zA-Z0-9 +{}'_!?.]+")/.source;
Expand Down Expand Up @@ -102,27 +104,39 @@ function replaceErrorMessage(getWebappTranslation, content) {
* @type {import('../generator-base.js').EditFileCallback}
* @this {import('../generator-base.js')}
*/
// eslint-disable-next-line import/prefer-default-export
export const createTranslationReplacer = getWebappTranslation =>
function replaceAngularTranslations(content, filePath) {
export const createTranslationReplacer = (getWebappTranslation, enableTranslation) => {
const htmlJhiTranslateReplacer = createJhiTransformTranslateReplacer(getWebappTranslation);
const htmlJhiTranslateStringifyReplacer = createJhiTransformTranslateStringifyReplacer(getWebappTranslation);
return function replaceAngularTranslations(content, filePath) {
if (/\.html$/.test(filePath)) {
content = content.replace(new RegExp(TRANSLATE_REGEX, 'g'), '');
content = replacePlaceholders(getWebappTranslation, content);
if (!enableTranslation) {
content = content.replace(new RegExp(TRANSLATE_REGEX, 'g'), '');
content = replacePlaceholders(getWebappTranslation, content);
}
content = htmlJhiTranslateReplacer(content);
content = htmlJhiTranslateStringifyReplacer(content);
}
if (/(:?route|module)\.ts$/.test(filePath)) {
content = replacePageTitles(getWebappTranslation, content);
if (/\.ts$/.test(filePath)) {
content = htmlJhiTranslateReplacer(content);
content = htmlJhiTranslateStringifyReplacer(content);
}
if (/error\.route\.ts$/.test(filePath)) {
content = replaceErrorMessage(getWebappTranslation, content);
if (!enableTranslation) {
if (/(:?route|module)\.ts$/.test(filePath)) {
content = replacePageTitles(getWebappTranslation, content);
}
if (/error\.route\.ts$/.test(filePath)) {
content = replaceErrorMessage(getWebappTranslation, content);
}
}
return content;
};
};

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 => {
const translate = createTranslationReplacer(getWebappTranslation);
const translateAngularFilesTransform = (getWebappTranslation, enableTranslation) => {
const translate = createTranslationReplacer(getWebappTranslation, enableTranslation);
return passthrough(file => {
file.contents = Buffer.from(translate(file.contents.toString(), file.path));
});
Expand Down
33 changes: 23 additions & 10 deletions generators/angular/support/translate-angular.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,20 @@ describe('generator - angular - transform', () => {
describe('.html files', () => {
const extension = '.html';

it('should replace jhiTranslate attribute', () => {
it('should translate __jhiTransformTranslate__ function', () => {
const body = `
<h1>__jhiTransformTranslate__('activate.title1')</h1>
<h1>__jhiTransformTranslate__('activate.title2')</h1>
`;
expect(replaceAngularTranslations(body, extension)).toMatchInlineSnapshot(`
"
<h1>translated-value-activate.title1-0</h1>
<h1>translated-value-activate.title2-1</h1>
"
`);
});

it('should remove jhiTranslate attribute', () => {
const body = `
<h1 jhiTranslate="activate.title1">activate.title1</h1>
<h1 jhiTranslate="activate.title2">activate.title2</h1>
Expand All @@ -46,7 +59,7 @@ describe('generator - angular - transform', () => {
`);
});

it('should replace [translateValues] attribute', () => {
it('should remove [translateValues] attribute', () => {
const body = `
<h1 [translateValues]="{ max: 50 }">translate-values1</h1>
<h1 [translateValues]="{ max: 50 }">translate-values2</h1>
Expand All @@ -59,7 +72,7 @@ describe('generator - angular - transform', () => {
`);
});

it('should replace neasted [translateValues] attribute', () => {
it('should remove neasted [translateValues] attribute', () => {
const body = `
<h1 [translateValues]="{ max: 50 }"><span [translateValues]="{ max: 50 }">translate-values1</span></h1>
<h1 [translateValues]="{ max: 50 }"><span [translateValues]="{ max: 50 }">translate-values2</span></h1>
Expand All @@ -72,7 +85,7 @@ describe('generator - angular - transform', () => {
`);
});

it('should replace [translateValues] attribute with any character', () => {
it('should remove [translateValues] attribute with any character', () => {
const body = `
<h1 [translateValues]="{ %79kma#@ }">translate-values1</h1>
<h1 [translateValues]="{ %79kma#@ }">translate-values2</h1>
Expand All @@ -85,7 +98,7 @@ describe('generator - angular - transform', () => {
`);
});

it('should replace neasted [translateValues] attribute', () => {
it('should remove neasted [translateValues] attribute', () => {
const body = `
<h1 [translateValues]="{ max: 50 }"><span [translateValues]="{ max: 50 }">translate-values1</span></h1>
<h1 [translateValues]="{ max: 20 }"><span [translateValues]="{ max: 20 }">translate-values2</span></h1>
Expand All @@ -98,7 +111,7 @@ describe('generator - angular - transform', () => {
`);
});

it('should replace placeholder attribute value with translated value', () => {
it('should remove placeholder attribute value with translated value', () => {
const body = `
<input placeholder="{{ 'global.form.currentpassword.placeholder1' | translate }}"/>
<input placeholder="{{ 'global.form.currentpassword.placeholder2' | translate }}"/>
Expand All @@ -111,7 +124,7 @@ describe('generator - angular - transform', () => {
`);
});

it('should replace title attribute value with translated value', () => {
it('should remove title attribute value with translated value', () => {
const body = `
<input title="{{ 'global.form.currentpassword.title1' | translate }}"/>
<input title="{{ 'global.form.currentpassword.title2' | translate }}"/>
Expand All @@ -128,7 +141,7 @@ describe('generator - angular - transform', () => {
describe('.route.ts files', () => {
const extension = '.route.ts';

it('should replace title fields with translation values', () => {
it('should translate title fields with translation values', () => {
const body = `
title: 'activate.title1',
title: 'activate.title2',
Expand All @@ -145,7 +158,7 @@ title: 'translated-value-activate.title2-1',
describe('.module.ts files', () => {
const extension = '.module.ts';

it('should replace title fields with translation values', () => {
it('should translate title fields with translation values', () => {
const body = `
title: 'activate.title1',
title: 'activate.title2',
Expand All @@ -162,7 +175,7 @@ title: 'translated-value-activate.title2-1',
describe('.error.route.ts files', () => {
const extension = '.error.route.ts';

it('should replace pageTitle fields with translation values', () => {
it('should translate pageTitle fields with translation values', () => {
const body = `
errorMessage: 'activate.title1',
errorMessage: 'activate.title2',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
<div>
<div class="d-flex justify-content-center">
<div class="col-md-8">
<h1 <%= jhiPrefix %>Translate="activate.title"><%- getWebappTranslation('activate.title') %></h1>
<h1 <%= jhiPrefix %>Translate="activate.title">__jhiTransformTranslate__('activate.title')</h1>

<div class="alert alert-success" *ngIf="success">
<span <%= jhiPrefix %>Translate="activate.messages.success"><%- getWebappTranslation('activate.messages.success') %></span>
<a class="alert-link" routerLink="/login" <%= jhiPrefix %>Translate="global.messages.info.authenticated.link"><%- getWebappTranslation('global.messages.info.authenticated.link') %></a>.
<span <%= jhiPrefix %>Translate="activate.messages.success">__jhiTransformTranslate__('activate.messages.success')</span>
<a class="alert-link" routerLink="/login" <%= jhiPrefix %>Translate="global.messages.info.authenticated.link">__jhiTransformTranslate__('global.messages.info.authenticated.link')</a>.
</div>

<div class="alert alert-danger" *ngIf="error" <%= jhiPrefix %>Translate="activate.messages.error">
<%- getWebappTranslation('activate.messages.error') %>
</div>
<div class="alert alert-danger" *ngIf="error" <%= jhiPrefix %>Translate="activate.messages.error">__jhiTransformTranslate__('activate.messages.error')</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,29 @@
<div>
<div class="d-flex justify-content-center">
<div class="col-md-4">
<h1 <%= jhiPrefix %>Translate="reset.finish.title"><%- getWebappTranslation('reset.finish.title') %></h1>
<h1 <%= jhiPrefix %>Translate="reset.finish.title">__jhiTransformTranslate__('reset.finish.title')</h1>

<div class="alert alert-danger" <%= jhiPrefix %>Translate="reset.finish.messages.keymissing" *ngIf="initialized && !key">
<%- getWebappTranslation('reset.finish.messages.keymissing') %>
</div>
<div class="alert alert-danger" <%= jhiPrefix %>Translate="reset.finish.messages.keymissing" *ngIf="initialized && !key">__jhiTransformTranslate__('reset.finish.messages.keymissing')</div>

<div class="alert alert-warning" *ngIf="key && !success">
<span <%= jhiPrefix %>Translate="reset.finish.messages.info"><%- getWebappTranslation('reset.finish.messages.info') %></span>
<span <%= jhiPrefix %>Translate="reset.finish.messages.info">__jhiTransformTranslate__('reset.finish.messages.info')</span>
</div>

<div class="alert alert-danger" *ngIf="error">
<span <%= jhiPrefix %>Translate="reset.finish.messages.error"><%- getWebappTranslation('reset.finish.messages.error') %></span>
<span <%= jhiPrefix %>Translate="reset.finish.messages.error">__jhiTransformTranslate__('reset.finish.messages.error')</span>
</div>

<div class="alert alert-success" *ngIf="success">
<span <%= jhiPrefix %>Translate="reset.finish.messages.success"><%- getWebappTranslation('reset.finish.messages.success') %> </span>
<a class="alert-link" routerLink="/login" <%= jhiPrefix %>Translate="global.messages.info.authenticated.link"><%- getWebappTranslation('global.messages.info.authenticated.link') %></a>.
<span <%= jhiPrefix %>Translate="reset.finish.messages.success">__jhiTransformTranslate__('reset.finish.messages.success')</span>
<a class="alert-link" routerLink="/login" <%= jhiPrefix %>Translate="global.messages.info.authenticated.link">__jhiTransformTranslate__('global.messages.info.authenticated.link')</a>.
</div>

<div class="alert alert-danger" *ngIf="doNotMatch" <%= jhiPrefix %>Translate="global.messages.error.dontmatch">
<%- getWebappTranslation('global.messages.error.dontmatch') %>
</div>
<div class="alert alert-danger" *ngIf="doNotMatch" <%= jhiPrefix %>Translate="global.messages.error.dontmatch">__jhiTransformTranslate__('global.messages.error.dontmatch')</div>

<div *ngIf="key && !success">
<form name="form" role="form" (ngSubmit)="finishReset()" [formGroup]="passwordForm">
<div class="mb-3">
<label class="form-label" for="newPassword" <%= jhiPrefix %>Translate="global.form.newpassword.label"><%- getWebappTranslation('global.form["newpassword.label"]') %></label>
<label class="form-label" for="newPassword" <%= jhiPrefix %>Translate="global.form.newpassword.label">__jhiTransformTranslate__('global.form["newpassword.label"]')</label>
<input
type="password"
class="form-control"
Expand All @@ -67,32 +63,26 @@
class="form-text text-danger"
*ngIf="passwordForm.get('newPassword')?.errors?.required"
<%= jhiPrefix %>Translate="global.messages.validate.newpassword.required"
>
<%- getWebappTranslation('global.messages.validate.newpassword.required') %>
</small>
>__jhiTransformTranslate__('global.messages.validate.newpassword.required')</small>

<small
class="form-text text-danger"
*ngIf="passwordForm.get('newPassword')?.errors?.minlength"
<%= jhiPrefix %>Translate="global.messages.validate.newpassword.minlength"
>
<%- getWebappTranslation('global.messages.validate.newpassword.minlength') %>
</small>
>__jhiTransformTranslate__('global.messages.validate.newpassword.minlength')</small>

<small
class="form-text text-danger"
*ngIf="passwordForm.get('newPassword')?.errors?.maxlength"
<%= jhiPrefix %>Translate="global.messages.validate.newpassword.maxlength"
>
<%- getWebappTranslation('global.messages.validate.newpassword.maxlength') %>
</small>
>__jhiTransformTranslate__('global.messages.validate.newpassword.maxlength')</small>
</div>

<<%= jhiPrefixDashed %>-password-strength-bar [passwordToCheck]="passwordForm.get('newPassword')!.value"></<%= jhiPrefixDashed %>-password-strength-bar>
</div>

<div class="mb-3">
<label class="form-label" for="confirmPassword" <%= jhiPrefix %>Translate="global.form.confirmpassword.label"><%- getWebappTranslation('global.form["confirmpassword.label"]') %></label>
<label class="form-label" for="confirmPassword" <%= jhiPrefix %>Translate="global.form.confirmpassword.label">__jhiTransformTranslate__('global.form["confirmpassword.label"]')</label>
<input
type="password"
class="form-control"
Expand All @@ -113,25 +103,19 @@
class="form-text text-danger"
*ngIf="passwordForm.get('confirmPassword')?.errors?.required"
<%= jhiPrefix %>Translate="global.messages.validate.confirmpassword.required"
>
<%- getWebappTranslation('global.messages.validate.confirmpassword.required') %>
</small>
>__jhiTransformTranslate__('global.messages.validate.confirmpassword.required')</small>

<small
class="form-text text-danger"
*ngIf="passwordForm.get('confirmPassword')?.errors?.minlength"
<%= jhiPrefix %>Translate="global.messages.validate.confirmpassword.minlength"
>
<%- getWebappTranslation('global.messages.validate.confirmpassword.minlength') %>
</small>
>__jhiTransformTranslate__('global.messages.validate.confirmpassword.minlength')</small>

<small
class="form-text text-danger"
*ngIf="passwordForm.get('confirmPassword')?.errors?.maxlength"
<%= jhiPrefix %>Translate="global.messages.validate.confirmpassword.maxlength"
>
<%- getWebappTranslation('global.messages.validate.confirmpassword.maxlength') %>
</small>
>__jhiTransformTranslate__('global.messages.validate.confirmpassword.maxlength')</small>
</div>
</div>

Expand All @@ -141,9 +125,7 @@
class="btn btn-primary"
<%= jhiPrefix %>Translate="reset.finish.form.button"
data-cy="submit"
>
<%- getWebappTranslation('reset.finish.form.button') %>
</button>
>__jhiTransformTranslate__('reset.finish.form.button')</button>
</form>
</div>
</div>
Expand Down
Loading

0 comments on commit 26fc6d2

Please sign in to comment.