plugins – How to use the WordPress localization API inside Classes and Functions

Question

I’m trying to translate a word that will be a term within WordPress, I use some functions that need to have that word already translated and I can’t do it. I only managed to do it at the time of applying the term, but in another function where I need to compare if the word is translated or not, I can’t get the translation.

I decided to make the variable global but that didn’t work either.
I should also mention that with each change I regenerate the .po and .mo files so that the locations are updated.
I’m pretty sure this is a structure problem in the class and functions, I’m taking my first steps in development.

I appreciate who can help

<?php

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

// This variable (which will be a term) will be applied when the user does not choose any option
// and the attachment is uploaded to the default location.
$default = __( '- Default', 'mf-domain' );

class Taxonomy {

    public function __construct() {
        // Register new taxonomy to apply to attachments
        function mf_add_taxonomy() {

            $args = array(
                'public' => false,
                'show_ui' => false,
                'show_in_rest' => false,
                'show_admin_column' => false,
                'rewrite' => false,
            );

            register_taxonomy( 'directory', 'attachment', $args );
        }
        add_action( 'init', 'mf_add_taxonomy' );

        // Set term on upload
        add_action('add_attachment', function($post_id) {

            $cookie = sanitize_text_field( filter_input( INPUT_COOKIE, 'mf_cookie' ) );

            global $default;
            
            // $default = __( '- Default', 'mf-domain' ); // Here it works, at the moment of adding the term it delivers the translation

            if ( empty ( $cookie ) ) {
                wp_set_post_terms( $post_id, $default, 'directory' );
            }
            else {
                wp_set_post_terms( $post_id, $cookie, 'directory' );
            }

        } );

        // In this plugin, the WordPress localization API is implemented to perform the translations,
        // but what would happen if the user downloads the plugin without translations, uses it and then downloads the translations?
        // The result would be that the user would have two terms (- Default) one translated and one not.
        // For a specific case like that we apply the following correction.
        function mf_fix_default_term() {
            if ( get_locale() !== 'en_*') {

                global $default;
                
                // $default = __( '- Default', 'mf-domain' ); // It doesn't work here, it doesn't translate

                $term_default = term_exists( '- Default', 'directory' );
                $term_translated = term_exists( $default, 'directory' );

                if ( $term_default !== 0 && $term_default !== null && $term_translated !== 0 && $term_translated !== null ) {

                    if ( $default !== '- Default' ) {

                        $posts_id = get_posts( array(
                            'post_type' => 'attachment',
                            'numberposts' => -1,
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'directory',
                                    'field' => 'name', 
                                    'terms' => '- Default',
                                )
                            )
                        ));

                        foreach( $posts_id as $post_id ) {
                            wp_set_post_terms( $post_id, $default, 'directory' );
                        }
                        wp_delete_term( '- Default', 'directory' );
                    }
                }
            }
        }
        add_action( 'init', 'mf_fix_default_term' );
    }
}

new Taxonomy;

// Notes:
// In the last function the translation doesn't work so I decided to make the variable
// ($default) global, but that doesn't work either. It is not translated anywhere.
// It is important to note that with each change I generate the translation files again so that it takes the location.

0
choseɳ 4 months 2022-04-15T12:04:06-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse