@coffic/cosy-ui 0.5.2 → 0.5.6
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/app.css +1 -1
- package/dist/components/base/Image.astro +2 -1
- package/dist/components/layouts/AppLayout.astro +4 -20
- package/dist/components/layouts/BaseLayout.astro +1 -1
- package/dist/components/layouts/Footer.astro +14 -9
- package/dist/components/layouts/Grid.astro +106 -98
- package/dist/components/layouts/Header.astro +153 -39
- package/dist/components/layouts/Sidebar.astro +59 -3
- package/dist/components/navigation/TableOfContents.astro +3 -3
- package/dist/integration.ts +4 -4
- package/dist/style.ts +2 -1
- package/dist/types/footer.ts +130 -135
- package/dist/types/header.ts +51 -55
- package/dist/utils/i18n.ts +57 -59
- package/dist/utils/language.ts +102 -162
- package/package.json +2 -2
package/dist/utils/language.ts
CHANGED
@@ -1,175 +1,115 @@
|
|
1
1
|
/**
|
2
2
|
* 语言工具模块
|
3
|
-
*
|
3
|
+
*
|
4
4
|
* 提供语言相关的工具函数,用于多语言支持
|
5
5
|
*/
|
6
6
|
|
7
7
|
import { getRelativeLocaleUrl } from 'astro:i18n';
|
8
8
|
import { logger } from './logger';
|
9
|
-
import { LinkUtil } from './link';
|
10
9
|
import type { AstroGlobal } from 'astro';
|
11
10
|
|
12
|
-
// 支持的语言列表
|
13
|
-
export const SUPPORTED_LANGUAGES = ['en', 'zh-cn', 'zh'] as const;
|
14
|
-
export type SupportedLanguage = typeof SUPPORTED_LANGUAGES[number];
|
15
|
-
|
16
11
|
// 默认语言
|
17
|
-
export const DEFAULT_LANGUAGE
|
12
|
+
export const DEFAULT_LANGUAGE = 'en';
|
18
13
|
|
19
14
|
export class LanguageUtil {
|
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
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
}
|
121
|
-
|
122
|
-
// 检查语言前缀匹配
|
123
|
-
// 例如: zh-TW -> zh-cn
|
124
|
-
const langPrefix = browserLang.split('-')[0];
|
125
|
-
for (const lang of SUPPORTED_LANGUAGES) {
|
126
|
-
if (lang.startsWith(langPrefix)) {
|
127
|
-
return lang;
|
128
|
-
}
|
129
|
-
}
|
130
|
-
|
131
|
-
return undefined;
|
132
|
-
}
|
133
|
-
/**
|
134
|
-
* 获取当前语言
|
135
|
-
* @param userLang 用户指定的语言(可选)
|
136
|
-
* @param url 当前URL(可选)
|
137
|
-
* @returns 当前应使用的语言信息,包括语言代码和来源
|
138
|
-
*/
|
139
|
-
static getCurrentLanguage(userLang?: string, url?: string): string {
|
140
|
-
// 如果用户指定了语言,优先使用用户指定的语言
|
141
|
-
if (userLang) {
|
142
|
-
if (this.isLanguageSupported(userLang)) {
|
143
|
-
return userLang as SupportedLanguage;
|
144
|
-
} else {
|
145
|
-
// 用户指定的语言不支持,使用默认语言
|
146
|
-
return DEFAULT_LANGUAGE;
|
147
|
-
}
|
148
|
-
}
|
149
|
-
|
150
|
-
// 否则自动检测语言
|
151
|
-
return this.detectLanguage();
|
152
|
-
}
|
153
|
-
|
154
|
-
/**
|
155
|
-
* 自动检测当前语言
|
156
|
-
* @param url 当前URL(可选)
|
157
|
-
* @returns 检测到的语言信息,包括语言代码和来源
|
158
|
-
*/
|
159
|
-
static detectLanguage(url?: string): string {
|
160
|
-
// 尝试从URL中获取语言
|
161
|
-
const urlLang = this.getLanguageFromURL(url);
|
162
|
-
if (urlLang) {
|
163
|
-
return urlLang as SupportedLanguage;
|
164
|
-
}
|
165
|
-
|
166
|
-
// 尝试从浏览器设置中获取语言
|
167
|
-
const browserLang = this.getLanguageFromBrowser();
|
168
|
-
if (browserLang) {
|
169
|
-
return browserLang as SupportedLanguage;
|
170
|
-
}
|
171
|
-
|
172
|
-
// 如果无法检测,返回默认语言
|
173
|
-
return DEFAULT_LANGUAGE;
|
174
|
-
}
|
15
|
+
static getRelativeLink(locale: string, astro: AstroGlobal): string {
|
16
|
+
const debug = false;
|
17
|
+
const currentLocale = astro.currentLocale;
|
18
|
+
const originalPath = astro.originPathname;
|
19
|
+
const result = getRelativeLocaleUrl(locale, originalPath.replaceAll('/' + currentLocale, ''));
|
20
|
+
|
21
|
+
if (debug) {
|
22
|
+
logger.debug(
|
23
|
+
`getRelativeLink: locale=${locale}, currentPath=${originalPath}, currentLocale=${currentLocale}, result=${result}`
|
24
|
+
);
|
25
|
+
}
|
26
|
+
|
27
|
+
return result;
|
28
|
+
}
|
29
|
+
|
30
|
+
static getLanguageName(code: string | undefined): string {
|
31
|
+
switch (code) {
|
32
|
+
case 'en':
|
33
|
+
return 'English';
|
34
|
+
case 'zh-cn':
|
35
|
+
return '简体中文';
|
36
|
+
case 'zh':
|
37
|
+
return '中文';
|
38
|
+
case undefined:
|
39
|
+
default:
|
40
|
+
return 'not known';
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
* 获取当前语言
|
46
|
+
* @param astro Astro全局对象
|
47
|
+
* @returns 当前应使用的语言代码
|
48
|
+
*/
|
49
|
+
static getCurrentLanguage(astro: AstroGlobal): string {
|
50
|
+
// 尝试从URL中获取语言
|
51
|
+
const urlLang = this.getLanguageFromURL(astro.url.pathname);
|
52
|
+
if (urlLang) {
|
53
|
+
return urlLang;
|
54
|
+
}
|
55
|
+
|
56
|
+
// 尝试从浏览器设置中获取语言
|
57
|
+
const browserLang = this.getLanguageFromBrowser();
|
58
|
+
if (browserLang) {
|
59
|
+
return browserLang;
|
60
|
+
}
|
61
|
+
|
62
|
+
// 如果无法检测,返回默认语言
|
63
|
+
return DEFAULT_LANGUAGE;
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* 从URL中提取语言代码
|
68
|
+
* @param url 当前URL
|
69
|
+
* @returns 从URL中提取的语言代码,如果无法提取则返回undefined
|
70
|
+
*/
|
71
|
+
private static getLanguageFromURL(url: string): string | undefined {
|
72
|
+
let currentUrl = url;
|
73
|
+
|
74
|
+
if (currentUrl === undefined) {
|
75
|
+
if (typeof window === 'undefined') return undefined;
|
76
|
+
currentUrl = window.location.href;
|
77
|
+
}
|
78
|
+
|
79
|
+
// 尝试从路径中提取语言代码
|
80
|
+
// 例如: /zh-cn/components/button
|
81
|
+
const pathMatch = currentUrl.match(/^\/([\w-]+)\//);
|
82
|
+
if (pathMatch) {
|
83
|
+
return pathMatch[1];
|
84
|
+
}
|
85
|
+
|
86
|
+
// 如果网站运行在二级目录,则从路径中提取语言代码
|
87
|
+
// 例如: /docs/zh-cn/components/button
|
88
|
+
const pathMatch2 = currentUrl.match(/^\/([^\/]+)\/([\w-]+)\//);
|
89
|
+
if (pathMatch2) {
|
90
|
+
return pathMatch2[2];
|
91
|
+
}
|
92
|
+
|
93
|
+
// 尝试从查询参数中提取语言代码
|
94
|
+
// 例如: ?lang=zh-cn
|
95
|
+
const urlParams = new URLSearchParams(currentUrl.split('?')[1]);
|
96
|
+
const langParam = urlParams.get('lang');
|
97
|
+
if (langParam) {
|
98
|
+
return langParam;
|
99
|
+
}
|
100
|
+
|
101
|
+
return undefined;
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* 从浏览器设置中获取首选语言
|
106
|
+
* @returns 从浏览器设置中获取的语言代码,如果无法获取则返回undefined
|
107
|
+
*/
|
108
|
+
private static getLanguageFromBrowser(): string | undefined {
|
109
|
+
if (typeof navigator === 'undefined') return undefined;
|
110
|
+
|
111
|
+
// 获取浏览器语言
|
112
|
+
const browserLang = navigator.language.toLowerCase();
|
113
|
+
return browserLang;
|
114
|
+
}
|
175
115
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@coffic/cosy-ui",
|
3
|
-
"version": "0.5.
|
3
|
+
"version": "0.5.6",
|
4
4
|
"description": "An astro component library",
|
5
5
|
"author": {
|
6
6
|
"name": "nookery",
|
@@ -31,7 +31,7 @@
|
|
31
31
|
"index.ts"
|
32
32
|
],
|
33
33
|
"scripts": {
|
34
|
-
"dev": "astro dev --host 0.0.0.0 --port
|
34
|
+
"dev": "astro dev --host 0.0.0.0 --port 6677",
|
35
35
|
"preview:docs": "astro preview --host 0.0.0.0 --port 4330 --outDir dist-docs",
|
36
36
|
"preview": "npm run preview:docs",
|
37
37
|
"build": "vite build && npm run build:docs && tsx scripts/post-build.ts",
|