Skip to content

Commit

Permalink
Merge pull request #27199 from mshima/translation
Browse files Browse the repository at this point in the history
fix translation with empty values
  • Loading branch information
DanielFran authored Sep 7, 2024
2 parents d4e73f2 + 341c9f6 commit 7b9ed5f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
7 changes: 6 additions & 1 deletion generators/angular/support/translate-angular.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('generator - angular - transform', () => {

beforeEach(() => {
let value = 0;
const testImpl = (key, data) => `translated-value-${key}-${data ? `${inspect(data)}-` : ''}${value++}`;
const testImpl = (key, data) => (key === 'blank' ? '' : `translated-value-${key}-${data ? `${inspect(data)}-` : ''}${value++}`);
replaceAngularTranslations = createTranslationReplacer(esmocha.fn().mockImplementation(testImpl), {
jhiPrefix: 'jhi',
enableTranslation: false,
Expand Down Expand Up @@ -189,6 +189,11 @@ translated-value-global.form.currentpassword.title2-0
`);
});

it('should replace __jhiTranslateTag__ with empty translated value', () => {
const body = `<tag>__jhiTranslateTag__('blank', { "username": "account()!.login" })</tag>`;
expect(replaceAngularTranslations(body, extension)).toMatchInlineSnapshot(`"<tag></tag>"`);
});

it('should replace __jhiTranslateTag__ with translation attribute and value', () => {
const body = `
<tag>__jhiTranslateTag__('global.form.currentpassword.title1', { "username": "account()!.login" })</tag>
Expand Down
20 changes: 8 additions & 12 deletions generators/angular/support/translate-angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ const TRANSLATE_REGEX = [JHI_TRANSLATE_REGEX, TRANSLATE_VALUES_REGEX].join('|');

export type ReplacerOptions = { jhiPrefix: string; enableTranslation: boolean };

function getTranslationValue(getWebappTranslation, key, data?) {
return getWebappTranslation(key, data) || undefined;
}

/**
* Replace translation key with translation values
*
Expand Down Expand Up @@ -69,7 +65,7 @@ function replaceTranslationKeysWithText(
// match is now the next match, in array form and our key is at index 1, index 1 is replace target.
const key = match[keyIndex];
const target = match[replacementIndex];
let translation = getTranslationValue(getWebappTranslation, key);
let translation = getWebappTranslation(key);
if (escape) {
translation = escape(translation, match);
}
Expand Down Expand Up @@ -134,7 +130,7 @@ const tagTranslation = (
const translatedValueInterpolate = parsedInterpolate
? Object.fromEntries(Object.entries(parsedInterpolate).map(([key, value]) => [key, `{{ ${value} }}`]))
: undefined;
const translatedValue = escapeHtmlTranslationValue(getTranslationValue(getWebappTranslation, key, translatedValueInterpolate));
const translatedValue = escapeHtmlTranslationValue(getWebappTranslation(key, translatedValueInterpolate));

if (enableTranslation) {
const translateValuesAttr = parsedInterpolate
Expand All @@ -160,7 +156,7 @@ const validationTagTranslation = (
if (!parsedInterpolate || Object.keys(parsedInterpolate).length === 0) {
throw new Error(`No interpolation values found for translation key ${key}, use __jhiTranslateTag__ instead.`);
}
const translatedValue = escapeHtmlTranslationValue(getTranslationValue(getWebappTranslation, key, parsedInterpolate));
const translatedValue = escapeHtmlTranslationValue(getWebappTranslation(key, parsedInterpolate));

if (enableTranslation) {
const translateValuesAttr = parsedInterpolate
Expand Down Expand Up @@ -189,7 +185,7 @@ const tagPipeTranslation = (
const translatedValueInterpolate = Object.fromEntries(
Object.entries(parsedInterpolate).map(([key, value]) => [key, getWebappTranslation(value)]),
);
const translatedValue = escapeHtmlTranslationValue(getTranslationValue(getWebappTranslation, key, translatedValueInterpolate));
const translatedValue = escapeHtmlTranslationValue(getWebappTranslation(key, translatedValueInterpolate));
if (enableTranslation) {
const translateValuesAttr = ` [translateValues]="{ ${Object.entries(parsedInterpolate)
.map(([key, value]) => `${key}: ('${value}' | translate)`)
Expand All @@ -213,7 +209,7 @@ const tagEnumTranslation = (
throw new Error(`Value is required for TagEnum ${key}.`);
}
const { value, fallback } = parsedInterpolate;
const translatedValue = `{{ ${JSON.stringify(getTranslationValue(getWebappTranslation, key))}[${value}]${fallback ? ` || ${fallback}` : ''} }}`;
const translatedValue = `{{ ${JSON.stringify(getWebappTranslation(key))}[${value}]${fallback ? ` || ${fallback}` : ''} }}`;
if (enableTranslation) {
return ` [${jhiPrefix}Translate]="'${key}.' + (${parsedInterpolate?.value})"${prefix}${translatedValue}${suffix}`;
}
Expand All @@ -234,7 +230,7 @@ const pipeTranslation = (
return `${prefix}{{ '${key}' | translate }}${suffix}`;
}

return `${prefix}${escapeHtmlTranslationValue(getTranslationValue(getWebappTranslation, key))}${suffix}`;
return `${prefix}${escapeHtmlTranslationValue(getWebappTranslation(key))}${suffix}`;
};

/**
Expand All @@ -245,7 +241,7 @@ const valueTranslation = (
_replacerOptions: ReplacerOptions,
{ filePath, key, prefix, suffix }: JHITranslateConverterOptions,
) => {
let translationValue = getTranslationValue(getWebappTranslation, key);
let translationValue = getWebappTranslation(key);
const fileExtension = extname(filePath);
if (fileExtension === '.html') {
translationValue = escapeHtmlTranslationValue(translationValue);
Expand All @@ -272,7 +268,7 @@ const pipeEnumTranslation = (
return `${prefix}{{ '${key}.' + ${value} | translate }}${suffix}`;
}

const translatedValue = `{{ ${JSON.stringify(getTranslationValue(getWebappTranslation, key))}[${value}]${fallback ? ` ?? ${fallback}` : ''} }}`;
const translatedValue = `{{ ${JSON.stringify(getWebappTranslation(key))}[${value}]${fallback ? ` ?? ${fallback}` : ''} }}`;
return `${prefix}${translatedValue}${suffix}`;
};

Expand Down
6 changes: 1 addition & 5 deletions generators/languages/support/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ export const escapeHtmlTranslationValue = (translation: string) =>

export const escapeTsTranslationValue = (translation: string) => translation.replace(/'/g, "\\'").replace(/\\/g, '\\\\');

function getTranslationValue(getWebappTranslation, key, data) {
return getWebappTranslation(key, data) || undefined;
}

export type TranslationReplaceOptions = {
keyPattern?: string;
interpolatePattern?: string;
Expand Down Expand Up @@ -77,7 +73,7 @@ export const replaceTranslationKeysWithText = (
}
}

const translation = getTranslationValue(getWebappTranslation, key, data);
const translation = getWebappTranslation(key, data);

let replacement = translation;
if (!replacement) {
Expand Down
6 changes: 1 addition & 5 deletions generators/react/support/translate-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ const INTERPOLATE_ATTRIBUTE = 'interpolate=\\{(?<interpolate>\\{[^\\}]+\\})\\}\\
const COMPONENT_ATTRIBUTE = 'component="(?<component>[^"]+)"\\s*';
const TRANSLATE_TAG = `<Translate\\s*(?:(?:${COMPONENT_ATTRIBUTE}|${INTERPOLATE_ATTRIBUTE}|${CONTENT_TYPE_ATTRIBUTE})+)>(?<translation>[\\s\\S]*?)<\\/Translate>`;

function getTranslationValue(getWebappTranslation, key, data) {
return getWebappTranslation(key, data) || undefined;
}

type Options = { keyPattern?: string; interpolatePattern?: string; wrapTranslation?: string | string[]; escapeHtml?: boolean };

const replaceTranslationKeysWithText = (
Expand Down Expand Up @@ -89,7 +85,7 @@ const replaceTranslationKeysWithText = (
}
}

const translation = getTranslationValue(getWebappTranslation, key, data);
const translation = getWebappTranslation(key, data);

let replacement = translation;
if (!replacement) {
Expand Down

0 comments on commit 7b9ed5f

Please sign in to comment.