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

Support translations #94

Open
bartkleinreesink opened this issue Sep 11, 2024 · 1 comment
Open

Support translations #94

bartkleinreesink opened this issue Sep 11, 2024 · 1 comment

Comments

@bartkleinreesink
Copy link

bartkleinreesink commented Sep 11, 2024

I like the package, but it lacks one very important feature for me to start using it: translation support.

I am not a native English speaker and my clients mostly aren't, either. So when I create a website, I want to make it at least bilingual.

Of course, I could extend the classes and move my way around the current limitations, but that is very cumbersome if it needs to be done for every project.

The biggest problem is that there is no check if the init hook has been fired already. My suggestion would be to call did_action('init'). If true, call the methods directly, otherwise, register them to the init hook.

This is a problem because I'm not able to choose when the post type is registered in the hook chain. So when I have translated strings in the declaration of the post type, they are not available at the time when the post type gets registered, because they are loaded too early. I want to decorate the class after the init hook has been fired.

@jjgrainger
Copy link
Owner

Thanks @bartkleinreesink

Translations should be supported after the changes made in 2.0.

Can you provide more details on how you are registering your PostTypes and translations?

The register method handles all the hooks needed to register post types, taxonomies, columns to WordPress so any of the PostType code should not be wrapped in any actions/filters otherwise this could lead to unexpected results. For example:

// This would lead to the `register` method not working correctly and any init actions would be registered too late.
add_action( 'init', function() {
    $books = new PostType( [
        'name'     => 'book',
        'singular' => __( 'Book', 'YOUR_TEXTDOMAIN' ),
        'plural'   => __( 'Books', 'YOUR_TEXTDOMAIN' ),
        'slug'     => 'books',
    ] );

    $books->register();
});

I'm wary of adding a did_action check as, although it may solve this use case, it may have other side effects.

Thanks

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