feat(search): i18n support
This commit is contained in:
parent
f5d45458fd
commit
c018f4967a
@ -39,12 +39,14 @@ class Search {
|
|||||||
private input: HTMLInputElement;
|
private input: HTMLInputElement;
|
||||||
private list: HTMLDivElement;
|
private list: HTMLDivElement;
|
||||||
private resultTitle: HTMLHeadElement;
|
private resultTitle: HTMLHeadElement;
|
||||||
|
private resultTitleTemplate: string;
|
||||||
|
|
||||||
constructor({ form, input, list, resultTitle }) {
|
constructor({ form, input, list, resultTitle, resultTitleTemplate }) {
|
||||||
this.form = form;
|
this.form = form;
|
||||||
this.input = input;
|
this.input = input;
|
||||||
this.list = list;
|
this.list = list;
|
||||||
this.resultTitle = resultTitle;
|
this.resultTitle = resultTitle;
|
||||||
|
this.resultTitleTemplate = resultTitleTemplate;
|
||||||
|
|
||||||
this.handleQueryString();
|
this.handleQueryString();
|
||||||
this.bindQueryStringChange();
|
this.bindQueryStringChange();
|
||||||
@ -136,7 +138,11 @@ class Search {
|
|||||||
|
|
||||||
const endTime = performance.now();
|
const endTime = performance.now();
|
||||||
|
|
||||||
this.resultTitle.innerText = `${results.length} pages (${((endTime - startTime) / 1000).toPrecision(1)} seconds)`;
|
this.resultTitle.innerText = this.generateResultTitle(results.length, ((endTime - startTime) / 1000).toPrecision(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
private generateResultTitle(resultLen, time) {
|
||||||
|
return this.resultTitleTemplate.replace("#PAGES_COUNT", resultLen).replace("#TIME_SECONDS", time);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getData() {
|
public async getData() {
|
||||||
@ -231,6 +237,12 @@ class Search {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
searchResultTitleTemplate: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
window.addEventListener('load', () => {
|
window.addEventListener('load', () => {
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
const searchForm = document.querySelector('.search-form') as HTMLFormElement,
|
const searchForm = document.querySelector('.search-form') as HTMLFormElement,
|
||||||
@ -242,7 +254,8 @@ window.addEventListener('load', () => {
|
|||||||
form: searchForm,
|
form: searchForm,
|
||||||
input: searchInput,
|
input: searchInput,
|
||||||
list: searchResultList,
|
list: searchResultList,
|
||||||
resultTitle: searchResultTitle
|
resultTitle: searchResultTitle,
|
||||||
|
resultTitleTemplate: window.searchResultTitleTemplate
|
||||||
});
|
});
|
||||||
}, 0);
|
}, 0);
|
||||||
})
|
})
|
||||||
|
11
i18n/en.toml
11
i18n/en.toml
@ -20,4 +20,13 @@
|
|||||||
other = "Not Found"
|
other = "Not Found"
|
||||||
|
|
||||||
[notFoundSubtitle]
|
[notFoundSubtitle]
|
||||||
other = "This page does not exist."
|
other = "This page does not exist."
|
||||||
|
|
||||||
|
[searchTitle]
|
||||||
|
other = "Search"
|
||||||
|
|
||||||
|
[searchPlaceholder]
|
||||||
|
other = "Type something..."
|
||||||
|
|
||||||
|
[searchResultTitle]
|
||||||
|
other = "#PAGES_COUNT pages (#TIME_SECONDS seconds)"
|
@ -20,4 +20,13 @@
|
|||||||
other = "404 错误"
|
other = "404 错误"
|
||||||
|
|
||||||
[notFoundSubtitle]
|
[notFoundSubtitle]
|
||||||
other = "页面不存在"
|
other = "页面不存在"
|
||||||
|
|
||||||
|
[searchTitle]
|
||||||
|
other = "搜索"
|
||||||
|
|
||||||
|
[searchPlaceholder]
|
||||||
|
other = "输入关键词..."
|
||||||
|
|
||||||
|
[searchResultTitle]
|
||||||
|
other = "#PAGES_COUNT 个结果 (用时 #TIME_SECONDS 秒)"
|
@ -7,8 +7,8 @@
|
|||||||
{{ define "main" }}
|
{{ define "main" }}
|
||||||
<form action="{{ .Permalink }}" class="search-form"{{ with .OutputFormats.Get "json" -}} data-json="{{ .Permalink }}"{{- end }}>
|
<form action="{{ .Permalink }}" class="search-form"{{ with .OutputFormats.Get "json" -}} data-json="{{ .Permalink }}"{{- end }}>
|
||||||
<p>
|
<p>
|
||||||
<label>Keyword</label>
|
<label>{{ T "searchTitle" }}</label>
|
||||||
<input name="keyword" placeholder="Type something..." />
|
<input name="keyword" placeholder="{{ T `searchPlaceholder` }}" />
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<button title="Search">
|
<button title="Search">
|
||||||
@ -19,6 +19,10 @@
|
|||||||
<h3 class="search-result--title"></h3>
|
<h3 class="search-result--title"></h3>
|
||||||
<div class="search-result--list article-list--compact"></div>
|
<div class="search-result--list article-list--compact"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
window.searchResultTitleTemplate = "{{ T `searchResultTitle` }}"
|
||||||
|
</script>
|
||||||
|
|
||||||
{{- $opts := dict "minify" hugo.IsProduction "JSXFactory" "createElement" -}}
|
{{- $opts := dict "minify" hugo.IsProduction "JSXFactory" "createElement" -}}
|
||||||
{{- $searchScript := resources.Get "ts/search.tsx" | js.Build $opts -}}
|
{{- $searchScript := resources.Get "ts/search.tsx" | js.Build $opts -}}
|
||||||
<script type="text/javascript" src="{{ $searchScript.RelPermalink }}" defer></script>
|
<script type="text/javascript" src="{{ $searchScript.RelPermalink }}" defer></script>
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<form action="/search" class="search-form widget" {{ with .OutputFormats.Get "json" -}}data-json="{{ .Permalink }}" {{- end }}>
|
<form action="/search" class="search-form widget" {{ with .OutputFormats.Get "json" -}}data-json="{{ .Permalink }}" {{- end }}>
|
||||||
<p>
|
<p>
|
||||||
<label>Search</label>
|
<label>{{ T "searchTitle" }}</label>
|
||||||
<input name="keyword" required placeholder="Type something..." />
|
<input name="keyword" required placeholder="{{ T `searchPlaceholder` }}" />
|
||||||
|
|
||||||
<button title="Search">
|
<button title="Search">
|
||||||
{{ partial "helper/icon" "search" }}
|
{{ partial "helper/icon" "search" }}
|
||||||
|
Loading…
Reference in New Issue
Block a user