OctoberCMS 翻译插件与 SEO 插件不翻译 CMS 页面和博客的 SEO 关键字

OctoberCMS translate plugin with SEO plugin doesnt translate SEO keywords for CMS pages and Blogs

我正在使用 OctoberCMS SEO 翻译插件 https://octobercms.com/plugin/anandpatel-seoextension and it works as per my expectations .. but I have a query with Translate plugin https://octobercms.com/plugin/rainlab-translate。请先看我的几张截图。

正如您在我上面的 2 个屏幕截图中看到的,在我应该创建或编辑博客的博客页面中,我无法为元标题、元描述、元关键字等进行翻译..

此外,在我的 CMS 页面中,我也无法使我的“元关键字”可翻译..

我试图将以下代码放入我的一个活动插件的 Plugin.php 文件中,以使我的博客“元标题”字段可翻译,但它也没有用..

\RainLab\Blog\Models\Post::extend(function($model) {            
            $model->translatable[] = 'seo_title';
        });

并尝试了下面的代码..

\AnandPatel\SeoExtension\Models\BlogPost::extend(function($model) {
            $model->translatable[] = 'seo_title';
        });

有人可以指导我如何使这些字段可翻译吗?

我调试了一下,问题是

SEO Extension Plugin is listening for \Event::listen('backend.form.extendFields' ... and this event is fired after backend.form.extendFieldsBefore so new added fields are not translatable.

backend.form.extendFieldsBefore 此事件负责将字段转换为 translatable 字段,因此在此事件之后添加的字段不会显示为 translatable.

我计划纠正此行为 并在我有空闲时间后对作者回购做出 PR 贡献https://github.com/anand-patel/oc-seo-extension

Solution could be [ need to be done in plugin base code ] to use backend.form.extendFieldsBefore event in SEO Extension Plugin and during extension of fields it need to directly work with raw config, right now it uses $widget->addFields so it has to remove that and directly injecting fields to config so, later on newly added fields can be processed by RainLab.Translate Plugin using backend.form.extendFieldsBefore event.

所以现在解决方法 在您的自定义插件中添加已经支持跨国的字段,或者您可以再次使用 extendFieldsBefore 并使用 raw config添加字段。

这里我们只使用简单的解决方案并添加具有可翻译字段类型的覆盖字段

use RainLab\Blog\Models\Post;
use Event;
use System\Classes\PluginManager;

public function register() {

  Post::extend(function($post) {
      if (!$post->propertyExists('translatable')) {
          $post->addDynamicProperty('translatable', []);
      }
      $post->translatable = array_merge($post->translatable, ['seo_title', 'seo_description' /* so on ....*/]);
  });

  Event::listen('backend.form.extendFields', function($widget) {
      if(PluginManager::instance()->hasPlugin('RainLab.Blog') && $widget->model instanceof \RainLab\Blog\Models\Post)
        {
            $widget->addFields([
              'seo_title' => [
                  'label'   => 'Meta Title',
                  'type'    => 'mltext', //<- HERE
                  'tab'     => 'SEO'
              ],
              'seo_description' => [
                  'label'   => 'Meta Description',
                  'type'    => 'mltextarea', //<- HERE
                  'size'    => 'tiny',
                  'tab'     => 'SEO'
              ],
              'seo_keywords' => [
                  'label'   => 'Meta Keywords',
                  'type'    => 'mltextarea', //<- HERE
                  'size'    => 'tiny',
                  'tab'     => 'SEO'
              ],
              'canonical_url' => [
                  'label'   => 'Canonical URL',
                  'type'    => 'mltext', //<- HERE
                  'tab'     => 'SEO',
                  'span'    => 'left'
              ],
              'redirect_url' => [
                  'label'   => 'Redirect URL',
                  'type'    => 'mltext', //<- HERE
                  'tab'     => 'SEO',
                  'span'    => 'right'

              ],
              // ... so on
          ],
          'secondary');
        }
  });
}

但这是标准解决方案的解决方法,我计划更正此问题并将 PR 推送给作者插件 repo 可能是 -> https://github.com/anand-patel/oc-seo-extension

如有疑问请评论。