fix(editor): simplifying localisation functions

This commit is contained in:
Csaba Tuncsik 2022-11-03 11:45:06 +01:00
parent 25d4a5077d
commit 3d93ccf0cf
7 changed files with 28 additions and 109 deletions

View file

@ -14,15 +14,12 @@
> >
<el-checkbox <el-checkbox
v-if="credentialPermissions.updateNodeAccess" v-if="credentialPermissions.updateNodeAccess"
:label="$locale.headerText({ :label="$locale.headerText(`headers.${shortNodeType(node)}.displayName`, node.displayName)"
key: `headers.${shortNodeType(node)}.displayName`,
fallback: node.displayName,
})"
:value="!!nodeAccess[node.name]" :value="!!nodeAccess[node.name]"
@change="(val) => onNodeAccessChange(node.name, val)" @change="(val) => onNodeAccessChange(node.name, val)"
/> />
<n8n-text v-else> <n8n-text v-else>
{{ $locale.headerText({ key: `headers.${shortNodeType(node)}.displayName`, fallback: node.displayName })}} {{ $locale.headerText(`headers.${shortNodeType(node)}.displayName`, node.displayName)}}
</n8n-text> </n8n-text>
</div> </div>
</el-col> </el-col>

View file

@ -252,10 +252,7 @@ export default mixins(
}, },
nodeTitle (): string { nodeTitle (): string {
if (this.data.name === 'Start') { if (this.data.name === 'Start') {
return this.$locale.headerText({ return this.$locale.headerText(`headers.start.displayName`, 'Start');
key: `headers.start.displayName`,
fallback: 'Start',
});
} }
return this.data.name; return this.data.name;

View file

@ -9,11 +9,7 @@
<div> <div>
<div :class="$style.details"> <div :class="$style.details">
<span :class="$style.name"> <span :class="$style.name">
{{ $locale.headerText({ {{ $locale.headerText(`headers.${shortNodeType}.displayName`, nodeType.displayName) }}
key: `headers.${shortNodeType}.displayName`,
fallback: nodeType.displayName,
})
}}
</span> </span>
<span v-if="isTrigger" :class="$style['trigger-icon']"> <span v-if="isTrigger" :class="$style['trigger-icon']">
<trigger-icon /> <trigger-icon />
@ -30,11 +26,7 @@
</n8n-tooltip> </n8n-tooltip>
</div> </div>
<div :class="$style.description"> <div :class="$style.description">
{{ $locale.headerText({ {{ $locale.headerText(`headers.${shortNodeType}.description`, nodeType.description) }}
key: `headers.${shortNodeType}.description`,
fallback: nodeType.description,
})
}}
</div> </div>
<div :class="$style['draggable-data-transfer']" ref="draggableDataTransfer" /> <div :class="$style['draggable-data-transfer']" ref="draggableDataTransfer" />

View file

@ -171,11 +171,7 @@ export default mixins(externalHooks, nodeHelpers).extend({
nodeTypeName(): string { nodeTypeName(): string {
if (this.nodeType) { if (this.nodeType) {
const shortNodeType = this.$locale.shortNodeType(this.nodeType.name); const shortNodeType = this.$locale.shortNodeType(this.nodeType.name);
return this.$locale.headerText(`headers.${shortNodeType}.displayName`, this.nodeType.name);
return this.$locale.headerText({
key: `headers.${shortNodeType}.displayName`,
fallback: this.nodeType.name,
});
} }
return ''; return '';
@ -183,11 +179,7 @@ export default mixins(externalHooks, nodeHelpers).extend({
nodeTypeDescription(): string { nodeTypeDescription(): string {
if (this.nodeType && this.nodeType.description) { if (this.nodeType && this.nodeType.description) {
const shortNodeType = this.$locale.shortNodeType(this.nodeType.name); const shortNodeType = this.$locale.shortNodeType(this.nodeType.name);
return this.$locale.headerText(`headers.${shortNodeType}.description`, this.nodeType.description);
return this.$locale.headerText({
key: `headers.${shortNodeType}.description`,
fallback: this.nodeType.description,
});
} else { } else {
return this.$locale.baseText('nodeSettings.noDescriptionFound'); return this.$locale.baseText('nodeSettings.noDescriptionFound');
} }

View file

@ -696,10 +696,7 @@ export default mixins(
allNodesData.push( allNodesData.push(
{ {
name: this.$locale.headerText({ name: this.$locale.headerText(`headers.${shortNodeType}.displayName`, nodeName),
key: `headers.${shortNodeType}.displayName`,
fallback: nodeName,
}),
options: this.sortOptions(nodeOptions), options: this.sortOptions(nodeOptions),
}, },
); );

View file

@ -77,9 +77,7 @@ export class I18nClass {
/** /**
* Render a string of dynamic text, i.e. a string with a constructed path to the localized value. * Render a string of dynamic text, i.e. a string with a constructed path to the localized value.
*/ */
private dynamicRender( private dynamicRender(key: string, fallback = ''): string {
{ key, fallback }: { key: string; fallback: string; },
) {
return this.i18n.te(key) ? this.i18n.t(key).toString() : fallback; return this.i18n.te(key) ? this.i18n.t(key).toString() : fallback;
} }
@ -87,8 +85,8 @@ export class I18nClass {
* Render a string of header text (a node's name and description), * Render a string of header text (a node's name and description),
* used variously in the nodes panel, under the node icon, etc. * used variously in the nodes panel, under the node icon, etc.
*/ */
headerText(arg: { key: string; fallback: string; }) { headerText(key: string, fallback = '') {
return this.dynamicRender(arg); return this.dynamicRender(key, fallback);
} }
/** /**
@ -108,16 +106,10 @@ export class I18nClass {
{ name: parameterName, displayName }: INodeProperties, { name: parameterName, displayName }: INodeProperties,
) { ) {
if (['clientId', 'clientSecret'].includes(parameterName)) { if (['clientId', 'clientSecret'].includes(parameterName)) {
return context.dynamicRender({ return context.dynamicRender(`_reusableDynamicText.oauth2.${parameterName}`, displayName);
key: `_reusableDynamicText.oauth2.${parameterName}`,
fallback: displayName,
});
} }
return context.dynamicRender({ return context.dynamicRender(`${credentialPrefix}.${parameterName}.displayName`, displayName);
key: `${credentialPrefix}.${parameterName}.displayName`,
fallback: displayName,
});
}, },
/** /**
@ -126,10 +118,7 @@ export class I18nClass {
hint( hint(
{ name: parameterName, hint }: INodeProperties, { name: parameterName, hint }: INodeProperties,
) { ) {
return context.dynamicRender({ return context.dynamicRender(`${credentialPrefix}.${parameterName}.hint`, hint);
key: `${credentialPrefix}.${parameterName}.hint`,
fallback: hint ?? '',
});
}, },
/** /**
@ -138,10 +127,7 @@ export class I18nClass {
inputLabelDescription( inputLabelDescription(
{ name: parameterName, description }: INodeProperties, { name: parameterName, description }: INodeProperties,
) { ) {
return context.dynamicRender({ return context.dynamicRender(`${credentialPrefix}.${parameterName}.description`, description);
key: `${credentialPrefix}.${parameterName}.description`,
fallback: description ?? '',
});
}, },
/** /**
@ -151,10 +137,7 @@ export class I18nClass {
{ name: parameterName }: INodeProperties, { name: parameterName }: INodeProperties,
{ value: optionName, name: displayName }: INodePropertyOptions, { value: optionName, name: displayName }: INodePropertyOptions,
) { ) {
return context.dynamicRender({ return context.dynamicRender(`${credentialPrefix}.${parameterName}.options.${optionName}.displayName`, displayName);
key: `${credentialPrefix}.${parameterName}.options.${optionName}.displayName`,
fallback: displayName,
});
}, },
/** /**
@ -164,10 +147,7 @@ export class I18nClass {
{ name: parameterName }: INodeProperties, { name: parameterName }: INodeProperties,
{ value: optionName, description }: INodePropertyOptions, { value: optionName, description }: INodePropertyOptions,
) { ) {
return context.dynamicRender({ return context.dynamicRender(`${credentialPrefix}.${parameterName}.options.${optionName}.description`, description);
key: `${credentialPrefix}.${parameterName}.options.${optionName}.description`,
fallback: description ?? '',
});
}, },
/** /**
@ -176,10 +156,7 @@ export class I18nClass {
placeholder( placeholder(
{ name: parameterName, placeholder }: INodeProperties, { name: parameterName, placeholder }: INodeProperties,
) { ) {
return context.dynamicRender({ return context.dynamicRender(`${credentialPrefix}.${parameterName}.placeholder`, placeholder);
key: `${credentialPrefix}.${parameterName}.placeholder`,
fallback: placeholder ?? '',
});
}, },
}; };
} }
@ -203,11 +180,7 @@ export class I18nClass {
path: string, path: string,
) { ) {
const middleKey = deriveMiddleKey(path, parameter); const middleKey = deriveMiddleKey(path, parameter);
return context.dynamicRender(`${initialKey}.${middleKey}.displayName`, parameter.displayName);
return context.dynamicRender({
key: `${initialKey}.${middleKey}.displayName`,
fallback: parameter.displayName,
});
}, },
/** /**
@ -218,11 +191,7 @@ export class I18nClass {
path: string, path: string,
) { ) {
const middleKey = deriveMiddleKey(path, parameter); const middleKey = deriveMiddleKey(path, parameter);
return context.dynamicRender(`${initialKey}.${middleKey}.description`, parameter.description);
return context.dynamicRender({
key: `${initialKey}.${middleKey}.description`,
fallback: parameter.description ?? '',
});
}, },
/** /**
@ -233,11 +202,7 @@ export class I18nClass {
path: string, path: string,
) { ) {
const middleKey = deriveMiddleKey(path, parameter); const middleKey = deriveMiddleKey(path, parameter);
return context.dynamicRender(`${initialKey}.${middleKey}.hint`, parameter.hint);
return context.dynamicRender({
key: `${initialKey}.${middleKey}.hint`,
fallback: parameter.hint ?? '',
});
}, },
/** /**
@ -257,10 +222,7 @@ export class I18nClass {
middleKey = insertOptionsAndValues(pathSegments).join('.'); middleKey = insertOptionsAndValues(pathSegments).join('.');
} }
return context.dynamicRender({ return context.dynamicRender(`${initialKey}.${middleKey}.placeholder`, parameter.placeholder);
key: `${initialKey}.${middleKey}.placeholder`,
fallback: parameter.placeholder ?? '',
});
}, },
/** /**
@ -279,10 +241,7 @@ export class I18nClass {
middleKey = insertOptionsAndValues(pathSegments).join('.'); middleKey = insertOptionsAndValues(pathSegments).join('.');
} }
return context.dynamicRender({ return context.dynamicRender(`${initialKey}.${middleKey}.options.${optionName}.displayName`, displayName);
key: `${initialKey}.${middleKey}.options.${optionName}.displayName`,
fallback: displayName,
});
}, },
/** /**
@ -301,10 +260,7 @@ export class I18nClass {
middleKey = insertOptionsAndValues(pathSegments).join('.'); middleKey = insertOptionsAndValues(pathSegments).join('.');
} }
return context.dynamicRender({ return context.dynamicRender(`${initialKey}.${middleKey}.options.${optionName}.description`, description);
key: `${initialKey}.${middleKey}.options.${optionName}.description`,
fallback: description ?? '',
});
}, },
/** /**
@ -324,10 +280,7 @@ export class I18nClass {
middleKey = insertOptionsAndValues(pathSegments).join('.'); middleKey = insertOptionsAndValues(pathSegments).join('.');
} }
return context.dynamicRender({ return context.dynamicRender(`${initialKey}.${middleKey}.options.${optionName}.displayName`, displayName);
key: `${initialKey}.${middleKey}.options.${optionName}.displayName`,
fallback: displayName,
});
}, },
/** /**
@ -337,20 +290,14 @@ export class I18nClass {
multipleValueButtonText( multipleValueButtonText(
{ name: parameterName, typeOptions}: INodeProperties, { name: parameterName, typeOptions}: INodeProperties,
) { ) {
return context.dynamicRender({ return context.dynamicRender(`${initialKey}.${parameterName}.multipleValueButtonText`, typeOptions?.multipleValueButtonText);
key: `${initialKey}.${parameterName}.multipleValueButtonText`,
fallback: typeOptions!.multipleValueButtonText!,
});
}, },
eventTriggerDescription( eventTriggerDescription(
nodeType: string, nodeType: string,
eventTriggerDescription: string, eventTriggerDescription: string,
) { ) {
return context.dynamicRender({ return context.dynamicRender(`n8n-nodes-base.nodes.${nodeType}.nodeView.eventTriggerDescription`, eventTriggerDescription);
key: `n8n-nodes-base.nodes.${nodeType}.nodeView.eventTriggerDescription`,
fallback: eventTriggerDescription,
});
}, },
}; };
} }

View file

@ -555,10 +555,7 @@ export default mixins(
this.updateNodesExecutionIssues(); this.updateNodesExecutionIssues();
}, },
translateName(type: string, originalName: string) { translateName(type: string, originalName: string) {
return this.$locale.headerText({ return this.$locale.headerText(`headers.${this.$locale.shortNodeType(type)}.displayName`, originalName);
key: `headers.${this.$locale.shortNodeType(type)}.displayName`,
fallback: originalName,
});
}, },
getUniqueNodeName({ getUniqueNodeName({
originalName, originalName,