Advanced Custom Fields (ACF) 不支援 Custom Post Type Label WPML 翻譯的解決方法

找到這篇文章發現 ACF 6 之後的 Post Type 功能在 Label 上其實是不支援 WPML 翻譯的...

https://wpml.org/errata/advanced-custom-fields-6-1-custom-post-types-and-custom-taxonomies-labels-not-translatable-yet/

解決方法是自己攔截註冊,做出翻譯的 text domain

<?php

function modify_post_type_labels($args, $post_type) {

    $registered_post_types = [
        'my-custom-post-type',
    ];

    if (in_array($post_type, $registered_post_types)) {
        // Change the labels
        $args['labels'] = array_merge(
            $args['labels'],
            array(
                'name' => _x($args['labels']['name'], 'Post Type General Name', 'wp-forcecon-translate'),
                // Add any other labels you want to change
            )
        );
    }

    return $args;
}
add_filter('register_post_type_args', 'modify_post_type_labels', 10, 2);

如果有需要可以另外用 CSS 簡化 WPML 的字串翻譯介面

add_action('admin_head', 'wpml_string_translation_custom_css');

function wpml_string_translation_custom_css() {
    $user = wp_get_current_user();
    if(!in_array('administrator', $user->roles) ) {
        echo '
        <style>
            .wpml-string-translation-filter,
            #wpml-mo-scan-st-page,
            #wpml-language-of-domains-link,
            #icl-tr-opt,
            #dashboard-widgets-wrap {
                display: none;
            }
        </style>';
    }
}