Refactored search language handling

This commit is contained in:
squidfunk 2018-01-21 22:56:54 +01:00 committed by Martin Donath
parent f915928507
commit 4b5c45c656
41 changed files with 66 additions and 17 deletions

View File

@ -99,10 +99,11 @@ The directory layout of the Material theme is as follows:
│ ├─ javascripts/ # JavaScript
│ └─ stylesheets/ # Stylesheets
├─ partials/
│ ├─ integrations/ # 3rd-party integrations
│ ├─ language/ # Localized languages
│ ├─ disqus.html # Disqus integration
│ ├─ footer.html # Footer bar
│ ├─ header.html # Header bar
│ ├─ hero.html # Hero teaser
│ ├─ language.html # Localized labels
│ ├─ nav-item.html # Main navigation item
│ ├─ nav.html # Main navigation

View File

@ -342,13 +342,14 @@ theme:
#### Site search
> Default: `en`
> Default: best match for given theme language, automatically set
Site search is implemented using [lunr.js][17], which includes stemmers for the
English language by default, while stemmers for other languages are included
with [lunr-languages][18], both of which are integrated with this theme. Support
for other languages and even multilingual search can be activated in your
project's `mkdocs.yml`:
with [lunr-languages][18], both of which are integrated with this theme.
Material selects the matching (or best-matching) stemmer for the given theme
language. Multilingual search can be activated in your project's `mkdocs.yml`
by explicitly defining the search language(s):
``` yaml
extra:
@ -356,9 +357,6 @@ extra:
language: 'en, de, ru'
```
All defined languages are used only for stemming. This will automatically load
the stemmers for the specified languages and set them up with site search.
At the time of writing, the following languages are supported: English `en`,
French `fr`, German `de`, Spanish `es`, Italian `it`, Dutch `du`, Danish `da`,
Portguese `pt`, Finnish `fi`, Romanian `ro`, Hungarian `hu`, Russian `ru`,
@ -367,8 +365,8 @@ Norwegian `no`, Swedish `sv`, Japanese `jp` and Turkish `tr`.
!!! info "Search language support for Chinese"
[lunr-languages][18] currently doesn't include a stemmer for Chinese or
other Asian languages, but some users reported the Japanese stemmer to
work quite well for those cases.
other Asian languages, but uses the Japanese stemmer, as some users
reported pretty decent results.
!!! warning "Only specify the languages you really need"

View File

@ -167,7 +167,7 @@
{% endblock %}
</div>
{% block scripts %}
<script src="{{ base_url }}/assets/javascripts/application.fa71e325.js"></script>
<script src="{{ base_url }}/assets/javascripts/application.5f9a6f1b.js"></script>
{% if lang.t("search.language") != "en" %}
{% set languages = lang.t("search.language").split(",") %}
{% if languages | length and languages[0] != "" %}
@ -178,7 +178,9 @@
{% if language == "jp" %}
<script src="{{ path }}/tinyseg.js"></script>
{% endif %}
<script src="{{ path }}/lunr.{{ language }}.js"></script>
{% if language in ("da", "de", "du", "es", "fi", "fr", "hu", "it", "jp", "no", "pt", "ro", "ru", "sv", "tr") %}
<script src="{{ path }}/lunr.{{ language }}.js"></script>
{% endif %}
{% endif %}
{% endfor %}
{% if languages | length > 1 %}

View File

