All Low add-ons are now owned by EEHarbor. Read the blog post.

Support archive

Multi-Language Search Result page

Ryan Taylor 10 Jan 2013 14:28 question, complete

Hi,

I'm using Structure to manage a multi-language site e.g.:

Home
Products
Services
About
de
- Products
- Services
- About
fr
- Products
- Services
- About

I'm new to Low Search so I'm experimenting but ideally I want to have:

domain.com/search/results

For all my English page results.

domain/com/de/search/results

For all my German page results etc.

However I don't have the different language entries in different channels. They're stored in the same channel(s) using the same Custom Fields for entry etc.

So my question is, how would I go about getting my desired behaviour working?

Thanks in advance.

Ryan

Replies

  1. Ryan Taylor 10 Jan 2013 15:06

    Additional.

    I've experimented with created a channel purely for search result pages. And then adding that in on Structure and then create a page for English, a child page for German etc.

    This results in my being:

    domain.com/search/

    and

    domain.com/de/search/

    Using entries gives me the added advantage of entering different language titles for the tops of the result pages.

    This works for my English version. I have this for the search form:

     
    {exp:low_search:form
    form_class="site-search" collection="page_standalone|page_top_level|partials_2_column|partials_sub_2_column"
    search_mode="all"
    result_page="search"}
    <input id="search-string" type="text" name="keywords" />
    <input id="search-submit" type="image" src="{site_url}/assets/images/site/search-icon@2x.png" height="21" width="21" name="search-submit" />
    {/exp:low_search:form}


    Then I'm just using the example code to see if it works:

     
    {exp:low_search:results query="{segment_2}" limit="10"}
    {if count == 1}<ol>{/if}
    <li><a href="{comment_url_title_auto_path}">{title}</a></li>
    {if count == total_results}</ol>{/if}
    {if no_results}No search results{/if}
    {/exp:low_search:results}


    And as I say it does for the English version. For the German page:

    domain.com/de/search/

    I can visit that URL and it appears and shows "No search results" but as soon as there's a search parameter in the URL I get redirected to a 404.

    I've tried changing the query parameter from {segment_2} to {segment_3} but that makes no difference.

    Does anyone have any suggestions?

    Thanks

    Ryan

  2. Low 10 Jan 2013 15:24

    As for getting the right entries for the right language: you'll need a way to differentiate the entries in the same way that you would for a channel:entries tag. So, by custom field, status, channel, or something like that. Then you can tell Low Search to just return entries with custom_field="de" or status="de" etc.

    Using Structure, the language is implied somewhere in the URI, which isn't searchable, so there's no way for Low Search to know which entry is in which language.

    As for generating the actual results: Structure does away with the native URI logic, so you'll need something like Freebie to get to the original segment variables.

    Check the template debugger for more info on what goes on behind the scenes.

  3. Ryan Taylor 10 Jan 2013 15:55

    Thanks for the reply. I've made good headway using Freebie. Now both English and German search results pages are working.

    However I need to dynamically switch the result_page="" parameter on the {exp:low_search:form} tag but I'm having trouble.

    I've written my own little plugin to determine the viewing language:

    {exp:viewing_lang} this returns a language code.

    So I want to set result_page="" to:

    result_page="en/search"
    result_page="de/search"
    result_page="fr/search"

    Depending on which language is being viewed.

    I was trying to do this with PHP like so:

     
    <?php $result_page = 'result_page="'.'{exp:viewing_lang}'.'/search"'; ?>
    {exp:low_search:form form_class="site-search" collection="page_standalone|page_top_level|partials_2_column|partials_sub_2_column" search_mode="all" <?=$result_page;?>}


    But is simply ignored and it ends up defaulting to search/results. If I echo I get what I expect:

    result_page="en/search"

    And ideas on how I can make this work?

    Thanks

    Ryan

  4. Low 10 Jan 2013 15:58

    Try not adding it as a tag parameter, but as a hidden input field instead:

    <input type="hidden" name="result_page" value="{exp:viewing_lang}/search" />

  5. Ryan Taylor 10 Jan 2013 16:07

    Awesome. That worked perfectly!

    My only issue now then is I need to switch the query="" parameter on the results page to use:

    query="{freebie_2}"

    for English pages and:

    query="{freebie_3}"

    for everything else...

    Again I've tried doing a check with PHP but this doesn't work. Tried two approaches:

     
    {if "{exp:viewing_lang}" == "en"}
    <?php $query = "{freebie_2}"; ?>
    {if:else}
    <?php $query = "{freebie_3}"; ?>
    {/if}
    {exp:low_search:results query="<?=query;?>" limit="10"}


    And also:

     
    {if "{exp:viewing_lang}" == "en"}
    {exp:low_search:results query="{freebie_2}" limit="10"}
    {if:else}
    {exp:low_search:results query="{freebie_3}" limit="10"}
    {/if}


    which results in a parsing error.

    If you can answer this last problem, you'll officially be my new hero!

  6. Ryan Taylor 10 Jan 2013 16:28

    Hmm... I've found a work around though certainly not ideal:

     
    {if "{exp:viewing_lang}" == "en"}
    {exp:low_search:results query="{freebie_2}" limit="10"}
    {if count == 1}<ol>{/if}
    <li><a href="{comment_url_title_auto_path}">{title}</a></li>
    {if count == total_results}</ol>{/if}
    {if no_results}No search results{/if}
    {/exp:low_search:results}
    {if:else}
    {exp:low_search:results query="{freebie_3}" limit="10"}
    {if count == 1}<ol>{/if}
    <li><a href="{comment_url_title_auto_path}">{title}</a></li>
    {if count == total_results}</ol>{/if}
    {if no_results}No search results{/if}
    {/exp:low_search:results}
    {/if}


    Duplicating all the code just to change the parameter. But... it works. So unless you can suggest a better alternative. I'll roll with that for the time being.

    Thanks for you help!

  7. Low 11 Jan 2013 08:28

    Your previous attempt won't work because of parse order. Advanced conditionals, PHP (on input or output) and tags... Not gonna work.

    The attempt above will work, but both tags will be executed, which you might notice in the performance.

    You might want to take a look at add-ons like Switchee or perhaps Stash to gain some performance there; but that's up to you.

  8. Ryan Taylor 11 Jan 2013 09:34

    Thanks for the tip! I'd not heard of Switchee. I'll use that. Thanks.