Skip to content

Custom post type translatable labels not working #26

@jhhazelaar

Description

@jhhazelaar

Thanks for creating lumberjack. This is the first time we use it and I've run into a problem with translations. We register custom post type as show in the documentation:

return [
    'register' => [
        App\PostTypes\Offer::class,
    ],
];

The custom post type is registert and we can use it in the admin and have an archive and detail view.

In the getPostTypeConfig we try to translate the labels off the custom post type:

protected static function getPostTypeConfig()
{
    return [
        'public' => true,
        'show_in_rest' => true,
        'capability_type' => 'page',
        'has_archive' => true,
        'query_var' => true,
        'label'  => __('Offers', 'text-domain'),
        'labels' => [
            'name' => __('Offers', 'text-domain'),
            'singular_name' => __('Offer', 'text-domain'),
        ],
    ];
}

In poedit we can create a .po file in the language folder. In the functions.php we load the text domain like so:

add_action('after_setup_theme', function () {
    load_theme_textdomain('text-domain', get_template_directory() . '/languages');
});

If checked the loaded paths and the textdomain path is correct. But still the labels aren't translated. After some testing with displaying translated text in several places and in different actions. I've discovered that if I put the translation in a init action it get translated. The CustomPostTypesServiceProvider register the custom post type not in action. If I wrap the post type register in an init action, the labels get translation. Here is my quick and dirty fix for the CustomPostTypesServiceProvider:

public function boot(Config $config)
{
    add_action('init', function () use ($config) {
        $postTypesToRegister = $config->get('posttypes.register');

        foreach ($postTypesToRegister as $postType) {
            $postType::register();
        }
    });
}

Is there any possible way of register the post type in the proper action hook? So that the translation is working.

What versions of software are you using?

Operating System: MacOS 11.5

PHP Version: 7.4.1

Lumberjack Version: 5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions