首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使古腾堡块翻译工作?

问如何使古腾堡块翻译工作?
EN

Stack Overflow用户
提问于 2020-08-17 03:23:28
回答 1查看 174关注 0票数 0

我刚刚构建了一个Gutenberg块,我想把它翻译成不同的语言。我正在使用葡萄牙语的WordPress,我已经做了葡萄牙语的翻译。我遵循了documentation https://developer.wordpress.org/block-editor/developers/internationalization/中提供的所有步骤,但它仍然不起作用。

我的插件叫做spoiler-alert。

我已经在/spoiler-alert/languages文件夹中创建了一个名为spoileralert.pot的.pot文件。然后,我生成了md5文件,甚至使用脚本处理程序创建了一个包含所有字符串的新文件。我的语言文件夹结构如下所示:

代码语言:javascript
复制
languages
- spoileralert.pot
- spoileralert-pt_BR.po
- spoileralert-pt_BR-<HASH-CODE-1>.json
- spoileralert-pt_BR-<HASH-CODE-2>.json
- spoileralert-pt_BR-<HASH-CODE-3>.json
- spoileralert-pt_BR-spoileralert.json

这是我的PHP文件spoiler-alert.php

代码语言:javascript
复制
function spoiler_alert_spoiler_alert_block_init() {
    $dir = dirname( __FILE__ );

    $script_asset_path = "$dir/build/index.asset.php";
    if ( ! file_exists( $script_asset_path ) ) {
        throw new Error(
            'You need to run `npm start` or `npm run build` for the "spoiler-alert/spoiler-alert" block first.'
        );
    }
    $index_js     = 'build/index.js';
    $script_asset = require( $script_asset_path );
    wp_register_script(
        'spoiler-alert-spoiler-alert-block-editor',
        plugins_url( $index_js, __FILE__ ),
        $script_asset['dependencies'],
        $script_asset['version']
    );

    $editor_css = 'build/index.css';
    wp_register_style(
        'spoiler-alert-spoiler-alert-block-editor',
        plugins_url( $editor_css, __FILE__ ),
        array(),
        filemtime( "$dir/$editor_css" )
    );

    $style_css = 'build/style-index.css';
    wp_register_style(
        'spoiler-alert-spoiler-alert-block',
        plugins_url( $style_css, __FILE__ ),
        array(),
        filemtime( "$dir/$style_css" )
    );

    register_block_type( 'spoiler-alert/spoiler-alert', array(
        'editor_script' => 'spoiler-alert-spoiler-alert-block-editor',
        'editor_style'  => 'spoiler-alert-spoiler-alert-block-editor',
        'style'         => 'spoiler-alert-spoiler-alert-block',
    ) );


    wp_set_script_translations( 'spoileralert', 'spoiler-alert', plugin_dir_path( __FILE__ ) . '/languages' );

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

在我的.js文件中,我使用:import { __ } from '@wordpress/i18n';导入了翻译包

我使用的翻译如下:title: __( 'Spoiler Alert', 'spoiler-alert' ),

如何才能使翻译正确显示?

EN

回答 1

Stack Overflow用户

发布于 2021-02-26 15:25:54

它已经有一段时间了,但它可能仍然对您或其他人有用。我只是简单地复制和粘贴我已经回答here的内容。

我已经经历过这个过程好几次了,所以我百分之百地确定这是正常工作的。仔细检查每一个步骤,你应该设法解决你可能遇到的任何问题;)

翻译终于得到了正确的开发和记录:https://developer.wordpress.org/block-editor/developers/internationalization/

基本上,您需要:

  1. 使用wp-i18n包来定义项目中的哪些字符串用于translatable.
  2. Compile您的代码(wp i18n make-pot将在编译后的代码中搜索翻译字符串,而不是在源代码中搜索)。
  3. 使用WP-CLI (wp i18n make-pot)创建.pot文件。就像我们一直使用WP主题一样,基于以前创建的.pot对.po文件进行plugins.
  4. Generate并翻译其内容。
  5. 再次使用WP-CLI (wp i18n make-json)将我们的.po文件转换为所需的JSON格式。
  6. 使用wp_set_script_translations PHP函数加载翻译文件。<代码>H222<代码>G223
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63441080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档