0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WP-CLIを使った翻訳ファイルの作成

Posted at

wp-cliを使って翻訳設定ファイルを作成する。

参考URL-本家マニュアル

ざっとした作成の流れのメモとなります。

WP-CLIのインストールなどの設定

WP-CLI公式

翻訳ファイルの作成

JavaScriptの翻訳ファイルの作成

JavaScript内の文字を翻訳するためのファイルは JED 1.x JSON フォーマット形式のファイルが必要。JED 翻訳ファイルを作成するには、まずテキストから文字列を抽出する必要があります。一般にすべての言語ファイルはプラグインの languages ディレクトリにあります。WP-CLI を使う場合は、該当ディレクトリで以下のコマンドを実行するとlanguagesディレクトリ内にxxx.pot ファイルを作成されます

mkdir languages
wp i18n make-pot ./ languages/myguten.pot

プロジェクトからのすべての翻訳可能文字列を含むファイル myguten.pot が生成されます。

# Copyright (C) 2020 
# This file is distributed under the same license as the Simple Block plugin.
msgid ""
msgstr ""
"Project-Id-Version: Simple Block\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/simple-block\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2020-05-04T10:48:26+00:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.4.0\n"
"X-Domain: myguten\n"

#. Plugin Name of the plugin
#: block.js:6
msgid "Simple Block"
msgstr ""

#: block.js:13
#: block.js:21
msgid "Hello World"
msgstr ""

ここで msgid は翻訳される文字列で msgstr は実際の翻訳です。POT ファイルでは msgstr は常に空です。

この POT ファイルを新しい翻訳のテンプレートとして使ることができます。翻訳予定の言語コードを使用して ファイルをコピー してください。この例では日本語 (ja) を使用します。

cd languages/
cp myguten.pot myguten-ja.po

この簡単な例であればエディターで .po ファイルを編集し、すべての msgstr に対して翻訳を追加できますが、もっと大量で複雑な翻訳用にGlotPress や Poedit などの支援ツールがあります。

また Language: ja パラメータを追加する必要があります。完全な myguten-ja.po 翻訳ファイルは以下のようになります。

# Copyright (C) 2020 
# This file is distributed under the same license as the Simple Block plugin.
msgid ""
msgstr ""
"Project-Id-Version: Simple Block\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/simple-block\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: ja\n" #←言語指定を追加
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2020-05-04T10:48:26+00:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.4.0\n"
"X-Domain: myguten\n"

#. Plugin Name of the plugin
#: block.js:6
msgid "Simple Block"
msgstr "シンプルブロック"

#: block.js:13
#: block.js:21
msgid "Hello World"
msgstr "こんにちは"

翻訳ファイルを作成する最後のステップは myguten-ja.po から必要な JSON フォーマットへの変換です。これには WP-CLI の wp i18n make-json コマンドを使うことができます。ただし WP-CLI v2.2.0 以上が必要です。

wp i18n make-json myguten-ja.po --no-purge

#作業ユーザーがrootの場合--alloe-rootを追加
wp i18n make-json myguten-ja.po --no-purge --alloe-root

以下のようなJSON ファイル myguten-ja-[md5].json が生成されます。

{
    "translation-revision-date": "YEAR-MO-DA HO:MI+ZONE",
    "generator": "WP-CLI\/2.4.0",
    "source": "block.js",
    "domain": "messages",
    "locale_data": {
        "messages": {
            "": {
                "domain": "messages",
                "lang": "ja",
                "plural-forms": "nplurals=2; plural=(n != 1);"
            },
            "Simple Block": [
                "\u30b7\u30f3\u30d7\u30eb\u30d6\u30ed\u30c3\u30af"
            ],
            "Hello World": [
                "\u3053\u3093\u306b\u3061\u306f"
            ]
        }
    }
}

PHPやJSONのための翻訳ファイルの作成

PHPやJSON用の翻訳ファイルはmo形式のファイルとなります。
poファイルからmoファイルの作成もWP-CLIに用意されていますので、そちらを利用します。

wp i18n make-mo myguten-ja.po

##作業ユーザーがrootの場合--alloe-rootを追加
wp i18n make-mo myguten-ja.po

翻訳ファイルのロード

各翻訳ファイルをロードする方法となります。

プラグインでの翻訳ファイルのロード

JavaScript用の翻訳ファイルのロード

function my_block_translation() {
	//JavaScript向け翻訳はinitアクションにて設定する
	wp_set_script_translations('myguten-box-editor-script','myguten',plugin_dir_path( __FILE__ ) . 'languages' );
}
add_action('init','my_block_translation');

wp_set_script_translations() | Function | WordPress Developer Resources

PHPやJSON用の翻訳ファイルのロード

function my_plugin_translation() {
		//moファイルのロードはplugins_loadedアクションを使う
		//もしinitアクションで使いたい場合は、ブロック登録
		//(register_block_type_from_metadata)よりも前に記述する
      load_plugin_textdomain( 'myguten', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );

}
add_action( 'plugins_loaded', 'my_plugin_translation' );

load_plugin_textdomain() | Function | WordPress Developer Resources

テーマでの翻訳ファイルのロード

PHPやJSON用の翻訳ファイルのロード

load_theme_textdomain( 'theme_textdomain', get_template_directory() . '/languages' );
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?