@ -3,7 +3,7 @@
{% macro t(key) %}{{ {
"search.language": (
config.extra.search | default({})
).language | default(config.theme.language, true),
).language,
"search.tokenizer": (
config.extra.search | default({})
).tokenizer | default("", true),

View File

@ -7,6 +7,7 @@
"footer.next": "Næste",
"meta.comments": "Kommentarer",
"meta.source": "Kilde",
"search.language": "da",
"search.placeholder": "Søg",
"search.result.placeholder": "Indtask søgeord",
"search.result.none": "Ingen resultater fundet",

View File

@ -7,6 +7,7 @@
"footer.next": "Nächste Seite",
"meta.comments": "Kommentare",
"meta.source": "Quellcode",
"search.language": "de",
"search.placeholder": "Suche",
"search.result.placeholder": "Suchbegriff eingeben",
"search.result.none": "Keine Suchergebnisse",

View File

@ -7,6 +7,7 @@
"footer.next": "Next",
"meta.comments": "Comments",
"meta.source": "Source",
"search.language": "en",
"search.placeholder": "Search",
"search.result.placeholder": "Type to start searching",
"search.result.none": "No matching documents",

View File

@ -7,6 +7,7 @@
"footer.next": "Siguiente",
"meta.comments": "Comentarios",
"meta.source": "Fuente",
"search.language": "es",
"search.placeholder": "Búsqueda",
"search.result.placeholder": "Teclee para comenzar búsqueda",
"search.result.none": "No se encontraron documentos",

View File

@ -7,6 +7,7 @@
"footer.next": "Suivant",
"meta.comments": "Commentaires",
"meta.source": "Source",
"search.language": "fr",
"search.placeholder": "Rechercher",
"search.result.placeholder": "Taper pour démarrer la recherche",
"search.result.none": "Aucun document trouvé",

View File

@ -7,6 +7,7 @@
"footer.next": "Prossimo",
"meta.comments": "Commenti",
"meta.source": "Sorgente",
"search.language": "it",
"search.placeholder": "Cerca",
"search.result.placeholder": "Scrivi per iniziare a cercare",
"search.result.none": "Nessun documento trovato",

View File

@ -7,6 +7,7 @@
"footer.next": "次",
"meta.comments": "コメント",
"meta.source": "ソース",
"search.language": "jp",
"search.placeholder": "検索",
"search.result.placeholder": "検索キーワードを入力してください",
"search.result.none": "何も見つかりませんでした",

View File

@ -7,6 +7,7 @@
"footer.next": "다음",
"meta.comments": "댓글",
"meta.source": "출처",
"search.language": "jp",
"search.placeholder": "검색",
"search.result.placeholder": "검색어를 입력하세요",
"search.result.none": "검색어와 일치하는 문서가 없습니다",

View File

@ -7,6 +7,7 @@
"footer.next": "Volgende",
"meta.comments": "Reacties",
"meta.source": "Bron",
"search.language": "du",
"search.placeholder": "Zoeken",
"search.result.placeholder": "Typ om te beginnen met zoeken",
"search.result.none": "Geen overeenkomende documenten",

View File

@ -7,6 +7,7 @@
"footer.next": "Neste",
"meta.comments": "Kommentarer",
"meta.source": "Kilde",
"search.language": "no",
"search.placeholder": "Søk",
"search.result.placeholder": "Skriv søkeord",
"search.result.none": "Ingen treff",

View File

@ -7,6 +7,7 @@
"footer.next": "Próximo",
"meta.comments": "Comentários",
"meta.source": "Fonte",
"search.language": "pt",
"search.placeholder": "Buscar",
"search.result.placeholder": "Digite para iniciar a busca",
"search.result.none": "Nenhum resultado encontrado",

View File

@ -7,6 +7,7 @@
"footer.next": "Вперед",
"meta.comments": "Комментарии",
"meta.source": "Исходный код",
"search.language": "ru",
"search.placeholder": "Поиск",
"search.result.placeholder": "Начните печатать для поиска",
"search.result.none": "Совпадений не найдено",

View File

@ -7,6 +7,7 @@
"footer.next": "Nästa",
"meta.comments": "Kommentarer",
"meta.source": "Källa",
"search.language": "sv",
"search.placeholder": "Sök",
"search.result.placeholder": "Skriv sökord",
"search.result.none": "Inga sökresultat",

View File

@ -7,6 +7,7 @@
"footer.next": "Sonraki",
"meta.comments": "Yorumlar",
"meta.source": "Kaynak",
"search.language": "tr",
"search.placeholder": "Ara",
"search.result.placeholder": "Aramaya başlamak için yazın",
"search.result.none": "Eşleşen doküman bulunamadı",

View File

@ -7,6 +7,7 @@
"footer.next": "下一頁",
"meta.comments": "評論",
"meta.source": "來源",
"search.language": "jp",
"search.placeholder": "搜尋",
"search.result.placeholder": "鍵入以開始檢索",
"search.result.none": "沒有找到符合條件的結果",

View File

@ -7,6 +7,7 @@
"footer.next": "前进",
"meta.comments": "评论",
"meta.source": "来源",
"search.language": "jp",
"search.placeholder": "搜索",
"search.result.placeholder": "键入以开始搜索",
"search.result.none": "没有找到符合条件的结果",

View File

@ -179,7 +179,7 @@ export default class Result {
)
/* Set up alternate search languages */
if (lang.length === 1 && lang[0] !== "en") {
if (lang.length === 1 && lang[0] !== "en" && lunr[lang[0]]) {
this.use(lunr[lang[0]])
} else if (lang.length > 1) {
this.use(lunr.multiLanguage(...lang))

View File

@ -318,7 +318,9 @@
{% if language == "jp" %}
<script src="{{ path }}/tinyseg.js"></script>
{% endif %}
<script src="{{ path }}/lunr.{{ language }}.js"></script>
{% if language in ($md-lunr-languages$) %}
<script src="{{ path }}/lunr.{{ language }}.js"></script>
{% endif %}
{% endif %}
{% endfor %}
{% if languages | length > 1 %}

View File

@ -30,7 +30,7 @@
{% macro t(key) %}{{ {
"search.language": (
config.extra.search | default({})
).language | default(config.theme.language, true),
).language,
"search.tokenizer": (
config.extra.search | default({})
).tokenizer | default("", true),

View File

@ -30,6 +30,7 @@
"footer.next": "Næste",
"meta.comments": "Kommentarer",
"meta.source": "Kilde",
"search.language": "da",
"search.placeholder": "Søg",
"search.result.placeholder": "Indtask søgeord",
"search.result.none": "Ingen resultater fundet",

View File

@ -30,6 +30,7 @@
"footer.next": "Nächste Seite",
"meta.comments": "Kommentare",
"meta.source": "Quellcode",
"search.language": "de",
"search.placeholder": "Suche",
"search.result.placeholder": "Suchbegriff eingeben",
"search.result.none": "Keine Suchergebnisse",

View File

@ -30,6 +30,7 @@
"footer.next": "Next",
"meta.comments": "Comments",
"meta.source": "Source",
"search.language": "en",
"search.placeholder": "Search",
"search.result.placeholder": "Type to start searching",
"search.result.none": "No matching documents",

View File

@ -30,6 +30,7 @@
"footer.next": "Siguiente",
"meta.comments": "Comentarios",
"meta.source": "Fuente",
"search.language": "es",
"search.placeholder": "Búsqueda",
"search.result.placeholder": "Teclee para comenzar búsqueda",
"search.result.none": "No se encontraron documentos",

View File

@ -30,6 +30,7 @@
"footer.next": "Suivant",
"meta.comments": "Commentaires",
"meta.source": "Source",
"search.language": "fr",
"search.placeholder": "Rechercher",
"search.result.placeholder": "Taper pour démarrer la recherche",
"search.result.none": "Aucun document trouvé",

View File

@ -30,6 +30,7 @@
"footer.next": "Prossimo",
"meta.comments": "Commenti",
"meta.source": "Sorgente",
"search.language": "it",
"search.placeholder": "Cerca",
"search.result.placeholder": "Scrivi per iniziare a cercare",
"search.result.none": "Nessun documento trovato",

View File

@ -30,6 +30,7 @@
"footer.next": "次",
"meta.comments": "コメント",
"meta.source": "ソース",
"search.language": "jp",
"search.placeholder": "検索",
"search.result.placeholder": "検索キーワードを入力してください",
"search.result.none": "何も見つかりませんでした",

View File

@ -30,6 +30,7 @@
"footer.next": "다음",
"meta.comments": "댓글",
"meta.source": "출처",
"search.language": "jp",
"search.placeholder": "검색",
"search.result.placeholder": "검색어를 입력하세요",
"search.result.none": "검색어와 일치하는 문서가 없습니다",

View File

@ -30,6 +30,7 @@
"footer.next": "Volgende",
"meta.comments": "Reacties",
"meta.source": "Bron",
"search.language": "du",
"search.placeholder": "Zoeken",
"search.result.placeholder": "Typ om te beginnen met zoeken",
"search.result.none": "Geen overeenkomende documenten",

View File

@ -30,6 +30,7 @@
"footer.next": "Neste",
"meta.comments": "Kommentarer",
"meta.source": "Kilde",
"search.language": "no",
"search.placeholder": "Søk",
"search.result.placeholder": "Skriv søkeord",
"search.result.none": "Ingen treff",

View File

@ -30,6 +30,7 @@
"footer.next": "Próximo",
"meta.comments": "Comentários",
"meta.source": "Fonte",
"search.language": "pt",
"search.placeholder": "Buscar",
"search.result.placeholder": "Digite para iniciar a busca",
"search.result.none": "Nenhum resultado encontrado",

View File

@ -30,6 +30,7 @@
"footer.next": "Вперед",
"meta.comments": "Комментарии",
"meta.source": "Исходный код",
"search.language": "ru",
"search.placeholder": "Поиск",
"search.result.placeholder": "Начните печатать для поиска",
"search.result.none": "Совпадений не найдено",

View File

@ -30,6 +30,7 @@
"footer.next": "Nästa",
"meta.comments": "Kommentarer",
"meta.source": "Källa",
"search.language": "sv",
"search.placeholder": "Sök",
"search.result.placeholder": "Skriv sökord",
"search.result.none": "Inga sökresultat",

View File

@ -30,6 +30,7 @@
"footer.next": "Sonraki",
"meta.comments": "Yorumlar",
"meta.source": "Kaynak",
"search.language": "tr",
"search.placeholder": "Ara",
"search.result.placeholder": "Aramaya başlamak için yazın",
"search.result.none": "Eşleşen doküman bulunamadı",

View File

@ -30,6 +30,7 @@
"footer.next": "下一頁",
"meta.comments": "評論",
"meta.source": "來源",
"search.language": "jp",
"search.placeholder": "搜尋",
"search.result.placeholder": "鍵入以開始檢索",
"search.result.none": "沒有找到符合條件的結果",

View File

@ -30,6 +30,7 @@
"footer.next": "前进",
"meta.comments": "评论",
"meta.source": "来源",
"search.language": "jp",
"search.placeholder": "搜索",
"search.result.placeholder": "键入以开始搜索",
"search.result.none": "没有找到符合条件的结果",

View File

@ -156,6 +156,20 @@ module.exports = env => {
/* Write theme version into template */
.replace("$md-name$", metadata.name)
.replace("$md-version$", metadata.version)
/* Write available search languages into template */
.replace("$md-lunr-languages$",
fs.readdirSync(
path.resolve(__dirname, "node_modules/lunr-languages")
).reduce((files, file) => {
const matches = file.match(/lunr.(\w{2}).js$/)
if (matches) {
const [, language] = matches
files.push(`"${language}"`)
}
return files
}, [])
.join(", "))
}
}
]),