Registry / i18n / sprintf-formatter

sprintf-formatter

JSON →
library2.0.1jsnpmunverified

sprintf-formatter v2.0.1 provides a formatter for vue-i18n that uses sprintf syntax, enabling printf-style string formatting in translations. It works with Vue 2 and vue-i18n v8.x, ships TypeScript types, and offers both ESM and UMD builds. Compared to vue-i18n's built-in formatter, it allows positional and named placeholders with sprintf formatting options. The package is stable but relies on an optional dependency on sprintf-js, which is bundled as part of vue-i18n in some setups. Since version 2, it only supports Vue 2 and vue-i18n v8, and does not support Vue 3.

npm install sprintf-formatter
INSTALL
IMPORT
SIG · SPRINTF-FORMATTER
S
sprintf-formatter
i18njavascriptv2.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SprintfFormatter
import SprintfFormatter from 'sprintf-formatter'
const SprintfFormatter = require('sprintf-formatter')
Default export; CommonJS require may still work depending on bundler.
vue-i18n
import VueI18n from 'vue-i18n'
ESM import for vue-i18n; ensure it is v8.x for compatibility.
new SprintfFormatter()
const i18n = new VueI18n({ formatter: new SprintfFormatter() })
new VueI18n().formatter = new SprintfFormatter()
Pass formatter in constructor options, not as a property assignment.

Shows how to install and configure SprintfFormatter with vue-i18n in a Vue 2 app, demonstrating sprintf-style placeholders.

import Vue from 'vue'; import VueI18n from 'vue-i18n'; import SprintfFormatter from 'sprintf-formatter'; Vue.use(VueI18n); const i18n = new VueI18n({ locale: 'en', messages: { en: { greeting: 'Hello, %s!', items: 'You have %d items.' } }, formatter: new SprintfFormatter() }); new Vue({ i18n, render: h => h('div', [ h('p', i18n.t('greeting', ['World'])), h('p', i18n.t('items', [42])) ]) }).$mount('#app');
Debug
Known issues
breakingVersion 2.x drops support for Vue 3. If you are using Vue 3 with vue-i18n v9, this package is incompatible.
fix
Use the built-in MessageFormat or implement a custom formatter for vue-i18n v9.
affects: >=2.0.0
gotchaWhen using Nuxt.js with nuxt-i18n, the formatter must be instantiated in a plugin file and passed via the vueI18n option as shown in the README. Directly setting formatter in nuxt-i18n config may not work.
fix
Create a plugin file exporting a function that returns an object with the formatter property.
affects: >=1.0.0
deprecatedThe package still lists sprintf-js as a dependency, but it may not be needed if vue-i18n bundles its own sprintf function. Installation may be unnecessary or cause duplication.
fix
Ensure only one version of sprintf is used; consider removing explicit sprintf-js dependency.
affects: >=2.0.0
gotchaThe formatter does not support named placeholders (e.g., %(name)s) natively. It only supports sprintf positional placeholders like %s, %d, etc.
fix
Use positional parameters in the translation string and pass values as an array.
affects: >0.0.0
Errors
Common errors & fixes
Missing formatter: 'formatter'
The formatter option is not passed to VueI18n constructor or vue-i18n config.
fix
import SprintfFormatter from 'sprintf-formatter'; new VueI18n({ formatter: new SprintfFormatter() })
TypeError: formatter.format is not a function
Using a custom object without the required format method.
fix
Use SprintfFormatter instance, which implements the format method required by vue-i18n.
Cannot read property 'i18n' of undefined
VueI18n plugin not installed with Vue.use(VueI18n) before creating instance.
fix
Add Vue.use(VueI18n) before new VueI18n().
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
vuerequiredPeer dependency: works with Vue 2; incompatible with Vue 3.
vue-i18nrequiredPeer dependency: formatter integrated with vue-i18n v8; does not support v9.
sprintf-jsoptionalOptional dependency: provides sprintf functionality; may be bundled with vue-i18n or installed separately.
Agent activity
4 hits · last 30 days
node
4
Resources
sprintf-formatter — npm install sprintf-formatter · libregistry