Jump to content

How can i get the current language for ajax search


kkalgidim
 Share

Recommended Posts

Hi,

i am using multi language system

i have ajax search system (live search)

When i enter the word into search box it find the related topics but also show the other langugae titles.

How can i made it for current language?

Here is the code that i use

Quote

// Check if ajax request
if($config->ajax) {

    $searchresults = $pages->find("has_parent!=2"); // Find all published pages and save as $results
    header("Content-type: application/json"); // Set header to JSON
    echo $searchresults->toJSON(); // Output the results as JSON via the toJSON function

} else {

result attached as image

Untitled-1.jpg

Link to comment
Share on other sites

@bernhard I used the module below. I dont really know about json files. Just what i want is live search system. 

 

https://modules.processwire.com/modules/pages2-json/

 

What does this do?

Simply adds method toJSON() to Page and PageArray elements, is capable to converting image and file arrays to URL containers, travels recursively all objects before outputs actual JSON.

 

Link to comment
Share on other sites

Does this works?


if($config->ajax) {

	$language = $languages->getLanguage();
    $searchresults = $pages->find("has_parent!=2, $language");
    header("Content-type: application/json"); // Set header to JSON
    echo $searchresults->toJSON(); // Output the results as JSON via the toJSON function

} else { ...

 

Link to comment
Share on other sites

2 hours ago, 3fingers said:

Does this works?



if($config->ajax) {

	$language = $languages->getLanguage();
    $searchresults = $pages->find("has_parent!=2, $language");
    header("Content-type: application/json"); // Set header to JSON
    echo $searchresults->toJSON(); // Output the results as JSON via the toJSON function

} else { ...

 

no ?

i also tried
 

Quote

 

 $currentLanguage = $user->language;

$searchresults = $pages->find("has_parent!=2, $currentlanguage");

 

but its not working. i think find function does not accept variable parameter. 

maybe if i know the value of the laguage param it can be something like that

Quote

$searchresults = $pages->find("has_parent!=2, langparam={$currentlanguage}");

 

Link to comment
Share on other sites

Worth a try:

if($config->ajax) {

    $searchresults = $pages->find("has_parent!=2, title.data{$user->language}"); // See below for the forum link about *.data
    header("Content-type: application/json"); // Set header to JSON
    echo $searchresults->toJSON(); // Output the results as JSON via the toJSON function

} else { ...

More on that here 

 

Link to comment
Share on other sites

On 9/3/2019 at 11:22 PM, Mats said:

Try sending the current language with the Ajax request. And set the language on the response. (On mobile). 

@Mats can you give an example for it.

i think that there must be a simple way to filter page array to current language but i coundnt figure it out

 

 

Link to comment
Share on other sites

  • 1 month later...

Hi,

i found solution and i would like to post it here. Maybe it helps someone.

i removed 

echo $searchresults->toJSON();

i used the code below instead of toJSON function

$myPages = $pages->find('template=clinic|doctor|treatment|location|resource');
$data =$myPages->explode(['title', 'url']);
echo wireEncodeJSON($data);

now it shows the current language results

You have to set the

$.typeahead({

cache: false,

 

if you dont set cache false it still show the cached language result even you change the language.

 

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...