Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Pandan opened this issue Aug 23, 2023 · 6 comments
Open

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

Pandan opened this issue Aug 23, 2023 · 6 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@Pandan
Copy link

Pandan commented Aug 23, 2023

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?

@Pandan Pandan added the enhancement New feature or request label Aug 23, 2023
@sargreal
Copy link
Member

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
Copy link
Author

Pandan commented Aug 24, 2023

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

@sargreal sargreal added the documentation Improvements or additions to documentation label Aug 23, 2024
@sargreal sargreal moved this from Investigate to Ideas in Translate Strapi Plugin Aug 23, 2024
@hadiulofficial
Copy link

@Pandan Did you find any solution for the automation?

@Pandan
Copy link
Author

Pandan commented Sep 4, 2024

@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
Copy link

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

@sargreal
Copy link
Member

sargreal commented Sep 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
Development

No branches or pull requests

3 participants