@prunellejs/i18n 0.7.3 → 0.8.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.
Files changed (3) hide show
  1. package/llms.txt +2 -2
  2. package/package.json +2 -2
  3. package/src/index.ts +32 -16
package/llms.txt CHANGED
@@ -27,7 +27,7 @@
27
27
  - HTML & build integration:
28
28
  - The link rewrite reads attributes: only `href` (single or double quotes) on an `<a>` is changed, never `data-href`, and a `>` inside another attribute value does not hide it.
29
29
  - Sets `<html lang={locale}>` and `hreflang` links in `head()`.
30
- - Prefixes internal `<a href="/...">` for non-default locales (`data-no-localize` opts a link out, e.g. language switchers; skips `/_pru/`, `/_i18n/`, already-prefixed paths).
30
+ - Prefixes internal `<a href="/...">` and `<form action="/...">` for non-default locales, never inside `<script>`, `<style>`, comments, `<pre>`, `<code>` or `<textarea>` (`data-no-localize` opts a link out, e.g. language switchers; skips `/_pru/`, `/_i18n/`, already-prefixed paths).
31
31
  - Expands `prerenderPaths` across all configured locales and shares `I18N_CONFIG_SERVICE` with `@prunellejs/seo` for multilingual `sitemap.xml`.
32
32
  - Checks & CLI: `prunelle check` fails on translation keys missing vs `defaultLocale`; `prunelle i18n-check` prints missing keys.
33
33
 
@@ -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.3",
3
+ "version": "0.8.0",
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.3"
24
+ "prunelle": "^0.8.0"
25
25
  }
26
26
  }
package/src/index.ts CHANGED
@@ -28,12 +28,13 @@ export interface I18nOptions {
28
28
  site?: string | undefined
29
29
  }
30
30
 
