@prunellejs/i18n 0.7.3 → 0.7.4
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/llms.txt +1 -1
- package/package.json +2 -2
- package/src/index.ts +22 -9
package/llms.txt
CHANGED
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
- `localizePath(pathname, targetLocale, defaultLocale, locales, prefixDefault?)`
|
|
37
37
|
- `stripLocalePrefix(pathname, locales)` → `{ locale, path }`
|
|
38
38
|
- `alternateLinks(pathname, { site?, locales, defaultLocale, prefixDefault? })`
|
|
39
|
-
- `formatDate(value, locale, options?)
|
|
39
|
+
- `formatDate(value, locale, options?)` (ISO date-only strings like `'2026-03-29'` default to `timeZone: 'UTC'` so they format the written calendar date in every timezone), `formatNumber(value, locale, options?)`
|
|
40
40
|
|
|
41
41
|
## Snippets
|
|
42
42
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/i18n",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "Locale URL rewriting and dictionary internationalization plugin for Prunelle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,6 +21,6 @@
|
|
|
21
21
|
"directory": "packages/plugin-i18n"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"prunelle": "^0.7.
|
|
24
|
+
"prunelle": "^0.7.4"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/src/index.ts
CHANGED
|
@@ -91,13 +91,14 @@ function localizeHtmlLinks(
|
|
|
91
91
|
return undefined
|
|
92
92
|
const cut = href.search(/[?#]/)
|
|
93
93
|
const pathOnly = cut === -1 ? href : href.slice(0, cut)
|
|
94
|
+
const suffix = cut === -1 ? '' : href.slice(cut)
|
|
94
95
|
if (isSkippedI18nPath(pathOnly, assetPrefix, assetPrefixSlash))
|
|
95
96
|
return undefined
|
|
96
97
|
const seg = pathOnly.slice(1).split('/', 1)[0]
|
|
97
98
|
if (seg !== undefined && localeSet.has(seg))
|
|
98
99
|
return undefined
|
|
99
100
|
// `href` is already extracted from a quoted HTML attribute; only escape the prepended locale segment to avoid double-escaping `&`.
|
|
100
|
-
return
|
|
101
|
+
return pathOnly === '/' ? `/${escapedLocale}${suffix}` : `/${escapedLocale}${href}`
|
|
101
102
|
}
|
|
102
103
|
return html.replace(
|
|
103
104
|
HTML_TOKEN_RE,
|
|
@@ -138,15 +139,22 @@ function collapseLeadingSlashes(p: string): string {
|
|
|
138
139
|
return collapsed.startsWith('/') ? collapsed : `/${collapsed}`
|
|
139
140
|
}
|
|
140
141
|
|
|
142
|
+
function splitPathAndSuffix(input: string): { path: string, suffix: string } {
|
|
143
|
+
const cut = input.search(/[?#]/)
|
|
144
|
+
return cut === -1 ? { path: input, suffix: '' } : { path: input.slice(0, cut), suffix: input.slice(cut) }
|
|
145
|
+
}
|
|
146
|
+
|
|
141
147
|
export function stripLocalePrefix(pathname: string, locales: readonly string[]): { locale: string | undefined, path: string } {
|
|
142
|
-
const
|
|
148
|
+
const { path: rawPath, suffix } = splitPathAndSuffix(pathname)
|
|
149
|
+
const normalized = collapseLeadingSlashes(rawPath)
|
|
143
150
|
const parts = normalized.split('/')
|
|
144
151
|
const first = parts[1]
|
|
145
152
|
if (first !== undefined && locales.includes(first)) {
|
|
146
153
|
const rest = collapseLeadingSlashes(`/${parts.slice(2).join('/')}`)
|
|
147
|
-
|
|
154
|
+
const cleanRest = rest === '/' ? '/' : rest.replace(/\/+$/, '') || '/'
|
|
155
|
+
return { locale: first, path: `${cleanRest}${suffix}` }
|
|
148
156
|
}
|
|
149
|
-
return { locale: undefined, path: normalized }
|
|
157
|
+
return { locale: undefined, path: `${normalized}${suffix}` }
|
|
150
158
|
}
|
|
151
159
|
|
|
152
160
|
export function localizePath(
|
|
@@ -157,10 +165,11 @@ export function localizePath(
|
|
|
157
165
|
prefixDefault = false,
|
|
158
166
|
): string {
|
|
159
167
|
const { path } = stripLocalePrefix(pathname, locales)
|
|
160
|
-
const
|
|
168
|
+
const { path: rawPath, suffix } = splitPathAndSuffix(path)
|
|
169
|
+
const safePath = collapseLeadingSlashes(rawPath)
|
|
161
170
|
if (targetLocale === defaultLocale && !prefixDefault)
|
|
162
|
-
return safePath
|
|
163
|
-
return safePath === '/' ? `/${targetLocale}` : `/${targetLocale}${safePath}`
|
|
171
|
+
return `${safePath}${suffix}`
|
|
172
|
+
return safePath === '/' ? `/${targetLocale}${suffix}` : `/${targetLocale}${safePath}${suffix}`
|
|
164
173
|
}
|
|
165
174
|
|
|
166
175
|
export function alternateLinks(
|
|
@@ -174,13 +183,17 @@ export function alternateLinks(
|
|
|
174
183
|
}))
|
|
175
184
|
}
|
|
176
185
|
|
|
186
|
+
const ISO_DATE_ONLY_RE = /^\d{4}(?:-\d{2}(?:-\d{2})?)?$/
|
|
187
|
+
|
|
177
188
|
export function formatDate(
|
|
178
189
|
value: Date | string | number,
|
|
179
190
|
locale: string,
|
|
180
191
|
options: Intl.DateTimeFormatOptions = { dateStyle: 'long' },
|
|
181
192
|
): string {
|
|
182
|
-
const
|
|
183
|
-
|
|
193
|
+
const isDateOnly = typeof value === 'string' && ISO_DATE_ONLY_RE.test(value.trim())
|
|
194
|
+
const d = value instanceof Date ? value : new Date(isDateOnly ? value.trim() : value)
|
|
195
|
+
const fmtOptions = isDateOnly && options.timeZone === undefined ? { ...options, timeZone: 'UTC' } : options
|
|
196
|
+
return new Intl.DateTimeFormat(locale, fmtOptions).format(d)
|
|
184
197
|
}
|
|
185
198
|
|
|
186
199
|
export function formatNumber(
|