Bulk text translation with DeepL?

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
columbo
Advanced
Posts: 125
Joined: 09 Oct 2019, 09:42

Bulk text translation with DeepL?

Post by columbo » 26 Oct 2023, 09:16

Hi,

with "AI-based text translation" DeepL translation of single text elemets is possible.
found here https://aimeos.org/tips/aimeos-2019-10-lts-release/ and https://aimeos.org/docs/2023.x/config/a ... #translate

Is there also an option to translate multipe text elements at once / as batch?
eg. translation of all product names (about 3.000 text items) from DE to EN at once.

Thank you

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: Bulk text translation with DeepL?

Post by aimeos » 27 Oct 2023, 17:42

No that's not possible out of the box and you have to write your own script to do that.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

columbo
Advanced
Posts: 125
Joined: 09 Oct 2019, 09:42

Re: Bulk text translation with DeepL?

Post by columbo » 07 Dec 2023, 14:51

What would be the best way to create a script for the mass translation of product texts?

Load the relevant texts from the DB table (SQL) -> call DeepL-API -> write translation to the DB?
Which table should be considered?

Is there another method or function of Aimeos that can be used?

Thank you!

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: Bulk text translation with DeepL?

Post by aimeos » 10 Dec 2023, 10:54

Use the Aimeos text manager to iterate e.g. over all English texts, call the DeepL API and save the returned text as new text item:
https://aimeos.org/docs/latest/models/s ... fficiently
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

columbo
Advanced
Posts: 125
Joined: 09 Oct 2019, 09:42

Re: Bulk text translation with DeepL?

Post by columbo » 18 Dec 2023, 07:53

Hi,

I'm trying to translate all product texts with type = 'name' from DE to EN and ES via DeepL API.
Selecting the 'name' texts and calling the API works

Code: Select all

$productManager = \Aimeos\MShop::create( $this->context(), 'product' );
	
$filter = $productManager->filter();
$cursor = $productManager->cursor( $filter );

while( $items = $productManager->iterate( $cursor, ['text'] ) ) {
    foreach ($items->getRefItems('text', 'name') as $textitems) {				
            foreach( $textitems as $entry ){

                $text_source = $entry['text.content'];
                echo($text_source);

            }
            
        }

    /**
    * get translation, call Deepl API
    */			
    $deepLy = new DeepLy(env('DEEPL_API_KEY'));
    $text_translated_de = $deepLy->translate($text_source, 'DE');
    $text_translated_es = $deepLy->translate($text_source, 'ES');


    /**
    * update product db entry, add transaltion to 
    */
    $items = $productManager->create();
    $items->addListItem( 'text', 'name', $text_translated_de,  );
    $items = $productManager->save( $items );
}
but two questions remain:

1) How can I check if an EN or ES translation already exists?
-> Only product name texts without an EN OR ES translation should be selected


2) How can I add the translated text to the product item?
I checked https://aimeos.org/docs/latest/models/m ... reate-item , but couldn't get it to work

Thank you

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: Bulk text translation with DeepL?

Post by aimeos » 22 Dec 2023, 09:14

columbo wrote: 18 Dec 2023, 07:53

Code: Select all

    $items = $productManager->create();
    $items->addListItem( 'text', 'name', $text_translated_de,  );
    $items = $productManager->save( $items );
1) How can I check if an EN or ES translation already exists?
-> Only product name texts without an EN OR ES translation should be selected

Code: Select all

    $partitions = $productItem->getRefItems( 'text', 'name', 'default' )->partition( function( $item ) {
        return $item->getLanguageId() === 'DE' ? 'DE' : 'tx';
    } );
    if( !$partitions->has( 'tx' ) ) {
        $textItem = $partitions->get( 'DE' )->first();
        // translate and add translation
    }
See PHP Map documentation: https://php-map.org/
columbo wrote: 18 Dec 2023, 07:53 2) How can I add the translated text to the product item?
I checked https://aimeos.org/docs/latest/models/m ... reate-item , but couldn't get it to work
https://aimeos.org/docs/latest/models/m ... ated-items

Code: Select all

    $listItem = $productManager->createListItem();
    $textItem = $textManager->create()->setType( 'name' )->setLanguageId( 'en' )->setContent( $text_translated_en );
    $productItem->addListItem( 'text', $listItem, $textItem->setLabel( 'Text in EN' );
    // ...
    $productManager->save( $productItems );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply