Update the output of CLI to make it possible to pre-translate resources when loading.
User story 1:
As an msg-cli user, I want the msg library updated to the latest version, so that I can use the new language utilities and the updated addNote interfaces.
User story 2:
As a msg-cli user who uses ESM modules, I want the create resource <project> <title> command to create a <title>.msg.js file in the i18n/resources folder, with ESM boilerplate code so that I can easily create a new MsgResource that is pre-translated for the current locale and import into my code.
EXAMPLE:
If an EMS user inputs create resource Main Messages it should create create a Messages.msg.js file in the i18n/resources directory with the following code, assuming the project's locales.sourceLocale is en and the dir is therefore left to right. The actual implementation should use the passed in <title> and the actual project.locales.sourceLocale.
/** ESM module **/
import { MsgResource, getLang } from '@worldware/msg';
import project from '#i18n/projects/Main';
/** Create a MsgResource object */
export const resource = MsgResource.create({
title: 'Messages',
attributes: {
lang: 'en',
dir: 'ltr'
},
notes: [
{type: 'DESCRIPTION', content: 'This is the Messages resource.'}
]
}, project);
/**
* Add messages to the resource using add(key, value, attributes, notes)
* The add method is chainable.
*/
resource
.add('sampleKey', 'Sample value.', {}, [
{ type: 'DESCRIPTION', content: 'This is first message.' }
])
.add('sampleKey2', 'Hi, {name}', { dnt: true }, [
{ type: 'DESCRIPTION', content: 'This is the second message.' },
{ type: 'PARAMETERS', content: 'The {name} parameter holds the user name.' }
]);
/**
* An async function to get a translated version of the resource
* If the runtime language has not been set using `setLang()`,
* it will return the original resource
*/
export async function getMessages() {
return await resource.getTranslation(getLang());
}
USAGE
// import the async getMessages function from the resource file
import { getMessages } from '#i18n/resources/Messages.msg.js';
// You can use module level await to get a translated resource
const messages = await getMessages();
// or use it inside another async function
async function init() {
const messages = await getMessages();
}
User story 3:
As a msg-cli user who uses CJS modules, I want the create resource <project> <title> command to create a <title>.msg.js file in the i18n/resources folder, with CJS boilerplate code so that I can easily create a new MsgResource that is pre-translated for the current locale and import into my code.
EXAMPLE:
If a CJS user inputs create resource Main Messages it should create create a Messages.msg.js file in the i18n/resources directory with the following code, assuming the project's locales.sourceLocale is en and the dir is therefore left to right. The actual implementation should use the passed in <title> and the actual project.locales.sourceLocale.
/** CJS implementation **/
const { MsgResource, getLang } = require('@worldware/msg');
const project = require('#i18n/projects/Main');
const resource = MsgResource.create({
title: 'Messages',
attributes: {
lang: 'en',
dir: 'ltr'
},
notes: [
{type: 'DESCRIPTION', content: 'This is the Messages resource.'}
]
}, project);
/**
* Add messages to the resource using add(key, value, attributes, notes)
* The add method is chainable.
*/
resource
.add('sampleKey', 'Sample value.', {}, [
{ type: 'DESCRIPTION', content: 'This is first message.' }
])
.add('sampleKey2', 'Hi, {name}', { dnt: true }, [
{ type: 'DESCRIPTION', content: 'This is the second message.' },
{ type: 'PARAMETERS', content: 'The {name} parameter holds the user name.' }
]);
/**
* An async function to get a translated version of the resource
* If a runtime language has not been set using `setLang()`,
* it will return the original resource
*/
async function getMessages() {
return await resource.getTranslation(getLang());
}
module.exports = {
resource,
getMessages
}
USAGE:
// import the async function
const { getMessages } = require('#i18n/resources/Messages.msg.js');
// Call the getMessages and then use the then method on the Promise
getMessages().then(messages => {
// use messages here
});
Update the output of CLI to make it possible to pre-translate resources when loading.
User story 1:
As an msg-cli user, I want the
msglibrary updated to the latest version, so that I can use the new language utilities and the updated addNote interfaces.User story 2:
As a msg-cli user who uses ESM modules, I want the
create resource <project> <title>command to create a<title>.msg.jsfile in thei18n/resourcesfolder, with ESM boilerplate code so that I can easily create a new MsgResource that is pre-translated for the current locale and import into my code.EXAMPLE:
If an EMS user inputs
create resource Main Messagesit should create create aMessages.msg.jsfile in thei18n/resourcesdirectory with the following code, assuming the project'slocales.sourceLocaleisenand thediris therefore left to right. The actual implementation should use the passed in<title>and the actualproject.locales.sourceLocale.USAGE
User story 3:
As a msg-cli user who uses CJS modules, I want the
create resource <project> <title>command to create a<title>.msg.jsfile in thei18n/resourcesfolder, with CJS boilerplate code so that I can easily create a new MsgResource that is pre-translated for the current locale and import into my code.EXAMPLE:
If a CJS user inputs
create resource Main Messagesit should create create aMessages.msg.jsfile in thei18n/resourcesdirectory with the following code, assuming the project'slocales.sourceLocaleisenand thediris therefore left to right. The actual implementation should use the passed in<title>and the actualproject.locales.sourceLocale.USAGE: