...
By default, if you add your apex domain (http://acme.com , for example) to the domains area of your console, our crawler will crawl all the content it can find on your website, including content in different languages, whether that content is stored in a sub directory such as a www.acme.com/fnfr/ or on a subdomain such as fnfr.acme.com.
The directory and domain information are stored in the schema fields dir1
, dir2
and domain
. For example the dir1
for www.acme.com/enfr/ would be ‘fn‘fr' (for French) and the domain
for enfr.acme.com would be ‘fr.acme.com’. We can then use this information to create search filters, settings and UIs based on different languages.
...
In our example the user is in France, so if we echo the result we get ‘lang=”fr-FR”’.
Code Block | ||
---|---|---|
| ||
<?php echo $currentLang; // Output: lang="fr-FR" ?> |
In our example, the dir1
schema field holds the language information, which for French is 'fr'
. Therefore,
we just need to get the ‘fr'
portion of the language attribute string and store it to a variable (alternatively you could use a switch statement to set the variable if you don’t have many languages!). To do this we will use some regex:
Code Block | ||
---|---|---|
| ||
<?php $currentLang = get_language_attributes(); // Get language attribute $regex = '/"(.*?)-/s'; // Regex pattern to extract 'fr' preg_match($regex, $currentLang, $output); // Store 'fr' in $output echo $output[1]; ?> |
...
Without the variable in place the default filter looks like this:
Code Block | ||
---|---|---|
| ||
"variables": { "filter": "dir1='fr'" }, |
Adding the variable in we get the following:
Code Block | ||
---|---|---|
| ||
"variables": { "filter": "dir1=\'' . $output[1] . '\'" }, |
...
To do this we simply echo the html onto the page:
Code Block | ||
---|---|---|
| ||
<?php echo '<div data-widget="overlay"> <script type="application/json"> { "account": "your-account", "collection": "your-collection", "endpoint": "//jsonapi-us-valkyrie.sajari.net", "pipeline": "website", "preset": "website", "variables": { "filter": "dir1=\'' . $output[1] . '\'" }, "filters": [], "options": { }, "results": { "viewType": "list", "showVariantImage": false, "imageAspectRatio": { "grid": 0.563, "list": 1 }, "imageObjectFit": { "grid": "cover", "list": "cover" } }, "showViewType": true, "input": { "hide": false, "position": "aside" }, "mode": "overlay", "buttonSelector": ".btn-search" }, "theme": { "color": { "primary": { "base": "#A335FF", "text": "#ffffff", "active": "#A335FF" } } }, "customClassNames": { "results": { "template": { "container": "list" } } } } </script> <noscript>This page requires JavaScript</noscript> </div> <script async src="https://cdn.sajari.com/embed/1/loader.js"></script>'; ?> |
...
If we look at our source code we can see that the correct filter is in place:
Code Block | ||
---|---|---|
| ||
<div data-widget="overlay"> <script type="application/json"> { "account": "your-account", "collection": "your-collection", "endpoint": "//jsonapi-us-valkyrie.sajari.net", "pipeline": "website", "preset": "website", "variables": { "filter": "dir1='fr'" }, |
...