@prunellejs/i18n 0.7.2 → 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.
Files changed (3) hide show
  1. package/llms.txt +1 -1
  2. package/package.json +2 -2
  3. package/src/index.ts +58 -23
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?)`, `formatNumber(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.2",
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.2"
24
+ "prunelle": "^0.7.4"
25
25
  }
26
26
  }
package/src/index.ts CHANGED
@@ -28,12 +28,41 @@ export interface I18nOptions {
28
28
  site?: string | undefined
29
29
  }
30
30
 
31
- const HTML_TOKEN_RE = /<(\/?)(pre|code|textarea)(?=[\s>/])[^>]*>|<a(?=[\s>/])((?:[^>"']|"[^"]*"|'[^']*')*)>/gi
32
- // One attribute of a start tag: its leading space, its name, and the value when it is quoted.
33
- const ATTR_RE = /(\s)([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'))?/g
34
- const NO_LOCALIZE_RE = /\sdata-no-localize(?=[\s=/]|$)/i
31
+ const HTML_TOKEN_RE = /<(\/?)(pre|code|textarea)(?=[\s>/])[^<>]*>|<a(?=[\s>/])((?:[^<>"']|"[^"]*"|'[^']*')*)>/gi
32
+ // One attribute of a start tag: optional leading separator, name, and optional `= value` (quoted or unquoted).
33
+ const ATTR_RE = /([\s/]*)([^\s"'<>/=]+)(?:(\s*=\s*)(?:"([^"]*)"|'([^']*)'|([^\s<>]+)))?/g
35
34
  const NON_HTML_EXT_RE = /\.(?:xml|json|txt|pdf|ico|png|jpe?g|gif|svg|webp|avif|webmanifest|zip|tar|gz|bz2|xz|7z|css|m?js|cjs|wasm|map|woff2?|ttf|otf|eot|mp3|mp4|webm|ogg|wav|csv|tsv|rss|atom|ics|vcf|epub|docx?|xlsx?|pptx?)$/i
36
35
 
36
+ function rewriteAnchorAttrs(attrs: string, localizeHref: (href: string) => string | undefined): string {
37
+ ATTR_RE.lastIndex = 0
38
+ let seenHref = false
39
+ let hrefReplacement: { start: number, end: number, text: string } | undefined
40
+ let match: RegExpExecArray | null
41
+ while ((match = ATTR_RE.exec(attrs)) !== null) {
42
+ const name = match[2]!.toLowerCase()
43
+ if (name === 'data-no-localize')
44
+ return attrs
45
+ if (name === 'href' && !seenHref) {
46
+ seenHref = true
47
+ const double = match[4]
48
+ const single = match[5]
49
+ const value = double ?? single
50
+ const next = value !== undefined ? localizeHref(value) : undefined
51
+ if (next !== undefined) {
52
+ const quote = double !== undefined ? '"' : '\''
53
+ hrefReplacement = {
54
+ start: match.index,
55
+ end: match.index + match[0].length,
56
+ text: `${match[1]}${match[2]}${match[3]}${quote}${next}${quote}`,
57
+ }
58
+ }
59
+ }
60
+ }
61
+ if (hrefReplacement === undefined)
62
+ return attrs
63
+ return `${attrs.slice(0, hrefReplacement.start)}${hrefReplacement.text}${attrs.slice(hrefReplacement.end)}`
64
+ }
65
+
37
66
  function isSkippedI18nPath(pathname: string, assetPrefix: string, assetPrefixSlash: string): boolean {
38
67
  return pathname === assetPrefix
39
68
  || pathname.startsWith(assetPrefixSlash)
@@ -62,13 +91,14 @@ function localizeHtmlLinks(
62
91
  return undefined
63
92
  const cut = href.search(/[?#]/)
64
93
  const pathOnly = cut === -1 ? href : href.slice(0, cut)
94
+ const suffix = cut === -1 ? '' : href.slice(cut)
65
95
  if (isSkippedI18nPath(pathOnly, assetPrefix, assetPrefixSlash))
66
96
  return undefined
67
97
  const seg = pathOnly.slice(1).split('/', 1)[0]
68
98
  if (seg !== undefined && localeSet.has(seg))
69
99
  return undefined
70
100
  // `href` is already extracted from a quoted HTML attribute; only escape the prepended locale segment to avoid double-escaping `&amp;`.
71
- return href === '/' ? `/${escapedLocale}` : `/${escapedLocale}${href}`
101
+ return pathOnly === '/' ? `/${escapedLocale}${suffix}` : `/${escapedLocale}${href}`
72
102
  }
73
103
  return html.replace(
74
104
  HTML_TOKEN_RE,
@@ -94,17 +124,10 @@ function localizeHtmlLinks(
94
124
  }
95
125
  return match
96
126
  }
97
- if (inTextarea || codePreDepth > 0 || attrs === undefined || NO_LOCALIZE_RE.test(attrs))
127
+ if (inTextarea || codePreDepth > 0 || attrs === undefined)
98
128
  return match
99
- const rewritten = attrs.replace(ATTR_RE, (attr, space: string, name: string, double: string | undefined, single: string | undefined) => {
100
- const value = double ?? single
101
- const next = name.toLowerCase() === 'href' && value !== undefined ? localizeHref(value) : undefined
102
- if (next === undefined)
103
- return attr
104
- const quote = double !== undefined ? '"' : "'"
105
- return `${space}${name}=${quote}${next}${quote}`
106
- })
107
- return `<a${rewritten}>`
129
+ const rewritten = rewriteAnchorAttrs(attrs, localizeHref)
130
+ return rewritten === attrs ? match : `<a${rewritten}>`
108
131
  },
109
132
  )
110
133
  }
@@ -116,15 +139,22 @@ function collapseLeadingSlashes(p: string): string {
116
139
  return collapsed.startsWith('/') ? collapsed : `/${collapsed}`
117
140
  }
118
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
+
119
147
  export function stripLocalePrefix(pathname: string, locales: readonly string[]): { locale: string | undefined, path: string } {
120
- const normalized = collapseLeadingSlashes(pathname)
148
+ const { path: rawPath, suffix } = splitPathAndSuffix(pathname)
149
+ const normalized = collapseLeadingSlashes(rawPath)
121
150
  const parts = normalized.split('/')
122
151
  const first = parts[1]
123
152
  if (first !== undefined && locales.includes(first)) {
124
153
  const rest = collapseLeadingSlashes(`/${parts.slice(2).join('/')}`)
125
- return { locale: first, path: rest === '/' ? '/' : rest.replace(/\/+$/, '') || '/' }
154
+ const cleanRest = rest === '/' ? '/' : rest.replace(/\/+$/, '') || '/'
155
+ return { locale: first, path: `${cleanRest}${suffix}` }
126
156
  }
127
- return { locale: undefined, path: normalized }
157
+ return { locale: undefined, path: `${normalized}${suffix}` }
128
158
  }
129
159
 
130
160
  export function localizePath(
@@ -135,10 +165,11 @@ export function localizePath(
135
165
  prefixDefault = false,
136
166
  ): string {
137
167
  const { path } = stripLocalePrefix(pathname, locales)
138
- const safePath = collapseLeadingSlashes(path)
168
+ const { path: rawPath, suffix } = splitPathAndSuffix(path)
169
+ const safePath = collapseLeadingSlashes(rawPath)
139
170
  if (targetLocale === defaultLocale && !prefixDefault)
140
- return safePath
141
- return safePath === '/' ? `/${targetLocale}` : `/${targetLocale}${safePath}`
171
+ return `${safePath}${suffix}`
172
+ return safePath === '/' ? `/${targetLocale}${suffix}` : `/${targetLocale}${safePath}${suffix}`
142
173
  }
143
174
 
144
175
  export function alternateLinks(
@@ -152,13 +183,17 @@ export function alternateLinks(
152
183
  }))
153
184
  }
154
185
 
186
+ const ISO_DATE_ONLY_RE = /^\d{4}(?:-\d{2}(?:-\d{2})?)?$/
187
+
155
188
  export function formatDate(
156
189
  value: Date | string | number,
157
190
  locale: string,
158
191
  options: Intl.DateTimeFormatOptions = { dateStyle: 'long' },
159
192
  ): string {
160
- const d = value instanceof Date ? value : new Date(value)
161
- return new Intl.DateTimeFormat(locale, options).format(d)
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)
162
197
  }
163
198
 
164
199
  export function formatNumber(