• Resolved aprinciple

    (@aprinciple)


    There is a handler for the form, in which, using the WP_Error class, I add custom error text, but it is not translated into other languages, but only into 1 language – Russian, since the main site is in Russian

    <?php
    
    /**
    * Form contact us
    */
    
    global $errors;
    $errors = new WP_Error;
    
    function handler_contact_us() {
      global $errors;
    
      if( empty($name) ) {
    
      $errors->add( 'name', __('Fill in the field "Your   name"', 'my-text-domain') );
    }
    
      if( $errors->has_errors() ) {
        wp_send_json_error($errors);
      }
    
    }

    This error is displayed on the front through javascript, and the translation of the line in russian (default language on my website) is displayed there in all versions of the language, there are 5 of them on the site. Used WP Multisite.

    Everywhere in the theme everything is correctly translated into other languages, but in this form handler in other versions of the language it is translated only into the main language, not even English, namely into Russian

    That is, inside the function it does not work correctly, how to fix it?

    • This topic was modified 1 year, 6 months ago by aprinciple.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter aprinciple

    (@aprinciple)

    I tried to find out which locale is defined in this form handler file. It returns ru_RU, so in any version of the site (language) it returns only the russian translation.
    The next question is how to get the correct version of the locale in order to give the correct line feed for ajax processing

    • This reply was modified 1 year, 6 months ago by aprinciple.
    Thread Starter aprinciple

    (@aprinciple)

    Plugin does not work correctly when using wp_ajax.

    • This reply was modified 1 year, 6 months ago by aprinciple.
    Plugin Author Tim W

    (@timwhitlock)

    WordPress Ajax is actually an admin function (/wp-admin/admin-ajax.php). So probably WordPress is using the current user profile language setting and not the site language setting.

    This topic is marked as “not a support question” because runtime locale selection and text domain bootstrapping are not functions of my plugin.

    Thread Starter aprinciple

    (@aprinciple)

    Okay, but I’m not the first with such a problem and will not be the last in the future, instead of solving the issue, you will write that this is not included in the support of your plugin?

    Writing forms that use the wp_ajax common case, how to translate the theme if the forms on the site cannot be translated?

    I understand your point of view, but it’s better to help once, at least suggest how, because this case is not uncommon.

    • This reply was modified 1 year, 6 months ago by aprinciple.
    Plugin Author Tim W

    (@timwhitlock)

    You are certainly not the first. I’ve maintained this plugin for 6+ years and it’s installed on a million websites. I can assure you that I have helped with every issue you can imagine a lot more than once. I have spent thousands of hours replying to topics and writing FAQs and still the same questions come every day.

    It’s physically impossible to help every person with a WordPress localisation problem. Hence I limit my support to issues that are actually related to my plugin. That doesn’t include writing forms or implementing Ajax hooks.

    Thread Starter aprinciple

    (@aprinciple)

    Thanks for the plugin and the hard work, of course. But is there a solution to this problem at all? I’ll try to write to the github repository of this plugin, maybe someone there will respond and help.

    Plugin Author Tim W

    (@timwhitlock)

    If you want someone to help you write an Ajax hook and bootstrap the default site language, I suggest you ask on an open WordPress forum where people are happy to give coding advice.

    Thread Starter aprinciple

    (@aprinciple)

    Do you have paid support?

    Plugin Author Tim W

    (@timwhitlock)

    There is no paid support.

    Thread Starter aprinciple

    (@aprinciple)

    My working solution, add input with name “lang” in your form

    <input type="hidden" name="lang" value="<?= get_locale(); ?>">

    Then in the function.php file, we check if the request is AJAX, and if there is a lang parameter, then we override the locale.

    if( wp_doing_ajax() && $_REQUEST['lang'] ) {
      $locale = $_REQUEST['lang'];
      add_filter('determine_locale', function() use ($locale) { 
        return $locale;
      });
    }

    The end. It’s working.

    • This reply was modified 1 year, 6 months ago by aprinciple.
    • This reply was modified 1 year, 6 months ago by aprinciple.
    • This reply was modified 1 year, 6 months ago by aprinciple.
    Plugin Author Tim W

    (@timwhitlock)

    Looks good. Thanks for posting.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Text for custom error (WP_Error) is translated into language default page’ is closed to new replies.