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 │ ├─ javascripts/ # JavaScript
│ └─ stylesheets/ # Stylesheets │ └─ stylesheets/ # Stylesheets
├─ partials/ ├─ partials/
│ ├─ integrations/ # 3rd-party integrations
│ ├─ language/ # Localized languages │ ├─ language/ # Localized languages
│ ├─ disqus.html # Disqus integration
│ ├─ footer.html # Footer bar │ ├─ footer.html # Footer bar
│ ├─ header.html # Header bar │ ├─ header.html # Header bar
│ ├─ hero.html # Hero teaser
│ ├─ language.html # Localized labels │ ├─ language.html # Localized labels
│ ├─ nav-item.html # Main navigation item │ ├─ nav-item.html # Main navigation item
│ ├─ nav.html # Main navigation │ ├─ nav.html # Main navigation

View File

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

View File

@ -167,7 +167,7 @@
{% endblock %} {% endblock %}
</div> </div>
{% block scripts %} {% 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" %} {% if lang.t("search.language") != "en" %}
{% set languages = lang.t("search.language").split(",") %} {% set languages = lang.t("search.language").split(",") %}
{% if languages | length and languages[0] != "" %} {% if languages | length and languages[0] != "" %}
@ -178,7 +178,9 @@
{% if language == "jp" %} {% if language == "jp" %}
<script src="{{ path }}/tinyseg.js"></script> <script src="{{ path }}/tinyseg.js"></script>
{% endif %} {% 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 %} {% endif %}
{% endfor %} {% endfor %}
{% if languages | length > 1 %} {% if languages | length > 1 %}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -179,7 +179,7 @@ export default class Result {
) )
/* Set up alternate search languages */ /* 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]]) this.use(lunr[lang[0]])
} else if (lang.length > 1) { } else if (lang.length > 1) {
this.use(lunr.multiLanguage(...lang)) this.use(lunr.multiLanguage(...lang))

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -156,6 +156,20 @@ module.exports = env => {
/* Write theme version into template */ /* Write theme version into template */
.replace("$md-name$", metadata.name) .replace("$md-name$", metadata.name)
.replace("$md-version$", metadata.version) .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(", "))
} }
} }
]), ]),