1

I wrote a plugin that creates a custom post type and I tried to translate it using Poedit, but it's not fully working. I did translate a few other plugins before and I'm not sure what's causing this error:

The plugin's name and description are correctly translated in the backend, but no post type names are translated.

Full Example

File Structure

test
├── languages
│   ├── test-de_DE.mo
│   ├── test-de_DE.po
│   └── test.pot
└── test.php

I defined the Text Domain test and Domain Path \languages in the plugin header in test.php.

The post type is defined in the same file like so:

test.php

<?php

/**
 * Plugin Name: A Test
 * Description: A Test Plugin
 * Version: 1.0.0
 * Text Domain: test
 * Domain Path: /languages
 */

function test_custom_post_types()
{
    register_post_type('test_post', [
        'labels' => [
            'name' => __('Test Posts', 'test'),
            'menu_name' => __('Test Posts', 'test'),
        ],
        'public' => true,
    ]);
}
add_action('init', 'test_custom_post_types');

I created a .pot file using the wp-cli command i18n make-pot .\wp-content\plugins\test\

languages/test.pot

# Copyright (C) 2020 
# This file is distributed under the same license as the A Test plugin.
msgid ""
msgstr ""
"Project-Id-Version: A Test 1.0.0\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/test\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-06-12T15:08:36+02:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.4.0\n"
"X-Domain: test\n"

#. Plugin Name of the plugin
msgid "A Test"
msgstr ""

#. Description of the plugin
msgid "A Test Plugin"
msgstr ""

#: test.php:15
#: test.php:16
msgid "Test Posts"
msgstr ""

Then I used Poedit to create the german .po and .mo files.

With the plugin activated I can see the plugin's name and description in german, but the added post type still shows up in english.

This is the .po file generated by Poedit:

languages/test-de_DE.po

# Copyright (C) 2020 
# This file is distributed under the same license as the A Test plugin.
msgid ""
msgstr ""
"Project-Id-Version: A Test 1.0.0\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/test\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2020-06-12T15:08:36+02:00\n"
"PO-Revision-Date: 2020-06-12 15:09+0200\n"
"X-Generator: Poedit 2.3.1\n"
"X-Domain: test\n"
"Last-Translator: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Language: de_DE\n"

#. Plugin Name of the plugin
msgid "A Test"
msgstr "Ein Test"

#. Description of the plugin
msgid "A Test Plugin"
msgstr "Ein Test-Plugin"

#: test.php:15 test.php:16
msgid "Test Posts"
msgstr "Test Beiträge"

As a bonus, it also doesn't translate (server side rendered) Gutenberg blocks that I created in the same plugin.

Any ideas how to fix the missing translations? Thanks!

1

Seems like you forgot to load the text domain

The plugin handbook says that:

You need to load the MO file with your plugin’s translations. You can load them by calling the function load_plugin_textdomain() (and load_muplugin_textdomain() if your plugin will be used as a Must-Use plugin). This call loads {text-domain}-{locale}.mo from your plugin’s base directory.

So based on the example there, you could add this to your main plugin file (test.php):

function test_load_plugin_textdomain() {
    load_plugin_textdomain( 'test', false, basename( __DIR__ ) . '/languages/' );
}
add_action( 'plugins_loaded', 'test_load_plugin_textdomain' );
1
  • 1
    Oh yes, that's correct! I totally skipped that part and always left it out! I wonder how I never noticed missing translations in other plugins D: Thanks! – Cebbi Jun 13 '20 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.