@datagouv/components-next 1.0.2-dev.189 → 1.0.2-dev.190

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { g as Ke } from "./main-DseJBKsd.js";
1
+ import { g as Ke } from "./main-HzoTSLAM.js";
2
2
  import We from "vue";
3
3
  function Fe($, K) {
4
4
  for (var V = 0; V < K.length; V++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datagouv/components-next",
3
- "version": "1.0.2-dev.189",
3
+ "version": "1.0.2-dev.190",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "engines": {
@@ -14,24 +14,39 @@ const translationModules = import.meta.glob<Record<string, string>>('../../../lo
14
14
  import: 'default',
15
15
  })
16
16
 
17
+ const LANGUAGE_SUBTAG_REGEX = /^[a-z]{2,3}$/
18
+
19
+ /**
20
+ * Extract the language subtag of an `accept-language` header or of
21
+ * `navigator.language`, and return it only if it is a well-formed one.
22
+ *
23
+ * Both are user-controlled, and the locale is handed over to `Intl` formatters,
24
+ * which throw a `RangeError` on anything that is not a valid language tag. The
25
+ * wildcard `*` is rejected here too: it means "any language", so it must fall
26
+ * back to the next detection step.
27
+ */
28
+ function parseLanguage(value: string | undefined | null): string | null {
29
+ if (!value) {
30
+ return null
31
+ }
32
+ const language = value.split(';')[0]!.split(',')[0]!.split('-')[0]!.toLowerCase()
33
+ return LANGUAGE_SUBTAG_REGEX.test(language) ? language : null
34
+ }
35
+
17
36
  function detectLanguage(): string {
18
37
  // Server-side (Nuxt)
19
38
  try {
20
- const header = useRequestHeader?.('accept-language')
21
- const acceptLanguage = header
22
- if (acceptLanguage) {
23
- const primaryLang = acceptLanguage.split(';')[0]!.split(',')[0]!.split('-')[0]!.toLowerCase()
24
- // Ignore wildcard * language, that should fallback to client side detection or default language
25
- if (primaryLang !== '*') return primaryLang
26
- }
39
+ const language = parseLanguage(useRequestHeader?.('accept-language'))
40
+ if (language) return language
27
41
  }
28
42
  catch {
29
43
  // useRequestHeaders not available, continue to client-side detection
30
44
  }
31
45
 
32
46
  // Client-side
33
- if (typeof window !== 'undefined' && navigator.language) {
34
- return navigator.language.split('-')[0]!.toLowerCase()
47
+ if (typeof window !== 'undefined') {
48
+ const language = parseLanguage(navigator.language)
49
+ if (language) return language
35
50
  }
36
51
 
37
52
  return 'fr'