Message parsers for sveltekit-i18n. These parsers handle the interpolation of dynamic values in your translations. While designed for @sveltekit-i18n/base, they can be used with any library β they don't require Svelte or SvelteKit.
The default parser with a simple, flexible syntax for placeholders and modifiers.
npm install @sveltekit-i18n/parser-defaultFeatures:
- Simple placeholder syntax:
{{name}} - Built-in modifiers:
number,date,currency,ago - Conditional rendering:
{{count; 1:item; default:items;}} - Comparison operators:
eq,ne,lt,gt,lte,gte - Custom modifiers support
- No external dependencies
Example:
{
"greeting": "Hello, {{name}}!",
"items": "You have {{count:number;}} {{count; 1:item; default:items;}}.",
"price": "Price: {{value:currency;}}",
"updated": "Updated {{date:ago;}}"
}ICU message format parser powered by intl-messageformat.
npm install @sveltekit-i18n/parser-icuFeatures:
- Industry-standard ICU message syntax
- Plural rules:
{count, plural, one {# item} other {# items}} - Select format:
{gender, select, male {He} female {She} other {They}} - Number formatting:
{price, number, ::currency/USD} - Date/time formatting:
{date, date, ::yyyyMMdd}
Example:
{
"greeting": "Hello, {name}!",
"items": "You have {count, plural, =0 {no items} one {# item} other {# items}}.",
"response": "{gender, select, male {He} female {She} other {They}} will respond shortly."
}- You want no external dependencies
- You need a lightweight solution
- You prefer simple, readable syntax
- You want to create custom modifiers easily
- Your translation needs are straightforward
- You need industry-standard ICU message format
- You're migrating from other i18n libraries that use ICU
- You need advanced plural rules for complex languages
- You want built-in number/date/time formatting options
- You're comfortable with ICU syntax
The main sveltekit-i18n package includes parser-default by default:
import i18n from 'sveltekit-i18n';
const config = {
// parser-default is already included
loaders: [/* ... */],
};Use any parser with the base package:
import i18n from '@sveltekit-i18n/base';
import parser from '@sveltekit-i18n/parser-default';
// or: import parser from '@sveltekit-i18n/parser-icu';
const config = {
parser: parser({
// parser-specific options
}),
loaders: [/* ... */],
};You can create your own parser to support any message syntax you need.
A parser is a function that returns an object with a parse method:
const customParser = (config = {}) => ({
parse: (value, params, locale, key) => {
// value: translation string from your JSON file
// params: array of parameters passed to $t()
// locale: current locale (e.g., 'en', 'cs')
// key: translation key (e.g., 'common.greeting')
// Return interpolated string
return value;
},
});const templateParser = () => ({
parse: (value, params) => {
const vars = params[0] || {};
return value.replace(/\${(\w+)}/g, (_, key) => vars[key] ?? key);
},
});
// Usage in translations:
// { "greeting": "Hello, ${name}!" }const mustacheParser = () => ({
parse: (value, params) => {
const vars = params[0] || {};
return value.replace(/\{\{(\w+)\}\}/g, (_, key) => vars[key] ?? '');
},
});
// Usage in translations:
// { "greeting": "Hello, {{name}}!" }const advancedParser = (config = {}) => ({
parse: (value, params, locale) => {
const vars = params[0] || {};
return value.replace(/\{(\w+)(?::(\w+))?\}/g, (match, key, modifier) => {
const val = vars[key];
if (modifier === 'upper') return String(val).toUpperCase();
if (modifier === 'lower') return String(val).toLowerCase();
if (modifier === 'number') return new Intl.NumberFormat(locale).format(val);
return val ?? key;
});
},
});
// Usage in translations:
// { "greeting": "Hello, {name:upper}!", "count": "{value:number}" }import i18n from '@sveltekit-i18n/base';
import customParser from './custom-parser';
const config = {
parser: customParser(),
loaders: [/* ... */],
};
export const { t } = new i18n(config);Each parser accepts its own configuration options. Check the specific parser documentation:
- π Complete Documentation Index β All guides and references
- π Getting Started β Quick tutorial
- π Best Practices β Production patterns
See working examples of different parsers:
Both official parsers include full TypeScript support:
import i18n from '@sveltekit-i18n/base';
import parser from '@sveltekit-i18n/parser-default';
import type { Config } from '@sveltekit-i18n/parser-default';
const config: Config = {
parser: parser({
// typed options
}),
loaders: [/* ... */],
};For general contribution guidelines, see the Contributing Guide.
For parser-specific contributions and issues, use this repository's issues.
MIT