|
55 | 55 | return best; |
56 | 56 | }; |
57 | 57 |
|
58 | | -const detectLang = query => { |
59 | | - const all = detectAll(query, { only: ['en', 'ru'] }); |
60 | | - console.log('detected languages', all) |
61 | | - const good = all.filter(i => i.accuracy > 0.01); |
62 | | - let lang = 'en'; |
63 | | - if (good.length > 0) { |
64 | | - good.sort((a, b) => b.accuracy - a.accuracy); |
65 | | - lang = good[0].lang; |
| 58 | +const detectLanguage = query => { |
| 59 | + const en = 'en'; |
| 60 | + const allowedLanguages = params.get('allowed_languages'); |
| 61 | + const only = allowedLanguages ? allowedLanguages.split(',') : [en]; |
| 62 | + console.log('allowed languages', only); |
| 63 | + if (only.length <= 1) { |
| 64 | + return en; |
66 | 65 | } |
67 | | - return lang; |
| 66 | + |
| 67 | + const all = detectAll(query, { only }); |
| 68 | + console.log('detected languages', all); |
| 69 | + |
| 70 | + return all |
| 71 | + .filter(i => i.accuracy > 0.01) |
| 72 | + .sort((a, b) => b.accuracy - a.accuracy)[0]? |
| 73 | + .lang || en; |
68 | 74 | }; |
69 | 75 |
|
70 | 76 | const redirect = (url, query) => { |
71 | 77 | params |
72 | 78 | .entries() |
73 | | - .filter(([k, _]) => k !== 'fast') |
| 79 | + .filter(([k, _]) => k !== 'fast' && k !== 'allowed_languages') |
74 | 80 | .forEach(([k, v]) => url.searchParams.set(k, v)); |
75 | 81 | url.searchParams.set('q', query) |
76 | | - url.searchParams.set('language', detectLang(query)); |
| 82 | + url.searchParams.set('language', detectLanguage(query)); |
77 | 83 | console.log(url); |
78 | 84 | window.location.replace(url.toString()); |
79 | 85 | }; |
|
87 | 93 | if (query) { |
88 | 94 | redirect(targetUrl, query); |
89 | 95 | } else { |
90 | | - document.body.innerHTML = '<p>This page accepts SearXNG <a target="_blank" href="https://docs.searxng.org/dev/search_api.html">GET parameters</a> + special optional parameter <code>fast=1</code> for hardcoded instances.</p><p>Example for browser settings (<code>chrome://settings/search</code> or <code>about:preferences#search</code>):</p><p><code>{{ site.url }}{{ page.url }}?q=%s&fast=1&image_proxy=True&categories=images</code></p>'; |
| 96 | + document.body.innerHTML = '<p>This page accepts SearXNG <a target="_blank" href="https://docs.searxng.org/dev/search_api.html">GET parameters</a> + special optional parameter <code>fast=1</code> for hardcoded instances and <code>allowed_languages=en,ru</code> for automatic language limit derived from the query.</p><p>Example for browser settings (<code>chrome://settings/search</code> or <code>about:preferences#search</code>):</p><p><code>{{ site.url }}{{ page.url }}?q=%s&fast=1&image_proxy=True&categories=images</code></p>'; |
91 | 97 |
|
92 | 98 | const input = document.createElement('input'); |
93 | 99 | input.name = 'q'; |
|
0 commit comments