Jump to:

5516 Posts in 1734 Topics by 1221 members

Customising the CMS

SilverStripe Forums » Customising the CMS » CMS Batch Actions: Translate / Translate and publish

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 5186 Views
  • drx
    Avatar
    Community Member
    7 Posts

    CMS Batch Actions: Translate / Translate and publish Link to this post

    I just played around with Silverstripe 2.3.2-rc2 and Translatable enabled. Translating pages really works nice with this RC - but translating pages is still a lot of work... you have to manually create translations for each Locale / Language .. after some translations you will go nuts...

    So - i am a lazy cms author but a hard-working coder - here are some CMS Batch Actions to make translating pages easier...

    Pleae keep in mind: this beta code snippet is just tested (a little bit) against 2.3.2-rc2 - it might not work on older releases .

    Please try, improve and let me now.

    <?php
    /**
    * Translate items to all available locales and store new pages as draft - CMS batch action.
    * Requires {@link Translatable::enabled} in your _config.php.
    *
    * Add batch actions by adding this to your _config.php:
    * CMSBatchActionHandler::register('translate', 'CMSBatchActionTranslate');
    *
    * @author Dirk Adler / KLITSCHE.DE
    */
    class CMSBatchActionTranslate extends CMSBatchAction
    {
    function getActionTitle()
    {
    return _t('CMSBatchActions.TRANSLATE_PAGES_TO_DRAFT', 'Translate to draft');
    }

    function getDoingText()
    {
    return _t('CMSBatchActions.TRANSLATING_PAGES_TO_DRAFT', 'Translating pages');
    }

    function run(DataObjectSet $pages)
    {
    return $this->batchaction(
             $pages,
             null,
             _t('CMSBatchActions.TRANSLATED_PAGES_TO_DRAFT', 'Processed %d pages and saved %d translations (draft)')
    );
    }

    public function batchaction(DataObjectSet $pages, $helperMethod, $successMessage)
    {
    if (Translatable::get_allowed_locales() == null)
    {
    FormResponse::add('statusMessage("'._t('CMSBatchAction.TRANSLATE_ALLOWED_LOCALES','Please add Translatable::set_allowed locales to your _config.php').'","bad");');
    }
    else
    {
    $translated = 0;

    foreach($pages as $page)
    {
    foreach (Translatable::get_allowed_locales() as $locale)
    {
    if ($page->Locale == $locale) continue;
    if (! $page->hasTranslation($locale))
    {
    try
    {
    $translation = $page->createTranslation($locale);
                         if ($helperMethod) $translation->$helperMethod();
                         $translation->destroy();
    unset($translation);
    $translated++;
    }
    catch (Exception $e)
    {
    // no permission - fail gracefully
    }
    }
    }

    $page->destroy();
    unset($page);
    }

    $message = sprintf($successMessage, $pages->Count(), $translated);
    FormResponse::add('statusMessage("'.$message.'","good");');
    }

    return FormResponse::respond();
    }
    }

    /**
    * Translate and publish items to all other available locales - batch action.
    * Requires {@link Translatable::enabled} in your _config.php.
    *
    * Add batch actions by adding this to your _config.php:
    * CMSBatchActionHandler::register('translate-and-publish', 'CMSBatchActionTranslateAndPublish');
    *
    * @author Dirk Adler / KLITSCHE.DE
    */
    class CMSBatchActionTranslateAndPublish extends CMSBatchActionTranslate
    {
    function getActionTitle()
    {
    return _t('CMSBatchActions.TRANSLATE_PAGES_TO_LIVE', 'Translate and publish');
    }

    function getDoingText()
    {
    return _t('CMSBatchActions.TRANSLATING_PAGES_TO_LIVE', 'Translating and publishing pages');
    }

    function run(DataObjectSet $pages)
    {
    return $this->batchaction(
             $pages,
             'doPublish',
             _t('CMSBatchActions.TRANSLATED_PAGES_TO_LIVE', 'Processed %s pages and saved %s translations (live)')
    );
    }
    }
    ?>

    Save code as CMSBatchActionTranslate.php in your site (mysite folder?) and add this to your config.php.

    CMSBatchActionHandler::register('translate', 'CMSBatchActionTranslate');
    CMSBatchActionHandler::register('translate-and-publish', 'CMSBatchActionTranslateAndPublish');

    Happy coding.
    drx

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: CMS Batch Actions: Translate / Translate and publish Link to this post

    Hey thats really awesome! Especially given that we've just created the CMSBatchAction API a couple of weeks ago
    Does anybody else find this useful? Might be something we'd consider as a core addition if drx has no objections. It would need some unit tests, although the script itself seems to be fairly straightforward.

    One cool addition would be a javascript-driven interface for selecting a specific locale you want to batch create a translation in - any takers? ;)

    @drx: Can you provide the sourcecode as a PHP attachement? Looks like the forum messed up indentations in your code.

  • drx
    Avatar
    Community Member
    7 Posts

    Re: CMS Batch Actions: Translate / Translate and publish Link to this post

    Hi Ingo, thanks a lot for your feedback. I attached the file.

    Sometimes autopublishing of translated pages does not work - you have to manually publish them - i didn´t track this down yet - but it´s probably connected only to parent pages / given order of page ids via url (?) ...

    Selecting one or several locales / languages would be really a cool feature ...

    happy coding
    drx

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: CMS Batch Actions: Translate / Translate and publish Link to this post

    Yes!!! I find that very very usefull!!! Thanks

  • heidgert
    Avatar
    Community Member
    16 Posts

    Re: CMS Batch Actions: Translate / Translate and publish Link to this post

    Sweeeet! Haven't tried it out yet, but certainly will. Thanks for sharing!

    I actually searched around for two days before finding this thread. After having translated around 10 pages (with 200 something to go) I decided to look for an alternative solution.

    Translating by hand is really a pain currently since after creating a page in a new language switches to this language and makes you need to switch back to the original language before being able to translate the next.

    Adding this to the core or as a module is a must for wanting to translate larger sites.

    Once again, thanks, I will be testing this within the next days.

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: CMS Batch Actions: Translate / Translate and publish Link to this post

    Would be nice as a core feature

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: CMS Batch Actions: Translate / Translate and publish Link to this post

    Is it also possible to copy my DataObjects into multiple languages?

  • Gelert
    Avatar
    Community Member
    8 Posts

    Re: CMS Batch Actions: Translate / Translate and publish Link to this post

    This is a brilliant addition. Thanks for posting. It's not in SS2.4.7 so here's hoping it gets added to SS3 as a feature.

    5186 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.