Skip to content

Commit

Permalink
Allow configuration of translator
Browse files Browse the repository at this point in the history
  • Loading branch information
cliftonc committed Nov 27, 2022
1 parent 81a0b5f commit 6951738
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 18 deletions.
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 29 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`)
}
}

Expand Down Expand Up @@ -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')

Expand All @@ -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 })
}
Expand All @@ -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]
}
}
}
]
]
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions src/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
36 changes: 28 additions & 8 deletions src/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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 })
}
Expand All @@ -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]
}
}
}
]
]
}
}
}

Expand Down

0 comments on commit 6951738

Please sign in to comment.