Spaces:
Running

File size: 4,617 Bytes
e0d0231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ab635d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0d0231
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * The language choices the client offers, in the order the select lists them.
 * `browser` is MiniSearch's own value and is never sent: the client resolves it
 * to `navigator.language` at search time. `auto` asks SearXNG to detect the
 * language from the query, as every search did before this setting existed.
 * `all` is SearXNG's own "no preference", and every other value is a locale
 * code, region included, since
 * SearXNG has no separate country parameter — `pt-BR` is Portuguese served by
 * Brazil's engine editions.
 *
 * Labels stay in their own language on purpose: a user who cannot read English
 * has to find their own language in this list.
 */
export const searchLanguageOptions = [
  { value: "browser", label: "Browser language" },
  { value: "auto", label: "Detect from the query" },
  { value: "all", label: "Any language" },
  { value: "en-US", label: "English (United States)" },
  { value: "en-GB", label: "English (United Kingdom)" },
  { value: "de", label: "Deutsch" },
  { value: "es", label: "Español" },
  { value: "fr", label: "Français" },
  { value: "it", label: "Italiano" },
  { value: "pt-BR", label: "Português (Brasil)" },
  { value: "pt-PT", label: "Português (Portugal)" },
  { value: "nl", label: "Nederlands" },
  { value: "pl", label: "Polski" },
  { value: "sv", label: "Svenska" },
  { value: "tr", label: "Türkçe" },
  { value: "ru", label: "Русский" },
  { value: "ar", label: "العربية" },
  { value: "hi", label: "हिन्दी" },
  { value: "ja", label: "日本語" },
  { value: "ko", label: "한국어" },
  { value: "zh-CN", label: "中文 (中国)" },
  { value: "zh-TW", label: "中文 (台灣)" },
] as const;

export type SearchLanguage = (typeof searchLanguageOptions)[number]["value"];

/**
 * SearXNG's own pattern for a locale, `VALID_LANGUAGE_CODE` in
 * `searx/webutils.py`: a language code with an optional region. Anything else
 * makes SearXNG raise a parameter error, so the server refuses it too rather
 * than pass it on.
 */
const searxngLanguageCode = /^[a-z]{2,3}(-[a-zA-Z]{2})?$/;

/** Whether SearXNG takes this value for its `language` parameter. */
export function isSearxngLanguage(value: string): boolean {
  // `all` matches the pattern above on its own; `auto` is the one other value
  // SearXNG accepts.
  return value === "auto" || searxngLanguageCode.test(value);
}

/**
 * The `language` to send for a saved setting, or nothing when the search has no
 * preference to state. "Browser language" is resolved here, at search time, so
 * the request carries the browser's own locale instead of the guess SearXNG
 * would have to make from the query text.
 *
 * A browser locale SearXNG cannot take, such as Chrome's `es-419` (Latin
 * American Spanish) or `zh-Hant-TW`, falls back to its language subtag (`es`,
 * `zh`), and is dropped only when even that does not fit: the server answers
 * such a value 400, and a search that cannot state its preference is better
 * served unfiltered than refused outright.
 */
export function resolveSearchLanguage(
  setting: SearchLanguage | undefined,
  browserLanguage: string,
): string | undefined {
  if (setting === "browser") {
    if (isSearxngLanguage(browserLanguage)) return browserLanguage;
    const primary = browserLanguage.split("-")[0] ?? "";
    return isSearxngLanguage(primary) ? primary : undefined;
  }
  return setting;
}

/**
 * The choices that ask for no language in particular: the default, which sends
 * whatever locale the browser happens to be in, and the two that leave the
 * choice to SearXNG. Every other choice names a language the user picked.
 */
const unspecifiedSearchLanguages: readonly SearchLanguage[] = [
  "browser",
  "auto",
  "all",
];

/**
 * Whether this setting asks for one specific language, the way only a choice
 * the user made by hand does. "Browser language" is the default and asks for
 * the browser's own locale, "Detect from the query" and "Any language" ask for
 * none, so none of the three states a language to hold the results to.
 */
export function isSpecificSearchLanguage(
  setting: SearchLanguage | undefined,
): boolean {
  return setting !== undefined && !unspecifiedSearchLanguages.includes(setting);
}

/**
 * Set to `false` on a search response whose requested language could not be
 * applied, which happens when a fallback provider answers a text search: those
 * take only a query and a count. The header is absent when the language was
 * applied, so a search that asked for none carries nothing new.
 */
export const SEARCH_LANGUAGE_APPLIED_HEADER = "X-Search-Language-Applied";