Skip to content

Add formatParams to format placeholder values before t - #3

Merged
velis74 merged 1 commit into
mainfrom
format-params
Oct 7, 2026
Merged

velis74 merged 1 commit into
mainfrom
format-params

Conversation

@velis74

@velis74 velis74 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

formatParams(t, format) returns a translation function that passes each placeholder value through format before t substitutes it. vue-i18n's t substitutes a value as String(value), so numbers miss the locale's digits and separators and dates print as Date.prototype.toString. With formatParams(t, v => typeof v === 'number' ? n(v) : v instanceof Date ? d(v) : v) the application formats values once, for every library, and the package still does not depend on vue-i18n.

  • Values are read and formatted on every call: getters return their current value, and reactive state format reads (vue-i18n's locale through n/d) is tracked by the computed or render that reads the string.
  • named is not mutated; the result of t is returned unchanged, so a returned key still means no translation.
  • The English-default fallback in translate is unchanged and interpolates unformatted values.

Tests (real vue-i18n): Persian digits with n in fa, a date with d, an unchanged value, no mutation, a getter read per call, a locale switch fa → en inside a computed, reactivity of state read only by format (over a non-reactive t), the unformatted English fallback, and formatParams over an interpolate-based t with Intl.

readme: formatParams in the intro, the values named holds, new section Formatting values in the application part, two vue-i18n notes. changelog and version 0.3.0.

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

✅ Incremental coverage — 100%

████████████████████ 5 / 5 changed lines covered · threshold 70% · main → format-params

File Coverage Covered Uncovered lines
✅ src/translatable.ts 100% 5 / 5 —
Total 100% 5 / 5
6 files not graded (2 ignored by pattern, 4 not source files)
File Reason
changelog.md not a source file
package-lock.json not a source file
package.json not a source file
readme.md not a source file
src/index.ts ignored by **/index.ts
src/translatable.spec.ts ignored by *.spec.*

Coverage reports: coverage/coverage-final.json (istanbul, 1 file) — incremental-coverage-check v3.0.0

@velis74
velis74 merged commit 07d99e6 into main Oct 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant