Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom post type translatable labels not working #26

Open
jhhazelaar opened this issue Aug 17, 2021 · 3 comments
Open

Custom post type translatable labels not working #26

jhhazelaar opened this issue Aug 17, 2021 · 3 comments

Comments

@jhhazelaar
Copy link

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

@adamtomat
Copy link
Member

Hey thanks for reporting the issue. We'll look into whether we can apply your fix to the Lumberjack core. In the meantime, you can replace the core CustomPostTypeServiceProvider with your own version.

In config/app.php all the service providers are defined here:
https://github.com/Rareloop/lumberjack/blob/master/config/app.php#L22

You should be able to add your own service provider inside of your theme. Here are some docs on creating a service provider if that's new to you:
https://docs.lumberjack.rareloop.com/container/service-providers#creating-service-providers

Once you have your own service provider, replace our one with yours in config/app.php. For example:

/**
     * List of providers to initialise during app boot
     */
    'providers' => [
        Rareloop\Lumberjack\Providers\RouterServiceProvider::class,
        Rareloop\Lumberjack\Providers\WordPressControllersServiceProvider::class,
        Rareloop\Lumberjack\Providers\TimberServiceProvider::class,
        Rareloop\Lumberjack\Providers\ImageSizesServiceProvider::class,
        // Rareloop\Lumberjack\Providers\CustomPostTypesServiceProvider::class,
        Rareloop\Lumberjack\Providers\MenusServiceProvider::class,
        Rareloop\Lumberjack\Providers\LogServiceProvider::class,
        Rareloop\Lumberjack\Providers\ThemeSupportServiceProvider::class,
        Rareloop\Lumberjack\Providers\QueryBuilderServiceProvider::class,
        Rareloop\Lumberjack\Providers\SessionServiceProvider::class,
        Rareloop\Lumberjack\Providers\EncryptionServiceProvider::class,

        // Application Providers
        App\Providers\AppServiceProvider::class,
        App\Providers\CustomPostTypesServiceProvider,
    ],

Hope this helps!

@jhhazelaar
Copy link
Author

@adamtomat thanks for the temp fix! Great work. I've manage to get the CPT labels translated with my own Service Provider

@jhhazelaar
Copy link
Author

Small update if you are looking for this. I think this is a beter solutions for the service provider:

<?php

namespace App\Providers;

use Rareloop\Lumberjack\Config;
use Rareloop\Lumberjack\Providers\ServiceProvider;

class CustomBlocksServiceProvider extends ServiceProvider
{
    public function boot(Config $config)
    {
        $blocks = $config->get('blocks.register');

        foreach ($blocks as $block) {
            add_action('init', [$block, 'register']);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants