[Resolved] An issue with the search results language

Home Forums Support [Resolved] An issue with the search results language

Home Forums Support An issue with the search results language

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #2213013
    Leo

    I have my site in three languages with Polylang. Catalan is the default language, while Spanish and English are the secondary languages.

    I’ve customized the 404 error page using a plugin and made three versions of it -one for each language. I also added a search box block on each error page.

    There are no issues with that search box in the default language. The problem occurs when using the search box in Spanish or English: in those cases, the system seeks matches in Catalan instead of the current language.

    For example, if you search the word “balloon” from the error page in English, you will get no results, despite some pages matching that word. Besides, the system jumps to the Catalan version of the site when displaying the no-matches notification.

    Interestingly, this problem doesn’t happen when using the magnifying glass in the menu.

    #2213089
    David
    Staff
    Customer Support

    Hi there,

    so the Search form in the 404 template has the same action attribute for all languages ie. the main domain not the subfolder. So all searches will be the same.

    Instead of using the Plugin to create the different 404s – have you tried using the Block Element – Content Template ?

    #2215407
    Leo

    Thank you, David. I have created the error pages with Elements and got rid of the plugin. Elements are so powerful and helpful!

    However, the problem with the language box remains the same.

    The problem occurs when using the search box in Spanish or English: in those cases, the system seeks matches in Catalan instead of the current language.

    For example, if you search the word “balloon” from the error page in English, you will get no results, despite some pages matching that word. Besides, the system jumps to the Catalan version of the site when displaying the no-matches notification.

    Interestingly, this problem doesn’t happen when using the magnifying glass in the menu.

    #2218146
    Leo

    Hi guys,

    I’m still struggling to change the language of the search box results.

    Results show up in Catalan, even if the search comes from the English or Spanish version of the website.

    However, searches using the magnifying glass in the menu are just fine. They match the language.

    If you can point me on how to fix this problem, I’ll appreciate it.

    Thank you! 🙂

    #2218328
    David
    Staff
    Customer Support

    Sorry we missed your last reply.
    So the core Search Block doesn’t respect the current URL.

    You could try adding this PHP Snippet to create your own search form shortcode:

    add_shortcode( 'search_form', function() {
        ob_start();
        
        ?>
        <form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
        <label>
        <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
        <input type="search" class="search-field"
            placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>"
            value="<?php echo get_search_query() ?>" name="s"
            title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
        </label>
        <input type="submit" class="search-submit"
            value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
        </form>
    
        <?php
        
        return ob_get_clean();
    } );

    Then instead of add the Search Block add shortcode: [search_form]

    #2219890
    Leo

    Thank you for your help, David. I followed your suggestion, but unfortunately the problem keeps the same.

    #2220001
    David
    Staff
    Customer Support

    Can you clear any caches and try again ?

    #2220514
    Leo

    I cleared the website cache as well as my browser cache, but the problem persists.

    #2220949
    David
    Staff
    Customer Support

    Ok i am sure there is probably a much easier way to do this, and you may want to ask Polylang for their input. But in the meantime, try updating the Shortcode snippet to:

    add_shortcode( 'search_form', function() {
        ob_start();
        
        ?>
        <form role="search" method="get" class="search-form" action="<?php echo home_url()  . '/' . pll_current_language(); ?>">
        <label>
        <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
        <input type="search" class="search-field"
            placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>"
            value="<?php echo get_search_query() ?>" name="s"
            title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
        </label>
        <input type="submit" class="search-submit"
            value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
        </form>
    
        <?php
        
        return ob_get_clean();
    } );

    The change is to just this line:

    <form role="search" method="get" class="search-form" action="<?php echo home_url() . '/' . pll_current_language(); ?>">

    #2221716
    Leo

    Thank you, David, it’s fantastic! I’m very grateful. 🙂

    A similar thread has been open in the Poylang forum for more than two years and a half. A few people seem to suffer this issue, and the plugin authors haven’t yet fixed it. I posted the workaround you gave me just in case it could help anybody else.

    Just one more thing. The search form displays the word “Search,” which is okay in English, but in Catalan and Spanish, it should say “Cerca” and “Busca.”

    I got the idea to create three snippets with three different shortcodes, one for each language, but maybe there is an easier way of doing this. What do you recommend?

    #2221997
    David
    Staff
    Customer Support

    So this line:

    placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>"

    you should be able to change it to this:

    placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder', 'generatepress' ) ?>"

    that will include the GP text domain for translation.

    #2223230
    Leo

    The text “Search …” doesn’t show up in Polylang’s string translations. I tried changing 'generatepress' to 'gp-premium', but it isn’t working either.

    #2223420
    David
    Staff
    Customer Support

    You can change the placeholder ie. 'Search …' to anything you want in the PHP Snippet. Try: 'Search'

    #2224915
    Leo

    I don’t know if I got you correctly. I ended up making three different snippets, one for each language, and changed the placeholder text in each of them. Was that what you meant?

    #2225187
    David
    Staff
    Customer Support

    What i meant was chaning the placeholder text to something Polylang could translate 🙂

    But it sounds like using 3 x different snippets is the better solution 🙂

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.