Jump to content
kkalgidim

How can i get the current language for ajax search

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

Share this post


Link to post
Share on other sites
On 8/31/2019 at 3:02 PM, kkalgidim said:

$searchresults->toJSON();

What does this method do? Can you share the code or explain where it comes from?

Share this post


Link to post
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.

 

Share this post


Link to post
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 { ...

 

Share this post


Link to post
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}");

 

Share this post


Link to post
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 

 

Share this post


Link to post
Share on other sites

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

  • Like 1

Share this post


Link to post
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

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...