docs/api/i18n.md
You can customize the initial state of the module from the editor initialization, by passing the following Configuration Object
const editor = grapesjs.init({
i18n: {
locale: 'en',
localeFallback: 'en',
messages: {
it: { hello: 'Ciao', ... },
...
}
}
})
Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance
const i18n = editor.I18n;
i18n:add New set of messages is added.editor.on('i18n:add', (messages) => { ... });
i18n:update The set of messages is updated.editor.on('i18n:update', (messages) => { ... });
i18n:locale Locale changed.editor.on('i18n:locale', ({ value, valuePrev }) => { ... });
Update current locale
locale String Locale valuei18n.setLocale('it');
Returns this
Get current locale
Returns String Current locale value
Get all messages
lang String? Specify the language of messages to return
opts Object? Options (optional, default {})
opts.debug Boolean? Show warnings in case of missing languagei18n.getMessages();
// -> { en: { hello: '...' }, ... }
i18n.getMessages('en');
// -> { hello: '...' }
Returns Object
Set new set of messages
msg Object Set of messagesi18n.getMessages();
// -> { en: { msg1: 'Msg 1', msg2: 'Msg 2', } }
i18n.setMessages({ en: { msg2: 'Msg 2 up', msg3: 'Msg 3', } });
// Set replaced
i18n.getMessages();
// -> { en: { msg2: 'Msg 2 up', msg3: 'Msg 3', } }
Returns this
Update messages
msg Object Set of messages to addi18n.getMessages();
// -> { en: { msg1: 'Msg 1', msg2: 'Msg 2', } }
i18n.addMessages({ en: { msg2: 'Msg 2 up', msg3: 'Msg 3', } });
// Set updated
i18n.getMessages();
// -> { en: { msg1: 'Msg 1', msg2: 'Msg 2 up', msg3: 'Msg 3', } }
Returns this
Translate the locale message
obj.setMessages({
en: { msg: 'Msg', msg2: 'Msg {test}'},
it: { msg2: 'Msg {test} it'},
});
obj.t('msg');
// -> outputs `Msg`
obj.t('msg2', { params: { test: 'hello' } }); // use params
// -> outputs `Msg hello`
obj.t('msg2', { l: 'it', params: { test: 'hello' } }); // custom local
// -> outputs `Msg hello it`
Returns String
Get configuration object
Returns Object