@windrun-huaiin/lib 12.0.0 → 12.1.0
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 -1
- package/dist/common-app-config.js +1 -1
- package/dist/common-app-config.mjs +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +28 -7
- package/dist/utils.mjs +28 -7
- package/package.json +1 -1
- package/src/common-app-config.ts +1 -1
- package/src/utils.ts +33 -7
|
@@ -47,7 +47,7 @@ export declare function createCommonAppConfig(options?: {
|
|
|
47
47
|
i18n: {
|
|
48
48
|
locales: readonly string[];
|
|
49
49
|
defaultLocale: string;
|
|
50
|
-
|
|
50
|
+
localePrefixAsNeeded: boolean;
|
|
51
51
|
localeLabels: {
|
|
52
52
|
[k: string]: "English" | "简体中文" | "日本語" | "한국어" | "Français" | "Deutsch" | "Español" | "Italiano" | "Português" | "Türkçe" | "Polski" | "Русский" | "العربية" | "हिन्दी" | "ไทย" | "Tiếng Việt";
|
|
53
53
|
};
|
|
@@ -43,7 +43,7 @@ function createCommonAppConfig(options) {
|
|
|
43
43
|
i18n: {
|
|
44
44
|
locales: locales,
|
|
45
45
|
defaultLocale,
|
|
46
|
-
|
|
46
|
+
localePrefixAsNeeded: process.env.NEXT_PUBLIC_I18N_LOCALE_PREFIX_AS_NEEDED !== 'false',
|
|
47
47
|
localeLabels: getLocaleLabels(locales),
|
|
48
48
|
detector: {
|
|
49
49
|
storageKey: process.env.NEXT_PUBLIC_I18N_STORAGE_KEY || 'language-preference-status',
|
|
@@ -41,7 +41,7 @@ function createCommonAppConfig(options) {
|
|
|
41
41
|
i18n: {
|
|
42
42
|
locales: locales,
|
|
43
43
|
defaultLocale,
|
|
44
|
-
|
|
44
|
+
localePrefixAsNeeded: process.env.NEXT_PUBLIC_I18N_LOCALE_PREFIX_AS_NEEDED !== 'false',
|
|
45
45
|
localeLabels: getLocaleLabels(locales),
|
|
46
46
|
detector: {
|
|
47
47
|
storageKey: process.env.NEXT_PUBLIC_I18N_STORAGE_KEY || 'language-preference-status',
|
package/dist/utils.d.ts
CHANGED
|
@@ -4,5 +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,
|
|
7
|
+
export declare function getAsNeededLocalizedUrl(locale: string, path: string, localePrefixAsNeeded?: boolean, defaultLocale?: string): string;
|
|
8
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,oBAAoB,GAAE,OAAc,EACpC,aAAa,GAAE,MAAa,GAC3B,MAAM,CAyBR"}
|
package/dist/utils.js
CHANGED
|
@@ -58,19 +58,40 @@ function handlePastePlainText(e) {
|
|
|
58
58
|
}
|
|
59
59
|
// Generates localized URL based on locale and locale prefix configuration
|
|
60
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'
|
|
61
64
|
// @param locale - Current locale (e.g., 'en', 'zh', 'ja')
|
|
62
|
-
// @param path - Base path (e.g., '/blog', '/docs')
|
|
63
|
-
// @param
|
|
65
|
+
// @param path - Base path (e.g., '/', '/blog', '/docs', or '' treated as '/')
|
|
66
|
+
// @param localePrefixAsNeeded - Whether localePrefix is set to 'as-needed' (default: true)
|
|
64
67
|
// @param defaultLocale - The default locale for the application (default: 'en')
|
|
65
68
|
// @example
|
|
69
|
+
// getAsNeededLocalizedUrl('en', '/', true, 'en') // Returns '/'
|
|
70
|
+
// getAsNeededLocalizedUrl('zh', '/', true, 'en') // Returns '/zh'
|
|
66
71
|
// getAsNeededLocalizedUrl('en', '/blog', true, 'en') // Returns '/blog'
|
|
67
72
|
// getAsNeededLocalizedUrl('zh', '/blog', true, 'en') // Returns '/zh/blog'
|
|
68
|
-
// getAsNeededLocalizedUrl('en', '/blog',
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
// getAsNeededLocalizedUrl('en', '/blog/', true, 'en') // Returns '/blog'
|
|
74
|
+
// getAsNeededLocalizedUrl('en', '/', false, 'en') // Returns '/en'
|
|
75
|
+
function getAsNeededLocalizedUrl(locale, path, localePrefixAsNeeded = 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;
|
|
72
81
|
}
|
|
73
|
-
|
|
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 (localePrefixAsNeeded && 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}`;
|
|
74
95
|
}
|
|
75
96
|
|
|
76
97
|
exports.cn = cn;
|
package/dist/utils.mjs
CHANGED
|
@@ -56,19 +56,40 @@ function handlePastePlainText(e) {
|
|
|
56
56
|
}
|
|
57
57
|
// Generates localized URL based on locale and locale prefix configuration
|
|
58
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'
|
|
59
62
|
// @param locale - Current locale (e.g., 'en', 'zh', 'ja')
|
|
60
|
-
// @param path - Base path (e.g., '/blog', '/docs')
|
|
61
|
-
// @param
|
|
63
|
+
// @param path - Base path (e.g., '/', '/blog', '/docs', or '' treated as '/')
|
|
64
|
+
// @param localePrefixAsNeeded - Whether localePrefix is set to 'as-needed' (default: true)
|
|
62
65
|
// @param defaultLocale - The default locale for the application (default: 'en')
|
|
63
66
|
// @example
|
|
67
|
+
// getAsNeededLocalizedUrl('en', '/', true, 'en') // Returns '/'
|
|
68
|
+
// getAsNeededLocalizedUrl('zh', '/', true, 'en') // Returns '/zh'
|
|
64
69
|
// getAsNeededLocalizedUrl('en', '/blog', true, 'en') // Returns '/blog'
|
|
65
70
|
// getAsNeededLocalizedUrl('zh', '/blog', true, 'en') // Returns '/zh/blog'
|
|
66
|
-
// getAsNeededLocalizedUrl('en', '/blog',
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
// getAsNeededLocalizedUrl('en', '/blog/', true, 'en') // Returns '/blog'
|
|
72
|
+
// getAsNeededLocalizedUrl('en', '/', false, 'en') // Returns '/en'
|
|
73
|
+
function getAsNeededLocalizedUrl(locale, path, localePrefixAsNeeded = 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;
|
|
70
79
|
}
|
|
71
|
-
|
|
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 (localePrefixAsNeeded && 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}`;
|
|
72
93
|
}
|
|
73
94
|
|
|
74
95
|
export { cn, formatTimestamp, getAsNeededLocalizedUrl, handlePastePlainText, viewLocalTime };
|
package/package.json
CHANGED
package/src/common-app-config.ts
CHANGED
|
@@ -58,7 +58,7 @@ export function createCommonAppConfig(options?: {
|
|
|
58
58
|
i18n: {
|
|
59
59
|
locales: locales as readonly string[],
|
|
60
60
|
defaultLocale,
|
|
61
|
-
|
|
61
|
+
localePrefixAsNeeded: process.env.NEXT_PUBLIC_I18N_LOCALE_PREFIX_AS_NEEDED !== 'false',
|
|
62
62
|
localeLabels: getLocaleLabels(locales),
|
|
63
63
|
detector: {
|
|
64
64
|
storageKey: process.env.NEXT_PUBLIC_I18N_STORAGE_KEY || 'language-preference-status',
|
package/src/utils.ts
CHANGED
|
@@ -62,22 +62,48 @@ export function handlePastePlainText(e: React.ClipboardEvent<HTMLElement>) {
|
|
|
62
62
|
|
|
63
63
|
// Generates localized URL based on locale and locale prefix configuration
|
|
64
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'
|
|
65
68
|
// @param locale - Current locale (e.g., 'en', 'zh', 'ja')
|
|
66
|
-
// @param path - Base path (e.g., '/blog', '/docs')
|
|
67
|
-
// @param
|
|
69
|
+
// @param path - Base path (e.g., '/', '/blog', '/docs', or '' treated as '/')
|
|
70
|
+
// @param localePrefixAsNeeded - Whether localePrefix is set to 'as-needed' (default: true)
|
|
68
71
|
// @param defaultLocale - The default locale for the application (default: 'en')
|
|
69
72
|
// @example
|
|
73
|
+
// getAsNeededLocalizedUrl('en', '/', true, 'en') // Returns '/'
|
|
74
|
+
// getAsNeededLocalizedUrl('zh', '/', true, 'en') // Returns '/zh'
|
|
70
75
|
// getAsNeededLocalizedUrl('en', '/blog', true, 'en') // Returns '/blog'
|
|
71
76
|
// getAsNeededLocalizedUrl('zh', '/blog', true, 'en') // Returns '/zh/blog'
|
|
72
|
-
// getAsNeededLocalizedUrl('en', '/blog',
|
|
77
|
+
// getAsNeededLocalizedUrl('en', '/blog/', true, 'en') // Returns '/blog'
|
|
78
|
+
// getAsNeededLocalizedUrl('en', '/', false, 'en') // Returns '/en'
|
|
73
79
|
export function getAsNeededLocalizedUrl(
|
|
74
80
|
locale: string,
|
|
75
81
|
path: string,
|
|
76
|
-
|
|
82
|
+
localePrefixAsNeeded: boolean = true,
|
|
77
83
|
defaultLocale: string = 'en'
|
|
78
84
|
): string {
|
|
79
|
-
|
|
80
|
-
|
|
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 (localePrefixAsNeeded && 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}`;
|
|
81
106
|
}
|
|
82
|
-
|
|
107
|
+
|
|
108
|
+
return `/${locale}${normalizedPath}`;
|
|
83
109
|
}
|