首页 > 解决方案 > 古腾堡块翻译

问题描述

我有带有 Polylang 插件的多语言网站。所有 PHP 翻译都有效,但现在我在我的主题中创建了自定义 Gutenberg 块,我正在努力将其翻译成英文。

我正在粘贴整个代码,因为我没有更多的想法去哪里寻找错误。

这是我的块src/index.js

import { __ } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import { InnerBlocks } from '@wordpress/block-editor';

registerBlockType( 'neuro/references-block', {
    title: __( 'Bibliografia', 'neuro' ),
    icon: 'book',
    category: 'text',

    edit: ( props ) => {

        const ALLOWED_BLOCKS = [ 'core/list', 'core/paragraph' ];

        return (
            <div className={ props.className }>
                <h4>{ __( 'Bibliografia', 'neuro' ) }</h4>
                <InnerBlocks allowedBlocks={ ALLOWED_BLOCKS } />
            </div>
        );
    },
    save: ( props ) => {
        
        return (
            <div className={ props.className }>
                <h4>{ __( 'Bibliografia', 'neuro' ) }</h4>
                <InnerBlocks.Content />
            </div>
        );
    },
} );

它被编译成build/index.js文件。

我创建了neuro-en_GB-neuro-references-block.json放置在languages文件夹中的文件,这是它的内容:

{"translation-revision-date":"","generator":"WP-CLI\/2.5.0","source":"build\/index.js","domain":"messages","locale_data":{"messages":{"":{"domain":"messages","lang":"en-gb","plural-forms":"nplurals=2; plural=(n != 1);"},"Bibliografia":["References"]}}}

functions.php文件中,我像这样注册古腾堡块:

function neuro_register_gutenberg_block() {

  wp_register_script(
    'neuro-references-block',
    get_template_directory_uri() . '/build/index.js',
    array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-i18n', 'wp-polyfill' ),
    '1.0.0'
  );

  register_block_type( 'neuro/references-block', array(
    'editor_script' => 'neuro-references-block',
  ) );

}
add_action( 'init', 'neuro_register_gutenberg_block' );

我设置这样的翻译:

function neuro_set_script_translations() {
  wp_set_script_translations( 'neuro-references-block', 'neuro', get_template_directory() . '/languages' );
}
add_action( 'init', 'neuro_set_script_translations' );

我的wp_set_script_translations函数返回true

有什么想法有什么问题吗?谢谢

标签: wordpresswordpress-gutenberggutenberg-blockspolylang

解决方案


推荐阅读