Fekide / strapi-plugin-translate

Strapi plugin for automated translations using different Translate Providers
https://market.strapi.io/plugins/strapi-plugin-translate
MIT License
51 stars 20 forks source link

[FEATURE]: Automatic translation or execute using cron? #284

Open Pandan opened 1 year ago

Pandan commented 1 year ago

Hi, I am wondering if there is someway to automate a translation or execute a batch using a cron job .

In our Strapi setup we are fetching data from an external api which then populates a collection-type in strapi. This fetching is done regularly using a cron job.

It would be great If I somehow could translate each entry automatically when its added or trigger a batch when all enteries has been added?

sargreal commented 1 year ago

Hi @Pandan and thanks for your question.

You can take inspiration on how it is done in plugin/server/controllers/translate.js. Currently the easier way is to do the batch translation. Direct works as well but the service is currently only written to translate individual fields and you need to import the utils manually. We will probably improve that at some point though

Batch Translation would look like this:

await getService('translate').batchTranslate({
  contentType: "api::article.article",
  // Use here the exact id of the locale you selected in the internationalization settings in strapi
  sourceLocale: "en-US",
  targetLocale: "de",
  // Optional, use to specify the individual ones to translate, 
  // otherwise all currently untranslated ones will be translated
  entityIds: [1,2,3], 
  autoPublish: true,
}),
Pandan commented 1 year ago

@sargreal Thank you for the quick response, I'll take a look and see if I can figure something out :)

hadiulofficial commented 1 month ago

@Pandan Did you find any solution for the automation?

Pandan commented 1 month ago

@hadiulofficial Yes, i did follow @sargreal advice and it worked great. In our case we where running a cron-job which fetched data from an external api and populated a collection-type. So we just executed the batchTranslate after this was finished.

  await getService("translate").batchTranslate({
    contentType: "api::event.event",
    sourceLocale: "sv-SE",
    targetLocale: "en",
    autoPublish: true,
  });
  console.log(`Events translated)`);
hadiulofficial commented 1 month ago

@Pandan Where u ran this code could u please tell? did u crated any new file or could u please tell the way

sargreal commented 1 month ago

Dear @hadiulofficial,

The cron option (for creating batch translations at a fixed time) is described in the strapi docs: https://docs.strapi.io/dev-docs/configurations/cron

If you want to run this translation directly after a content is created or updated, you will need to use the lifecycle hooks, described here in the strapi docs: https://docs.strapi.io/dev-docs/backend-customization/models#lifecycle-hooks Using the lifecycle hooks may however require several checks to determine what needs to be translated exactly (translate only if the created entity is the default locale, delete existing translations as updating is not released yet, etc.) and should not block the request.