2.2.3 • Published 2 years ago
@billogram/message-catalog v2.2.3
@billogram/message-catalog
Container and utilities for copy strings
Install
yarn add @billogram/message-catalog
Usage
import MessageCatalog from '@billogram/message-catalog';
const catalog = new MessageCatalog({
'Greeting': {
'sv': 'Hallå',
'en': 'Hello',
},
'PersonalGreeting[%name%]': {
'sv': 'Hej %name%!',
'en': 'Hi %name%!',
},
})
// Fetches the copy string for the Greeting key, using the current locale.
// Renders markdown as React components.
catalog.get('Greeting') === 'Hello';
// With interpolation
catalog.get('PersonalGreeting[%name%]', { name: 'James' }) === 'Hi James!';
// Fetches the copy string for the Greeting key, using the current locale.
// Returns a raw string. Only use if renderer does not support React.ReactNode.
catalog.raw('Greeting') === 'Hello';
// With interpolation
catalog.raw('PersonalGreeting[%name%]', { name: 'James' }) === 'Hi James!';
// Checks if the Greeting key exists in the catalog
catalog.includes('Greeting') === true;
Plural support
const catalog = new MessageCatalog({
'Apples': {
'sv': [
'Ett äpple',
'%count% äpplen',
],
'en': [
'An apple',
'%count% apples',
],
},
});
// nget/nraw automatically includes the count param as an interpolatable value.
catalog.nget('Apples', 1) === 'An apple';
catalog.nraw('Apples', 1) === 'An apple';
catalog.nget('Apples', 6) === '6 apples';
catalog.nraw('Apples', 6) === '6 apples';
API
class MessageCatalog {
constructor(messages: Messages) {}
get(key: string, params?: Params<React.ReactNode>): React.ReactNode {}
nget(key: string, count: number, params?: Params<React.ReactNode>): React.ReactNode {}
raw(key: string, params?: Params<string>): string {}
nraw(key: string, count: number, params?: Params<string>): string {}
includes(key: string): boolean {}
}
type Message = {
[key: string]: string;
};
type Messages = {
[key: string]: Message;
};
type Params<T> = {
[key: string]: T;
};
Note
This library is being published with our use cases in mind and is not necessarily meant to be consumed by the broader public. We probably won't take your feature requests unless they align with our own needs.
License
MIT