diff --git a/action.yml b/action.yml index a9df3e7..26858e6 100644 --- a/action.yml +++ b/action.yml @@ -6,7 +6,17 @@ branding: inputs: notion_token: description: 'Notion API token' + required: true + translation_engine: + description: 'Engine to use for translation (see: https://www.npmjs.com/package/translate)' required: true + default: 'google' + translation_key: + description: 'Key to use for translation if required (see: https://www.npmjs.com/package/translate)' + required: false + translation_url: + description: 'Url to use for translation if required (see: https://www.npmjs.com/package/translate)' + required: false database: description: 'Parent database to translate' required: true diff --git a/dist/index.js b/dist/index.js index 6377c1f..a2a3214 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7634,7 +7634,7 @@ const loadData = async ({ notion }) => { const checkField = (field, type, typeDescription) => { if (!field || structure[field] !== type) { error = true - core.error(`The field ${field} must exist in the Notion database and be of type "${typeDescription}"`) + core.error(`The field ${field} must exist in the Notion database and be of type ${typeDescription} but instead found ${structure[field]}`) } } @@ -7695,6 +7695,19 @@ const getText = (richText) => { } } +// Configure translator +const translationEngine = core.getInput('translation_engine') +const translationKey = core.getInput('translation_key') +const translationUrl = core.getInput('translation_url') +if (translationEngine) { + translator.engine = translationEngine +} +if (translationKey) { + translator.key = translationKey +} +if (translationUrl) { + translator.url = translationUrl +} const defaultLanguageFrom = core.getInput('default_language_from') const defaultLanguageTo = core.getInput('default_language_to') @@ -7704,7 +7717,12 @@ const translate = async ({ notion, database, rows, fields }) => { const inputLanguage = fields.language ? getText(row.properties[fields.language].rich_text) || defaultLanguageFrom : defaultLanguageFrom for (const input of fields.inputs) { const inputText = getText(row.properties[input].rich_text) - translations[input] = inputText ? await translator(inputText, { from: inputLanguage, to: defaultLanguageTo }) : '' + try { + translations[input] = inputText ? await translator(inputText, { from: inputLanguage, to: defaultLanguageTo }) : '' + } catch (ex) { + core.error(`Error with translation: ${ex.message}`) + process.exit(1) + } } await updateNotionRow(row, translations, { notion, database, fields }) } @@ -7715,14 +7733,16 @@ const updateNotionRow = async (row, translations, { notion, database, fields }) try { const properties = {} for (const input of fields.inputs) { - properties[fields.translations[input]] = { - rich_text: [ - { - text: { - content: translations[input] + if (translations[input]) { + properties[fields.translations[input]] = { + rich_text: [ + { + text: { + content: translations[input] + } } - } - ] + ] + } } } diff --git a/src/data.js b/src/data.js index 864ea4c..c9f8d8a 100644 --- a/src/data.js +++ b/src/data.js @@ -45,7 +45,7 @@ const loadData = async ({ notion }) => { const checkField = (field, type, typeDescription) => { if (!field || structure[field] !== type) { error = true - core.error(`The field ${field} must exist in the Notion database and be of type "${typeDescription}"`) + core.error(`The field ${field} must exist in the Notion database and be of type ${typeDescription} but instead found ${structure[field]}`) } } diff --git a/src/index.test.js b/src/index.test.js index 21d71b1..9604098 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -7,6 +7,7 @@ jest.setTimeout(180000) test('complete input should succeed with default inputs', () => { process.env.INPUT_NOTION_TOKEN = process.env.NOTION_TOKEN process.env.INPUT_DATABASE = '3ea38518219c45dc8051c5c70b9de5ca' + process.env.INPUT_TRANSLATION_ENGINE = 'google' process.env.INPUT_DEFAULT_LANGUAGE_FROM = 'nl' process.env.INPUT_DEFAULT_LANGUAGE_TO = 'en' process.env.INPUT_FIELD_TO_TRANSLATE = 'Message' diff --git a/src/local.js b/src/local.js index 5d722a5..760aa32 100644 --- a/src/local.js +++ b/src/local.js @@ -4,6 +4,7 @@ const process = require('process') process.env.INPUT_NOTION_TOKEN = process.env.NOTION_TOKEN process.env.INPUT_DATABASE = '3ea38518219c45dc8051c5c70b9de5ca' +process.env.INPUT_TRANSLATION_ENGINE = 'google' process.env.INPUT_DEFAULT_LANGUAGE_FROM = 'nl' process.env.INPUT_DEFAULT_LANGUAGE_TO = 'en' process.env.INPUT_FIELD_TO_TRANSLATE = 'Message,Second Field' diff --git a/src/translation.js b/src/translation.js index 1a7d663..6b4104e 100644 --- a/src/translation.js +++ b/src/translation.js @@ -12,6 +12,19 @@ const getText = (richText) => { } } +// Configure translator +const translationEngine = core.getInput('translation_engine') +const translationKey = core.getInput('translation_key') +const translationUrl = core.getInput('translation_url') +if (translationEngine) { + translator.engine = translationEngine +} +if (translationKey) { + translator.key = translationKey +} +if (translationUrl) { + translator.url = translationUrl +} const defaultLanguageFrom = core.getInput('default_language_from') const defaultLanguageTo = core.getInput('default_language_to') @@ -21,7 +34,12 @@ const translate = async ({ notion, database, rows, fields }) => { const inputLanguage = fields.language ? getText(row.properties[fields.language].rich_text) || defaultLanguageFrom : defaultLanguageFrom for (const input of fields.inputs) { const inputText = getText(row.properties[input].rich_text) - translations[input] = inputText ? await translator(inputText, { from: inputLanguage, to: defaultLanguageTo }) : '' + try { + translations[input] = inputText ? await translator(inputText, { from: inputLanguage, to: defaultLanguageTo }) : '' + } catch (ex) { + core.error(`Error with translation: ${ex.message}`) + process.exit(1) + } } await updateNotionRow(row, translations, { notion, database, fields }) } @@ -32,14 +50,16 @@ const updateNotionRow = async (row, translations, { notion, database, fields }) try { const properties = {} for (const input of fields.inputs) { - properties[fields.translations[input]] = { - rich_text: [ - { - text: { - content: translations[input] + if (translations[input]) { + properties[fields.translations[input]] = { + rich_text: [ + { + text: { + content: translations[input] + } } - } - ] + ] + } } }