@windrun-huaiin/lib 11.0.1 → 12.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/dist/common-app-config.d.ts +1 -0
- package/dist/common-app-config.d.ts.map +1 -1
- package/dist/common-app-config.js +1 -0
- package/dist/common-app-config.mjs +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +38 -0
- package/dist/utils.mjs +38 -1
- package/package.json +1 -1
- package/src/common-app-config.ts +1 -0
- package/src/utils.ts +49 -1
|
@@ -47,6 +47,7 @@ export declare function createCommonAppConfig(options?: {
|
|
|
47
47
|
i18n: {
|
|
48
48
|
locales: readonly string[];
|
|
49
49
|
defaultLocale: string;
|
|
50
|
+
localPrefixAsNeeded: boolean;
|
|
50
51
|
localeLabels: {
|
|
51
52
|
[k: string]: "English" | "简体中文" | "日本語" | "한국어" | "Français" | "Deutsch" | "Español" | "Italiano" | "Português" | "Türkçe" | "Polski" | "Русский" | "العربية" | "हिन्दी" | "ไทย" | "Tiếng Việt";
|
|
52
53
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common-app-config.d.ts","sourceRoot":"","sources":["../src/common-app-config.ts"],"names":[],"mappings":"AACA,QAAA,MAAM,iBAAiB;;;;;;;;;;;;;;;;;CAiBb,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,iBAAiB,CAAC;AAa7D,wBAAgB,qBAAqB,CAAC,OAAO,CAAC,EAAE;IAE9C,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;;;;;;;;;;;;;;;;;;;;;;;;;iBAqByB,SAAS,MAAM,EAAE
|
|
1
|
+
{"version":3,"file":"common-app-config.d.ts","sourceRoot":"","sources":["../src/common-app-config.ts"],"names":[],"mappings":"AACA,QAAA,MAAM,iBAAiB;;;;;;;;;;;;;;;;;CAiBb,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,iBAAiB,CAAC;AAa7D,wBAAgB,qBAAqB,CAAC,OAAO,CAAC,EAAE;IAE9C,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;;;;;;;;;;;;;;;;;;;;;;;;;iBAqByB,SAAS,MAAM,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAiE1C;AAGD,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,UAAU,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC;gCACzD,MAAM,KAAG,MAAM,IAAI,CAAA,OAAO,UAAU,CAAC,OAAO,EAAC,MAAM,CAAC;6BAIvD,MAAM,KAAG,CAAA,OAAO,UAAU,CAAC,OAAO,EAAC,MAAM,CAAC;;;;;EAc3E;AAGD,eAAO,MAAM,cAAc;;iBAEK,MAAM,EAAE;uBAAyB,MAAM;;;iBAGnC,MAAM,EAAE;uBAAyB,MAAM;;;iBAG5B,MAAM,EAAE;uBAAyB,MAAM;;;iBAG/B,MAAM,EAAE;uBAAyB,MAAM;;;iBAGT,MAAM,EAAE;uBAAyB,MAAM;;;iBAGnG,MAAM,EAAE;uBAAyB,MAAM;;CAC/D,CAAC"}
|
|
@@ -43,6 +43,7 @@ function createCommonAppConfig(options) {
|
|
|
43
43
|
i18n: {
|
|
44
44
|
locales: locales,
|
|
45
45
|
defaultLocale,
|
|
46
|
+
localPrefixAsNeeded: process.env.NEXT_PUBLIC_I18N_LOCALE_PREFIX_AS_NEEDED !== 'false',
|
|
46
47
|
localeLabels: getLocaleLabels(locales),
|
|
47
48
|
detector: {
|
|
48
49
|
storageKey: process.env.NEXT_PUBLIC_I18N_STORAGE_KEY || 'language-preference-status',
|
|
@@ -41,6 +41,7 @@ function createCommonAppConfig(options) {
|
|
|
41
41
|
i18n: {
|
|
42
42
|
locales: locales,
|
|
43
43
|
defaultLocale,
|
|
44
|
+
localPrefixAsNeeded: process.env.NEXT_PUBLIC_I18N_LOCALE_PREFIX_AS_NEEDED !== 'false',
|
|
44
45
|
localeLabels: getLocaleLabels(locales),
|
|
45
46
|
detector: {
|
|
46
47
|
storageKey: process.env.NEXT_PUBLIC_I18N_STORAGE_KEY || 'language-preference-status',
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ var commonAppConfig = require('./common-app-config.js');
|
|
|
8
8
|
|
|
9
9
|
exports.cn = utils.cn;
|
|
10
10
|
exports.formatTimestamp = utils.formatTimestamp;
|
|
11
|
+
exports.getAsNeededLocalizedUrl = utils.getAsNeededLocalizedUrl;
|
|
11
12
|
exports.handlePastePlainText = utils.handlePastePlainText;
|
|
12
13
|
exports.viewLocalTime = utils.viewLocalTime;
|
|
13
14
|
exports.getLLMText = llmUtils.getLLMText;
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { cn, formatTimestamp, handlePastePlainText, viewLocalTime } from './utils.mjs';
|
|
1
|
+
export { cn, formatTimestamp, getAsNeededLocalizedUrl, handlePastePlainText, viewLocalTime } from './utils.mjs';
|
|
2
2
|
export { getLLMText } from './llm-utils.mjs';
|
|
3
3
|
export { LOCALE_PRESETS, createCommonAppConfig, createI18nHelpers } from './common-app-config.mjs';
|
package/dist/utils.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export declare function cn(...inputs: ClassValue[]): string;
|
|
|
4
4
|
export declare function viewLocalTime(origin: Date | null): string;
|
|
5
5
|
export declare function formatTimestamp(timestamp: string, formatter: string): string;
|
|
6
6
|
export declare function handlePastePlainText(e: React.ClipboardEvent<HTMLElement>): void;
|
|
7
|
+
export declare function getAsNeededLocalizedUrl(locale: string, path: string, localPrefixAsNeeded?: boolean, defaultLocale?: string): string;
|
|
7
8
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AACpC,OAAO,EAAQ,KAAK,UAAU,EAAE,MAAM,MAAM,CAAA;AAI5C,wBAAgB,EAAE,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,UAEzC;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,IAAI,GAAG,IAAI,UAEhD;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,UA4BnE;AAGD,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,KAAK,CAAC,cAAc,CAAC,WAAW,CAAC,QAgBxE"}
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AACpC,OAAO,EAAQ,KAAK,UAAU,EAAE,MAAM,MAAM,CAAA;AAI5C,wBAAgB,EAAE,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,UAEzC;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,IAAI,GAAG,IAAI,UAEhD;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,UA4BnE;AAGD,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,KAAK,CAAC,cAAc,CAAC,WAAW,CAAC,QAgBxE;AAkBD,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,mBAAmB,GAAE,OAAc,EACnC,aAAa,GAAE,MAAa,GAC3B,MAAM,CAyBR"}
|
package/dist/utils.js
CHANGED
|
@@ -56,8 +56,46 @@ function handlePastePlainText(e) {
|
|
|
56
56
|
selection.removeAllRanges();
|
|
57
57
|
selection.addRange(range);
|
|
58
58
|
}
|
|
59
|
+
// Generates localized URL based on locale and locale prefix configuration
|
|
60
|
+
// Supports both 'as-needed' and 'always' localePrefix configurations
|
|
61
|
+
// Ensures URLs have proper trailing slash handling:
|
|
62
|
+
// - Root path (domain only) has trailing slash: '/'
|
|
63
|
+
// - All other paths have no trailing slash: '/blog', '/zh/blog', '/en'
|
|
64
|
+
// @param locale - Current locale (e.g., 'en', 'zh', 'ja')
|
|
65
|
+
// @param path - Base path (e.g., '/', '/blog', '/docs', or '' treated as '/')
|
|
66
|
+
// @param localPrefixAsNeeded - Whether localePrefix is set to 'as-needed' (default: true)
|
|
67
|
+
// @param defaultLocale - The default locale for the application (default: 'en')
|
|
68
|
+
// @example
|
|
69
|
+
// getAsNeededLocalizedUrl('en', '/', true, 'en') // Returns '/'
|
|
70
|
+
// getAsNeededLocalizedUrl('zh', '/', true, 'en') // Returns '/zh'
|
|
71
|
+
// getAsNeededLocalizedUrl('en', '/blog', true, 'en') // Returns '/blog'
|
|
72
|
+
// getAsNeededLocalizedUrl('zh', '/blog', true, 'en') // Returns '/zh/blog'
|
|
73
|
+
// getAsNeededLocalizedUrl('en', '/blog/', true, 'en') // Returns '/blog'
|
|
74
|
+
// getAsNeededLocalizedUrl('en', '/', false, 'en') // Returns '/en'
|
|
75
|
+
function getAsNeededLocalizedUrl(locale, path, localPrefixAsNeeded = true, defaultLocale = 'en') {
|
|
76
|
+
// Normalize path: empty string or undefined treated as '/'
|
|
77
|
+
let normalizedPath = (path || '/').trim();
|
|
78
|
+
// Ensure path starts with '/'
|
|
79
|
+
if (!normalizedPath.startsWith('/')) {
|
|
80
|
+
normalizedPath = '/' + normalizedPath;
|
|
81
|
+
}
|
|
82
|
+
// Remove trailing slashes except for root path '/'
|
|
83
|
+
if (normalizedPath !== '/' && normalizedPath.endsWith('/')) {
|
|
84
|
+
normalizedPath = normalizedPath.replace(/\/+$/, '');
|
|
85
|
+
}
|
|
86
|
+
// Return based on locale prefix configuration
|
|
87
|
+
if (localPrefixAsNeeded && locale === defaultLocale) {
|
|
88
|
+
return normalizedPath;
|
|
89
|
+
}
|
|
90
|
+
// Add locale prefix, but avoid double trailing slash when path is '/'
|
|
91
|
+
if (normalizedPath === '/') {
|
|
92
|
+
return `/${locale}`;
|
|
93
|
+
}
|
|
94
|
+
return `/${locale}${normalizedPath}`;
|
|
95
|
+
}
|
|
59
96
|
|
|
60
97
|
exports.cn = cn;
|
|
61
98
|
exports.formatTimestamp = formatTimestamp;
|
|
99
|
+
exports.getAsNeededLocalizedUrl = getAsNeededLocalizedUrl;
|
|
62
100
|
exports.handlePastePlainText = handlePastePlainText;
|
|
63
101
|
exports.viewLocalTime = viewLocalTime;
|
package/dist/utils.mjs
CHANGED
|
@@ -54,5 +54,42 @@ function handlePastePlainText(e) {
|
|
|
54
54
|
selection.removeAllRanges();
|
|
55
55
|
selection.addRange(range);
|
|
56
56
|
}
|
|
57
|
+
// Generates localized URL based on locale and locale prefix configuration
|
|
58
|
+
// Supports both 'as-needed' and 'always' localePrefix configurations
|
|
59
|
+
// Ensures URLs have proper trailing slash handling:
|
|
60
|
+
// - Root path (domain only) has trailing slash: '/'
|
|
61
|
+
// - All other paths have no trailing slash: '/blog', '/zh/blog', '/en'
|
|
62
|
+
// @param locale - Current locale (e.g., 'en', 'zh', 'ja')
|
|
63
|
+
// @param path - Base path (e.g., '/', '/blog', '/docs', or '' treated as '/')
|
|
64
|
+
// @param localPrefixAsNeeded - Whether localePrefix is set to 'as-needed' (default: true)
|
|
65
|
+
// @param defaultLocale - The default locale for the application (default: 'en')
|
|
66
|
+
// @example
|
|
67
|
+
// getAsNeededLocalizedUrl('en', '/', true, 'en') // Returns '/'
|
|
68
|
+
// getAsNeededLocalizedUrl('zh', '/', true, 'en') // Returns '/zh'
|
|
69
|
+
// getAsNeededLocalizedUrl('en', '/blog', true, 'en') // Returns '/blog'
|
|
70
|
+
// getAsNeededLocalizedUrl('zh', '/blog', true, 'en') // Returns '/zh/blog'
|
|
71
|
+
// getAsNeededLocalizedUrl('en', '/blog/', true, 'en') // Returns '/blog'
|
|
72
|
+
// getAsNeededLocalizedUrl('en', '/', false, 'en') // Returns '/en'
|
|
73
|
+
function getAsNeededLocalizedUrl(locale, path, localPrefixAsNeeded = true, defaultLocale = 'en') {
|
|
74
|
+
// Normalize path: empty string or undefined treated as '/'
|
|
75
|
+
let normalizedPath = (path || '/').trim();
|
|
76
|
+
// Ensure path starts with '/'
|
|
77
|
+
if (!normalizedPath.startsWith('/')) {
|
|
78
|
+
normalizedPath = '/' + normalizedPath;
|
|
79
|
+
}
|
|
80
|
+
// Remove trailing slashes except for root path '/'
|
|
81
|
+
if (normalizedPath !== '/' && normalizedPath.endsWith('/')) {
|
|
82
|
+
normalizedPath = normalizedPath.replace(/\/+$/, '');
|
|
83
|
+
}
|
|
84
|
+
// Return based on locale prefix configuration
|
|
85
|
+
if (localPrefixAsNeeded && locale === defaultLocale) {
|
|
86
|
+
return normalizedPath;
|
|
87
|
+
}
|
|
88
|
+
// Add locale prefix, but avoid double trailing slash when path is '/'
|
|
89
|
+
if (normalizedPath === '/') {
|
|
90
|
+
return `/${locale}`;
|
|
91
|
+
}
|
|
92
|
+
return `/${locale}${normalizedPath}`;
|
|
93
|
+
}
|
|
57
94
|
|
|
58
|
-
export { cn, formatTimestamp, handlePastePlainText, viewLocalTime };
|
|
95
|
+
export { cn, formatTimestamp, getAsNeededLocalizedUrl, handlePastePlainText, viewLocalTime };
|
package/package.json
CHANGED
package/src/common-app-config.ts
CHANGED
|
@@ -58,6 +58,7 @@ export function createCommonAppConfig(options?: {
|
|
|
58
58
|
i18n: {
|
|
59
59
|
locales: locales as readonly string[],
|
|
60
60
|
defaultLocale,
|
|
61
|
+
localPrefixAsNeeded: process.env.NEXT_PUBLIC_I18N_LOCALE_PREFIX_AS_NEEDED !== 'false',
|
|
61
62
|
localeLabels: getLocaleLabels(locales),
|
|
62
63
|
detector: {
|
|
63
64
|
storageKey: process.env.NEXT_PUBLIC_I18N_STORAGE_KEY || 'language-preference-status',
|
package/src/utils.ts
CHANGED
|
@@ -39,7 +39,7 @@ export function formatTimestamp(timestamp: string, formatter: string) {
|
|
|
39
39
|
console.error("Error formatting date:", error);
|
|
40
40
|
return fail;
|
|
41
41
|
}
|
|
42
|
-
}
|
|
42
|
+
}
|
|
43
43
|
|
|
44
44
|
// Only allow pasting plain text, prohibit style content
|
|
45
45
|
export function handlePastePlainText(e: React.ClipboardEvent<HTMLElement>) {
|
|
@@ -58,4 +58,52 @@ export function handlePastePlainText(e: React.ClipboardEvent<HTMLElement>) {
|
|
|
58
58
|
range.collapse(true);
|
|
59
59
|
selection.removeAllRanges();
|
|
60
60
|
selection.addRange(range);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Generates localized URL based on locale and locale prefix configuration
|
|
64
|
+
// Supports both 'as-needed' and 'always' localePrefix configurations
|
|
65
|
+
// Ensures URLs have proper trailing slash handling:
|
|
66
|
+
// - Root path (domain only) has trailing slash: '/'
|
|
67
|
+
// - All other paths have no trailing slash: '/blog', '/zh/blog', '/en'
|
|
68
|
+
// @param locale - Current locale (e.g., 'en', 'zh', 'ja')
|
|
69
|
+
// @param path - Base path (e.g., '/', '/blog', '/docs', or '' treated as '/')
|
|
70
|
+
// @param localPrefixAsNeeded - Whether localePrefix is set to 'as-needed' (default: true)
|
|
71
|
+
// @param defaultLocale - The default locale for the application (default: 'en')
|
|
72
|
+
// @example
|
|
73
|
+
// getAsNeededLocalizedUrl('en', '/', true, 'en') // Returns '/'
|
|
74
|
+
// getAsNeededLocalizedUrl('zh', '/', true, 'en') // Returns '/zh'
|
|
75
|
+
// getAsNeededLocalizedUrl('en', '/blog', true, 'en') // Returns '/blog'
|
|
76
|
+
// getAsNeededLocalizedUrl('zh', '/blog', true, 'en') // Returns '/zh/blog'
|
|
77
|
+
// getAsNeededLocalizedUrl('en', '/blog/', true, 'en') // Returns '/blog'
|
|
78
|
+
// getAsNeededLocalizedUrl('en', '/', false, 'en') // Returns '/en'
|
|
79
|
+
export function getAsNeededLocalizedUrl(
|
|
80
|
+
locale: string,
|
|
81
|
+
path: string,
|
|
82
|
+
localPrefixAsNeeded: boolean = true,
|
|
83
|
+
defaultLocale: string = 'en'
|
|
84
|
+
): string {
|
|
85
|
+
// Normalize path: empty string or undefined treated as '/'
|
|
86
|
+
let normalizedPath = (path || '/').trim();
|
|
87
|
+
|
|
88
|
+
// Ensure path starts with '/'
|
|
89
|
+
if (!normalizedPath.startsWith('/')) {
|
|
90
|
+
normalizedPath = '/' + normalizedPath;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Remove trailing slashes except for root path '/'
|
|
94
|
+
if (normalizedPath !== '/' && normalizedPath.endsWith('/')) {
|
|
95
|
+
normalizedPath = normalizedPath.replace(/\/+$/, '');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Return based on locale prefix configuration
|
|
99
|
+
if (localPrefixAsNeeded && locale === defaultLocale) {
|
|
100
|
+
return normalizedPath;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Add locale prefix, but avoid double trailing slash when path is '/'
|
|
104
|
+
if (normalizedPath === '/') {
|
|
105
|
+
return `/${locale}`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return `/${locale}${normalizedPath}`;
|
|
61
109
|
}
|