SilverStripe Translatable 在默认语言环境中保存值,并将其复制到非默认语言环境
SilverStripe Translatable save value in default locale, and duplicate it to non-default locales
我的项目是用 SilverStripe
3.1.13(CMS 和框架)构建的,我使用 silverstripe-translatable
和 dataobject-translatable
来组织我的翻译网站。
假设我有 Post.php
(这是 DataObject
用于处理我网站上的帖子),并且每个 post 都有自己的 category
(many-many
关系在这里,但这并不重要))。
问题是:我在创建新的 Post
数据对象时点击了 Save
按钮,我希望这些 category
会自动复制到另一个语言环境。
如何在我的应用程序中实现它?我想将这些值(它们是布尔值)保存到这些数据对象的另一个翻译中。
afaik 没有内置功能,但您可以在 onAfterWrite() 方法中编写如下内容:
public function onAfterWrite() {
$this->syncCategories();
}
public function syncCategories() {
//check if you're in main language, assuminig en_US here
if ($this->Locale !== 'en_US') return;
foreach ($this->getTranslations() as $translatedPage) {
//sync relations here
$translatedPage->RelationName = $this->RelationName;
//maybe more fine grained locic for only publishing when translated page is alredy published
$translatedPage->doPublish(); //writes and publishes in one
}
}
我的项目是用 SilverStripe
3.1.13(CMS 和框架)构建的,我使用 silverstripe-translatable
和 dataobject-translatable
来组织我的翻译网站。
假设我有 Post.php
(这是 DataObject
用于处理我网站上的帖子),并且每个 post 都有自己的 category
(many-many
关系在这里,但这并不重要))。
问题是:我在创建新的 Post
数据对象时点击了 Save
按钮,我希望这些 category
会自动复制到另一个语言环境。
如何在我的应用程序中实现它?我想将这些值(它们是布尔值)保存到这些数据对象的另一个翻译中。
afaik 没有内置功能,但您可以在 onAfterWrite() 方法中编写如下内容:
public function onAfterWrite() {
$this->syncCategories();
}
public function syncCategories() {
//check if you're in main language, assuminig en_US here
if ($this->Locale !== 'en_US') return;
foreach ($this->getTranslations() as $translatedPage) {
//sync relations here
$translatedPage->RelationName = $this->RelationName;
//maybe more fine grained locic for only publishing when translated page is alredy published
$translatedPage->doPublish(); //writes and publishes in one
}
}