古登堡的翻译不起作用

时间:2020-02-11 作者:Winston

我遵循WordPress关于翻译Gutenberg块的文档,使用WP CLI创建。锅采购订单和。json文件并上传到该插件文件夹中的我的langauges文件夹,但它仍然不起作用。有人能告诉我我的代码有什么问题吗?

PHP:

function custom_blocks_load_plugin_textdomain() {
    load_plugin_textdomain( \'custom-blocks\', FALSE, \'\');
}
add_action( \'plugins_loaded\', \'custom_blocks_load_plugin_textdomain\' );

//register block template
function block_registration_template($block_path, $block_handle, $script_handle, $render_callback = false){
    wp_register_script(
        $script_handle,
        plugins_url( $block_path , __FILE__ ),
        [ \'wp-i18n\', \'wp-element\', \'wp-blocks\', \'wp-components\', \'wp-editor\' ],
        filemtime( plugin_dir_path( $block_path , __FILE__ ) )
    );

    $parameters = array(
        \'editor_script\' => $script_handle,
    );

    if($render_callback){
        $parameters[\'render_callback\'] = $render_callback;
    }

    register_block_type( $block_handle, $parameters );

}

function block_registration(){
block_registration_template(\'/ref_block.js\', \'custom-blocks/ref\', \'CUSTOM-block-ref\');

block_registration_template(\'/ref_holder_block.js\', \'custom-blocks/ref-holder\', \'CUSTOM-block-ref-holder\');
}

add_action(\'init\', \'block_registration\');

function custom_blocks_set_script_translations() {
    wp_set_script_translations( \'CUSTOM-block-ref-holder\', \'custom-blocks\');
}
add_action( \'init\', \'custom_blocks_set_script_translations\' );
JS公司

(() => {
    const __ = wp.i18n.__; // The __() for internationalization.
    const el = wp.element.createElement; // The wp.element.createElement() function to create elements.
    const registerBlockType = wp.blocks.registerBlockType; // The registerBlockType() to register blocks.

    const ServerSideRender = wp.components.ServerSideRender;
    const TextControl = wp.components.TextControl;
    const TextareaControl = wp.components.TextareaControl;
    const InspectorControls = wp.editor.InspectorControls;
    const { RichText } = wp.blockEditor;
    const { InnerBlocks } = wp.blockEditor;

    //Custom variable
    const allowedBlocks = [ \'custom-blocks/ref\' ] ;
    const blockTemplate = [ [ \'custom-blocks/ref\', {} ] ];

    registerBlockType( \'custom-blocks/ref-holder\', { // Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block.
        title: __( \'Reference Holder\', \'custom-blocks\' ), // Block title.

        category: \'custom_block\',
        keywords: [ __(\'ref\'), \'custom-blocks\'],
        attributes: {

        },
        supports: {
            customClassName: false,
            className: false,
        },

        edit(props) {

            return el(
                \'div\', { className: \'ref-block-holder\'},
                el(\'h2\', {}, __(\'Reference\', \'custom-blocks\') ), 
                el ( InnerBlocks,
                {
                    allowedBlocks: allowedBlocks,
                    template: blockTemplate
                } )
            );
        },

        save(props) {

            return el(
                \'div\', { className: \'ref-block-holder\'},
                el(\'h2\', {}, __(\'Reference\', \'custom-blocks\') ),
                el ( InnerBlocks.Content,
                {
                    allowedBlocks: allowedBlocks,
                    template: blockTemplate
                } )
            );
        },
    });
})();
JSON:

{ 
   "translation-revision-date":"2020-02-10 15:33+0800",
   "generator":"WP-CLI/2.4.0",
   "source":"ref_holder_block.js",
   "domain":"custom-blocks",
   "locale_data":{ 
      "messages":{ 
         "":{ 
            "domain":"custom-blocks",
            "lang":"zh_HK",
            "plural-forms":"nplurals=1; plural=0;"
         },
         "Reference":[ 
            "\\u53c3\\u8003\\u8cc7\\u6599"
         ],
         "Reference Holder":[ 
            ""
         ]
      }
   }
}
任何帮助都将不胜感激。谢谢

2 个回复
最合适的回答,由SO网友:Winston 整理而成

好吧,我自己找到了答案。如果你想让翻译成功,你必须注册脚本wp_register_script() 在里面init 中的操作挂钩和排队脚本wp_enqueue_scripts 钩子,如果你钩子wp_set_script_translationsinit.

如果你跑步wp_register_script() wp_enqueue_scripts 钩子和钩子wp_set_script_translationsinit, wp_set_script_translations 无法获取句柄,因为init 先开火!

最后

SO网友:Varstahl

我花了一整天的时间调试这个问题,通过查看代码,我可以猜到我们有相同的问题:

假设$script_handleCUSTOM-block-ref-holder, PHP文件几乎是正确的

  • wp_set_script_translations 需要完整的工作路径。在您的情况下,虽然您可能需要调整您的路径,但它应该类似于
    wp_set_script_translations(
      \'CUSTOM-block-ref-holder\',
      \'custom-blocks\',
      plugin_dir_path(__FILE__) . \'languages/\'
    );
    
    custom-blocks-zh_HK-CUSTOM-block-ref-holder.jsonmessages, 根据WP CLI的默认设置,但在我的测试中,它们甚至可以处理完全任意和随机的字符串
  • 参考:JavaScript i18n support in WordPress 5 | Advanced usage

    相关推荐