5102 Posts in 1520 Topics by 1116 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 3052 Views |
-
CMS Batch Actions: Translate / Translate and publish

11 June 2009 at 10:45pm
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 -
Re: CMS Batch Actions: Translate / Translate and publish

14 June 2009 at 10:16pm Last edited: 14 June 2009 10:16pm
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.
-
Re: CMS Batch Actions: Translate / Translate and publish

16 June 2009 at 7:58pm
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 -
Re: CMS Batch Actions: Translate / Translate and publish

17 June 2009 at 4:22am
Yes!!! I find that very very usefull!!!
Thanks -
Re: CMS Batch Actions: Translate / Translate and publish

7 July 2009 at 12:58am
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.
-
Re: CMS Batch Actions: Translate / Translate and publish

1 March 2010 at 8:47pm
Would be nice as a core feature
-
Re: CMS Batch Actions: Translate / Translate and publish

18 February 2012 at 2:15am
Is it also possible to copy my DataObjects into multiple languages?
-
Re: CMS Batch Actions: Translate / Translate and publish

19 May 2012 at 8:36pm
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.
| 3052 Views | ||
|
Page:
1
|
Go to Top |