31
- const HTML_TOKEN_RE = /<(\/?)(pre|code|textarea)(?=[\s>/])[^<>]*>|<a(?=[\s>/])((?:[^<>"']|"[^"]*"|'[^']*')*)>/gi
31
+ // One pass over the page: comments and whole <script>/<style> elements are matched to be skipped, <pre>/<code>/<textarea> tags track depth, <a> and <form> start tags are rewritten.
32
+ const HTML_TOKEN_RE = /<!--[\s\S]*?-->|<(script|style)(?=[\s>/])[^<>]*>[\s\S]*?<\/\1\s*>|<(\/?)(pre|code|textarea)(?=[\s>/])[^<>]*>|<(a|form)(?=[\s>/])((?:[^<>"']|"[^"]*"|'[^']*')*)>/gi
32
33
  // One attribute of a start tag: optional leading separator, name, and optional `= value` (quoted or unquoted).
33
34
  const ATTR_RE = /([\s/]*)([^\s"'<>/=]+)(?:(\s*=\s*)(?:"([^"]*)"|'([^']*)'|([^\s<>]+)))?/g
34
35
  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
35
36
 
36
- function rewriteAnchorAttrs(attrs: string, localizeHref: (href: string) => string | undefined): string {
37
+ function rewriteLinkAttrs(attrs: string, linkAttr: string, localizeHref: (href: string) => string | undefined): string {
37
38
  ATTR_RE.lastIndex = 0
38
39
  let seenHref = false
39
40
  let hrefReplacement: { start: number, end: number, text: string } | undefined
@@ -42,7 +43,7 @@ function rewriteAnchorAttrs(attrs: string, localizeHref: (href: string) => strin
42
43
  const name = match[2]!.toLowerCase()
43
44
  if (name === 'data-no-localize')
44
45
  return attrs
45
- if (name === 'href' && !seenHref) {
46
+ if (name === linkAttr && !seenHref) {
46
47
  seenHref = true
47
48
  const double = match[4]
48
49
  const single = match[5]
@@ -91,17 +92,20 @@ function localizeHtmlLinks(
91
92
  return undefined
92
93
  const cut = href.search(/[?#]/)
93
94
  const pathOnly = cut === -1 ? href : href.slice(0, cut)
95
+ const suffix = cut === -1 ? '' : href.slice(cut)
94
96
  if (isSkippedI18nPath(pathOnly, assetPrefix, assetPrefixSlash))
95
97
  return undefined
96
98
  const seg = pathOnly.slice(1).split('/', 1)[0]
97
99
  if (seg !== undefined && localeSet.has(seg))
98
100
  return undefined
99
101
  // `href` is already extracted from a quoted HTML attribute; only escape the prepended locale segment to avoid double-escaping `&amp;`.
100
- return href === '/' ? `/${escapedLocale}` : `/${escapedLocale}${href}`
102
+ return pathOnly === '/' ? `/${escapedLocale}${suffix}` : `/${escapedLocale}${href}`
101
103
  }
102
104
  return html.replace(
103
105
  HTML_TOKEN_RE,
104
- (match, slash: string | undefined, skipTag: string | undefined, attrs: string | undefined) => {
106
+ (match, rawTag: string | undefined, slash: string | undefined, skipTag: string | undefined, linkTag: string | undefined, attrs: string | undefined) => {
107
+ if (rawTag !== undefined || match.startsWith('<!--'))
108
+ return match
105
109
  if (skipTag !== undefined) {
106
110
  const tag = skipTag.toLowerCase()
107
111
  const isClosing = slash === '/'
@@ -123,10 +127,10 @@ function localizeHtmlLinks(
123
127
  }
124
128
  return match
125
129
  }
126
- if (inTextarea || codePreDepth > 0 || attrs === undefined)
130
+ if (inTextarea || codePreDepth > 0 || attrs === undefined || linkTag === undefined)
127
131
  return match
128
- const rewritten = rewriteAnchorAttrs(attrs, localizeHref)
129
- return rewritten === attrs ? match : `<a${rewritten}>`
132
+ const rewritten = rewriteLinkAttrs(attrs, linkTag.toLowerCase() === 'form' ? 'action' : 'href', localizeHref)
133
+ return rewritten === attrs ? match : `<${linkTag}${rewritten}>`
130
134
  },
131
135
  )
132
136
  }
@@ -138,15 +142,22 @@ function collapseLeadingSlashes(p: string): string {
138
142
  return collapsed.startsWith('/') ? collapsed : `/${collapsed}`
139
143
  }
140
144
 
145
+ function splitPathAndSuffix(input: string): { path: string, suffix: string } {
146
+ const cut = input.search(/[?#]/)
147
+ return cut === -1 ? { path: input, suffix: '' } : { path: input.slice(0, cut), suffix: input.slice(cut) }
148
+ }
149
+
141
150
  export function stripLocalePrefix(pathname: string, locales: readonly string[]): { locale: string | undefined, path: string } {
142
- const normalized = collapseLeadingSlashes(pathname)
151
+ const { path: rawPath, suffix } = splitPathAndSuffix(pathname)
152
+ const normalized = collapseLeadingSlashes(rawPath)
143
153
  const parts = normalized.split('/')
144
154
  const first = parts[1]
145
155
  if (first !== undefined && locales.includes(first)) {
146
156
  const rest = collapseLeadingSlashes(`/${parts.slice(2).join('/')}`)
147
- return { locale: first, path: rest === '/' ? '/' : rest.replace(/\/+$/, '') || '/' }
157
+ const cleanRest = rest === '/' ? '/' : rest.replace(/\/+$/, '') || '/'
158
+ return { locale: first, path: `${cleanRest}${suffix}` }
148
159
  }
149
- return { locale: undefined, path: normalized }
160
+ return { locale: undefined, path: `${normalized}${suffix}` }
150
161
  }
151
162
 
152
163
  export function localizePath(
@@ -157,10 +168,11 @@ export function localizePath(
157
168
  prefixDefault = false,
158
169
  ): string {
159
170
  const { path } = stripLocalePrefix(pathname, locales)
160
- const safePath = collapseLeadingSlashes(path)
171
+ const { path: rawPath, suffix } = splitPathAndSuffix(path)
172
+ const safePath = collapseLeadingSlashes(rawPath)
161
173
  if (targetLocale === defaultLocale && !prefixDefault)
162
- return safePath
163
- return safePath === '/' ? `/${targetLocale}` : `/${targetLocale}${safePath}`
174
+ return `${safePath}${suffix}`
175
+ return safePath === '/' ? `/${targetLocale}${suffix}` : `/${targetLocale}${safePath}${suffix}`
164
176
  }
165
177
 
166
178
  export function alternateLinks(
@@ -174,13 +186,17 @@ export function alternateLinks(
174
186
  }))
175
187
  }
176
188
 
189
+ const ISO_DATE_ONLY_RE = /^\d{4}(?:-\d{2}(?:-\d{2})?)?$/
190
+
177
191
  export function formatDate(
178
192
  value: Date | string | number,
179
193
  locale: string,
180
194
  options: Intl.DateTimeFormatOptions = { dateStyle: 'long' },
181
195
  ): string {
182
- const d = value instanceof Date ? value : new Date(value)
183
- return new Intl.DateTimeFormat(locale, options).format(d)
196
+ const isDateOnly = typeof value === 'string' && ISO_DATE_ONLY_RE.test(value.trim())
197
+ const d = value instanceof Date ? value : new Date(isDateOnly ? value.trim() : value)
198
+ const fmtOptions = isDateOnly && options.timeZone === undefined ? { ...options, timeZone: 'UTC' } : options
199
+ return new Intl.DateTimeFormat(locale, fmtOptions).format(d)
184
200
  }
185
201
 
186
202
  export function formatNumber(