mordoc 1.0.0 → 1.0.1
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/package.json +1 -1
- package/src/app/lang-utils.ts +42 -9
package/package.json
CHANGED
package/src/app/lang-utils.ts
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
|
+
import type { LanguageConfig } from '../types/language.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
4
|
+
* Lang utilities for the React app. Self-contained so that src/app/ has no
|
|
5
|
+
* imports outside its own directory — required for it to work as a published
|
|
6
|
+
* npm artifact compiled by the user's Vite. Node-side code uses the copy in
|
|
7
|
+
* src/utils/lang-utils.ts compiled to dist/.
|
|
5
8
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
|
|
10
|
+
export function detectCurrentLang(
|
|
11
|
+
pathname: string,
|
|
12
|
+
language: LanguageConfig | null,
|
|
13
|
+
defaultLanguage: string,
|
|
14
|
+
): string {
|
|
15
|
+
if (!language || language.languages.length <= 1) return defaultLanguage;
|
|
16
|
+
const firstSegment = pathname.split('/').filter(Boolean)[0] ?? '';
|
|
17
|
+
const nonDefault = language.languages.filter((l) => l !== defaultLanguage);
|
|
18
|
+
return nonDefault.includes(firstSegment) ? firstSegment : defaultLanguage;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function buildLangPrefix(lang: string, defaultLanguage: string): string {
|
|
22
|
+
return lang === defaultLanguage ? '' : `/${lang}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function stripLangPrefix(
|
|
26
|
+
pathname: string,
|
|
27
|
+
lang: string,
|
|
28
|
+
defaultLanguage: string,
|
|
29
|
+
): string {
|
|
30
|
+
if (lang === defaultLanguage) return pathname;
|
|
31
|
+
const prefix = `/${lang}`;
|
|
32
|
+
const stripped = pathname.startsWith(prefix) ? pathname.slice(prefix.length) : pathname;
|
|
33
|
+
return stripped || '/';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function resolveLabel(
|
|
37
|
+
defaultLabel: string,
|
|
38
|
+
lang: string,
|
|
39
|
+
defaultLanguage: string,
|
|
40
|
+
translations: Record<string, Record<string, string>>,
|
|
41
|
+
): string {
|
|
42
|
+
if (lang === defaultLanguage) return defaultLabel;
|
|
43
|
+
return translations[lang]?.[defaultLabel] ?? defaultLabel;
|
|
44
|
+
}
|