Skip to content

Commit de89c1e

Browse files
authored
fix(locales): use correct lang (#283)
1 parent 77f144a commit de89c1e

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

docs/config/algolia-search.md

+72-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,75 @@ module.exports = {
1313
}
1414
```
1515

16-
For more options, check out [Algolia DocSearch's documentation](https://github.com/algolia/docsearch#docsearch-options).
16+
For more options, check out [Algolia DocSearch's documentation](https://docsearch.algolia.com/docs/behavior). You can pass any extra option alongside other options, e.g. passing `searchParameters`:
17+
18+
```js
19+
module.exports = {
20+
themeConfig: {
21+
algolia: {
22+
apiKey: 'your_api_key',
23+
indexName: 'index_name',
24+
searchParameters: {
25+
facetFilters: ['tags:guide,api']
26+
}
27+
}
28+
}
29+
}
30+
```
31+
32+
## Internationalization (i18n)
33+
34+
If you have multiple locales in your documentation and you have defined a `locales` object in your `themeConfig`:
35+
36+
```js
37+
module.exports = {
38+
themeConfig: {
39+
locales: {
40+
// ...
41+
},
42+
algolia: {
43+
apiKey: 'your_api_key',
44+
indexName: 'index_name'
45+
}
46+
}
47+
}
48+
```
49+
50+
VitePress will automatically add a `language` _facetFilter_ to the `searchParams.facetFilter` array with the correct language value. **Make sure to properly configure your DocSearch config as well** by adding `language` as a _custom attribute for faceting_ and by setting it based on the `lang` attribute of the `<html>` element. Here is a short example of DocSearch config:
51+
52+
```json
53+
{
54+
"index_name": "<the name of your library>",
55+
"start_urls": [
56+
{
57+
"url": "<your deployed url>"
58+
}
59+
],
60+
"stop_urls": ["(?:(?<!\\.html)(?<!/))$"],
61+
"selectors": {
62+
"lvl0": {
63+
"selector": ".sidebar > .sidebar-links > .sidebar-link .sidebar-link-item.active",
64+
"global": true,
65+
"default_value": "Documentation"
66+
},
67+
"lvl1": ".content h1",
68+
"lvl2": ".content h2",
69+
"lvl3": ".content h3",
70+
"lvl4": ".content h4",
71+
"lvl5": ".content h5",
72+
"lvl6": ".content p, .content li",
73+
"text": ".content [class^=language-]",
74+
"language": {
75+
"selector": "/html/@lang",
76+
"type": "xpath",
77+
"global": true,
78+
"default_value": "en-US"
79+
}
80+
},
81+
"custom_settings": {
82+
"attributesForFaceting": ["language"]
83+
}
84+
}
85+
```
86+
87+
You can take a look at the [DocSearch config used by Vue Router](https://github.com/algolia/docsearch-configs/blob/master/configs/next_router_vuejs.json) for a complete example.

src/client/theme-default/Layout.vue

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
<NavBar v-if="showNavbar" @toggle="toggleSidebar">
44
<template #search>
55
<slot name="navbar-search">
6-
<AlgoliaSearchBox v-if="theme.algolia" :options="theme.algolia" />
6+
<AlgoliaSearchBox
7+
v-if="theme.algolia"
8+
:options="theme.algolia"
9+
:multilang="theme.locales"
10+
:key="siteRouteData.lang"
11+
/>
712
</slot>
813
</template>
914
</NavBar>

src/client/theme-default/components/AlgoliaSearchBox.vue

+17-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ import { defineProps, getCurrentInstance, onMounted, watch } from 'vue'
99
import docsearch from '@docsearch/js'
1010
import type { DefaultTheme } from '../config'
1111
import type { DocSearchHit } from '@docsearch/react/dist/esm/types'
12+
import { useSiteDataByRoute } from 'vitepress'
13+
14+
const siteData = useSiteDataByRoute()
1215
1316
const props = defineProps<{
1417
options: DefaultTheme.AlgoliaSearchOptions
18+
multilang?: boolean
1519
}>()
1620
1721
const vm = getCurrentInstance()
@@ -54,11 +58,23 @@ function update(options: any) {
5458
}
5559
5660
function initialize(userOptions: any) {
61+
// if the user has multiple locales, the search results should be filtered
62+
// based on the language
63+
const facetFilters = props.multilang
64+
? ['language:' + siteData.value.lang]
65+
: []
66+
5767
docsearch(
5868
Object.assign({}, userOptions, {
5969
container: '#docsearch',
6070
61-
searchParameters: Object.assign({}, userOptions.searchParameters),
71+
searchParameters: Object.assign({}, userOptions.searchParameters, {
72+
// pass a custom lang facetFilter to allow multiple language search
73+
// https://github.com/algolia/docsearch-configs/pull/3942
74+
facetFilters: facetFilters.concat(
75+
userOptions.searchParameters?.facetFilters || []
76+
)
77+
}),
6278
6379
navigator: {
6480
navigate: ({ suggestionUrl }: { suggestionUrl: string }) => {

src/shared/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function resolveSiteDataByRoute(siteData: SiteData, route: string) {
4747
// clean the locales to reduce the bundle size
4848
locales: {}
4949
},
50+
lang: localeThemeConfig.lang || siteData.lang,
5051
locales: {}
5152
}
5253
}

0 commit comments

Comments
 (0)