没有不值得去解决的问题,也没有不值得去学习的技术!

Fix the Editor Display Issue in Yii2 Starter Kit After Switching to Chinese

When the Locale value is set to Simplified Chinese, the editor fails to display normally when creating an article

1. When the Locale value is set to English (US), the editor displays normally when creating an article, but the corresponding language pack file /assets/5fbb6f5a/lang/en.js returns a 404 response, as shown in Figure 1.

When the Locale value is set to English (US), the editor displays normally when creating an article, but the corresponding language pack file /assets/5fbb6f5a/lang/en.js returns a 404 response
Figure 1

2. When the Locale value is set to Simplified Chinese, the editor fails to display normally when creating an article, as shown in Figure 2.

When the Locale value is set to Simplified Chinese, the editor fails to display normally when creating an article
Figure 2

3. Refreshing the page and checking the browser Console reveals that the cause is the missing corresponding language file /assets/5fbb6f5a/lang/zh.js, which in turn leads to Uncaught TypeError: Cannot read property ‘html’ of undefined, as shown in Figure 3.

Refreshing the page and checking the browser Console reveals that the cause is the missing corresponding language file /assets/5fbb6f5a/lang/zh.js, which in turn leads to Uncaught TypeError: Cannot read property 'html' of undefined
Figure 3

4. Opening the URL http://backend.yii2-starter-kit.terentev.net/content/article/create shows that the official demo still has this bug, as shown in Figure 4.

Opening the URL http://backend.yii2-starter-kit.terentev.net/content/article/create shows that the official demo still has this bug
Figure 4

5. Checking \backend\views\article\_form.php, the editor code is as follows, which is based on asofter/yii2-imperavi-redactor.

PHP

    field($model, 'body')->widget(
        \yii\imperavi\Widget::className(),
        [
            'plugins' => ['fullscreen', 'fontcolor', 'video'],
            'options' => [
                'minHeight' => 400,
                'maxHeight' => 400,
                'buttonSource' => true,
                'convertDivs' => false,
                'removeEmptyTags' => false,
                'imageUpload' => Yii::$app->urlManager->createUrl(['/file-storage/upload-imperavi'])
            ]
        ]
    ) ?>

7. Open the URL https://imperavi.com/download/redactor/langs/en/ to download en.js, and copy it to \vendor\asofter\yii2-imperavi-redactor\yii\imperavi\assets\lang\en.js to resolve the 404 response for /assets/5fbb6f5a/lang/en.js. Note: If \vendor is not under version control, it is recommended to place en.js under version control so that it is automatically copied to \vendor\asofter\yii2-imperavi-redactor\yii\imperavi\assets\lang\en.js after running composer install. Refresh the page; /assets/5fbb6f5a/lang/en.js now returns a 200 response, as shown in Figure 6.

Open the URL https://imperavi.com/download/redactor/langs/en/ to download en.js, and copy it to \vendor\asofter\yii2-imperavi-redactor\yii\imperavi\assets\lang\en.js to resolve the 404 response for /assets/5fbb6f5a/lang/en.js. Note: If \vendor is not under version control, it is recommended to place en.js under version control so that it is automatically copied to \vendor\asofter\yii2-imperavi-redactor\yii\imperavi\assets\lang\en.js after running composer install. Refresh the page; /assets/5fbb6f5a/lang/en.js now returns a 200 response
Figure 6

8. Opening the URL https://imperavi.com/download/redactor/langs/zh/ fails to download. I decided to manually define the editor’s language option based on the current locale to ensure the corresponding language pack file can be found correctly, thereby resolving the editor display issue. Edit \backend\views\article\_form.php.

PHP
    <?php
        /* 基于本地语言环境手动定义编辑器的语言选项 */
        switch (Yii::$app->language) {
            case 'en-US':
                $lang = 'en';
                break;
            case 'ru-RU':
                $lang = 'ru';
                break;
            case 'uk-UA':
                $lang = 'uk';
                break;
            case 'es':
                $lang = 'es';
                break;
            case 'vi':
                $lang = 'vi';
                break;
            case 'zh-CN':
                $lang = 'zh_cn';
                break;
            case 'pl-PL':
                $lang = 'pl';
                break;
            default:
                $lang = 'en';
        }
    ?>
 
    <?php echo $form->field($model, 'body')->widget(
        \yii\imperavi\Widget::className(),
        [
            'plugins' => ['fullscreen', 'fontcolor', 'video'],
            'options' => [
                'lang' => $lang,
                'minHeight' => 400,
                'maxHeight' => 400,
                'buttonSource' => true,
                'convertDivs' => false,
                'removeEmptyTags' => false,
                'imageUpload' => Yii::$app->urlManager->createUrl(['/file-storage/upload-imperavi'])
            ]
        ]
    ) ?>

9. When the Locale value is set to Simplified Chinese, the editor displays normally when creating an article, as shown in Figure 7.

When the Locale value is set to Simplified Chinese, the editor displays normally when creating an article
Figure 7

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系