2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2021-03-18 15:34:15 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2024-08-29 06:55:53 -07:00
|
|
|
import { NodeConnectionType } from 'n8n-workflow';
|
2021-03-18 15:34:15 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { deepLApiRequest } from './GenericFunctions';
|
2021-03-18 15:34:15 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { textOperations } from './TextDescription';
|
2021-03-18 15:34:15 -07:00
|
|
|
|
|
|
|
export class DeepL implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'DeepL',
|
|
|
|
name: 'deepL',
|
2024-06-26 00:35:59 -07:00
|
|
|
icon: { light: 'file:deepl.svg', dark: 'file:deepL.dark.svg' },
|
2021-03-18 15:34:15 -07:00
|
|
|
group: ['input', 'output'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Translate data using DeepL',
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
defaults: {
|
|
|
|
name: 'DeepL',
|
|
|
|
},
|
2024-08-29 06:55:53 -07:00
|
|
|
inputs: [NodeConnectionType.Main],
|
|
|
|
outputs: [NodeConnectionType.Main],
|
2021-03-18 15:34:15 -07:00
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'deepLApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-03-18 15:34:15 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Language',
|
|
|
|
value: 'language',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'language',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-03-18 15:34:15 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
resource: ['language'],
|
2021-03-18 15:34:15 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Translate',
|
|
|
|
value: 'translate',
|
|
|
|
description: 'Translate data',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Translate a language',
|
2021-03-18 15:34:15 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'translate',
|
|
|
|
},
|
|
|
|
...textOperations,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getLanguages(this: ILoadOptionsFunctions) {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-01 13:47:55 -07:00
|
|
|
const languages = await deepLApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/languages',
|
|
|
|
{},
|
|
|
|
{ type: 'target' },
|
|
|
|
);
|
2021-03-18 15:34:15 -07:00
|
|
|
for (const language of languages) {
|
|
|
|
returnData.push({
|
|
|
|
name: language.name,
|
|
|
|
value: language.language,
|
|
|
|
});
|
|
|
|
}
|
2021-05-30 10:36:04 -07:00
|
|
|
|
|
|
|
returnData.sort((a, b) => {
|
2022-08-01 13:47:55 -07:00
|
|
|
if (a.name < b.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name > b.name) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-05-30 10:36:04 -07:00
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
2021-03-18 15:34:15 -07:00
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const length = items.length;
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const responseData: INodeExecutionData[] = [];
|
2021-03-18 15:34:15 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2022-12-02 12:54:28 -08:00
|
|
|
const resource = this.getNodeParameter('resource', i);
|
|
|
|
const operation = this.getNodeParameter('operation', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'language') {
|
|
|
|
if (operation === 'translate') {
|
2022-07-05 00:02:25 -07:00
|
|
|
let body: IDataObject = {};
|
2021-07-19 23:58:54 -07:00
|
|
|
const text = this.getNodeParameter('text', i) as string;
|
|
|
|
const translateTo = this.getNodeParameter('translateTo', i) as string;
|
2022-08-01 13:47:55 -07:00
|
|
|
body = { target_lang: translateTo, text } as IDataObject;
|
2021-03-18 15:34:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.sourceLang !== undefined) {
|
2022-07-05 00:02:25 -07:00
|
|
|
body.source_lang = ['EN-GB', 'EN-US'].includes(additionalFields.sourceLang as string)
|
2021-07-19 23:58:54 -07:00
|
|
|
? 'EN'
|
|
|
|
: additionalFields.sourceLang;
|
|
|
|
}
|
2021-03-18 15:34:15 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const { translations } = await deepLApiRequest.call(this, 'GET', '/translate', body);
|
|
|
|
const [translation] = translations;
|
2023-02-27 19:39:43 -08:00
|
|
|
const translationJsonArray = this.helpers.returnJsonArray(translation as IDataObject[]);
|
2022-11-08 06:28:21 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(translationJsonArray, {
|
|
|
|
itemData: { item: i },
|
|
|
|
});
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData.push(...executionData);
|
2021-03-18 15:34:15 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
2024-06-19 22:45:00 -07:00
|
|
|
if (this.continueOnFail(error)) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = {
|
|
|
|
json: {} as IDataObject,
|
|
|
|
error: error.message,
|
|
|
|
itemIndex: i,
|
|
|
|
};
|
|
|
|
responseData.push(executionErrorData as INodeExecutionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-03-18 15:34:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
return [responseData];
|
2021-03-18 15:34:15 -07:00
|
|
|
}
|
|
|
|
}
|