@tekkare/romulus 0.1.374 → 0.1.375
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.
package/dist/module.json
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Langue transmise par une autre app OIP (`?lang=fr`).
|
|
3
|
+
*
|
|
4
|
+
* Le portail oip7 et les apps n'ont ni le meme domaine ni la meme strategie
|
|
5
|
+
* i18n : sans ce relais, l'utilisateur qui lit oip7 en francais arrive dans
|
|
6
|
+
* l'app dans sa langue par defaut. Le lien sortant porte `lang` ; a l'arrivee
|
|
7
|
+
* on applique la langue puis on retire le parametre de l'URL.
|
|
8
|
+
*
|
|
9
|
+
* - Strategies a prefixe (`prefix_and_default`, `prefix`...) : redirection vers
|
|
10
|
+
* la meme route dans la langue demandee (`/hco/1?lang=fr` -> `/fr/hco/1`).
|
|
11
|
+
* - `no_prefix` : la langue n'est pas dans l'URL, on l'applique avec setLocale.
|
|
12
|
+
*
|
|
13
|
+
* Une langue inconnue de l'app est ignoree (le parametre est seulement retire).
|
|
14
|
+
*
|
|
15
|
+
* Les apps qui n'etendent pas le layer branchent la meme fonction dans un
|
|
16
|
+
* middleware global :
|
|
17
|
+
* import { localeFromQuery } from "@tekkare/romulus/layer-utils/localeFromQuery";
|
|
18
|
+
* export default defineNuxtRouteMiddleware(localeFromQuery);
|
|
19
|
+
*/
|
|
20
|
+
import type { RouteLocationNormalized } from "vue-router";
|
|
21
|
+
import { navigateTo, useLocalePath, useNuxtApp, useRouteBaseName } from "#imports";
|
|
22
|
+
|
|
23
|
+
export const LOCALE_QUERY_KEY = "lang";
|
|
24
|
+
|
|
25
|
+
export async function localeFromQuery(to: RouteLocationNormalized) {
|
|
26
|
+
const lang = to.query[LOCALE_QUERY_KEY];
|
|
27
|
+
if (lang === undefined) return;
|
|
28
|
+
|
|
29
|
+
const query = { ...to.query };
|
|
30
|
+
delete query[LOCALE_QUERY_KEY];
|
|
31
|
+
const stripped = { path: to.path, query, hash: to.hash };
|
|
32
|
+
|
|
33
|
+
const i18n = useNuxtApp().$i18n as any;
|
|
34
|
+
const codes: string[] = i18n?.localeCodes?.value ?? [];
|
|
35
|
+
if (typeof lang !== "string" || !codes.includes(lang)) {
|
|
36
|
+
return navigateTo(stripped, { replace: true });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const baseName = useRouteBaseName()(to);
|
|
40
|
+
const target = baseName
|
|
41
|
+
? useLocalePath()({ name: baseName, params: to.params, query, hash: to.hash }, lang)
|
|
42
|
+
: "";
|
|
43
|
+
|
|
44
|
+
// Strategie a prefixe : la langue vit dans l'URL, la redirection suffit.
|
|
45
|
+
if (target && target.split(/[?#]/)[0] !== to.path) {
|
|
46
|
+
return navigateTo(target, { replace: true });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// no_prefix (ou deja dans la bonne langue) : appliquer puis nettoyer l'URL.
|
|
50
|
+
if (i18n.locale?.value !== lang) await i18n.setLocale(lang);
|
|
51
|
+
return navigateTo(stripped, { replace: true });
|
|
52
|
+
}
|