-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the message-dictionary wiki!
const message = require('message-dictionary');
// set configuration
var config = {
dirPath: require('path').join('./locales'),
namespace: 'app'
}
message.init(config).load();
// Get list
message.list();
// Get message
message.get('CODE','LOCALE');
// Get message all language
message.getAll('CODE');
If this is your first time of using message-dictionary, then you have to create message data first
// Add message
message.addMessage('XX01','en','This is a new message','',function(err, data) {
if(err) return console.log(err);
console.log(data);
});
// Update message
message.updateMessage('XX01','en','This is a updated message','',function(err, data) {
if(err) return console.log(err);
console.log(data);
});
// Delete message locale (only delete the specified locale)
message.deleteMessageLocale('XX01','en',function(err, data) {
if(err) return console.log(err);
console.log(data);
});
// Delete message
message.deleteMessage('XX01',function(err, data) {
if(err) return console.log(err);
console.log(data);
});
// Drop all message
message.drop(function(err) {
if(err) return console.log(err);
})
Example:
// Default config
var config = {
dirPath: '',
namespace: 'app'
}
-
dirPath
is the location file of message-dictionary. This isrequired
. -
namespace
is like a category or scope message to be used in your application. Default isapp
.
-
message.init() - Load data from file into table
(Synchronous)
- message.reload(callback) - Reload data from file to table
- message.add(code,message,extend,callback) - Add new message
- message.update(code,message,extend,callback) - Update message
- message.delete(code,callback) - Delete message
- message.drop(callback) - Drop all message
-
message.list() - Get list message
(Synchronous)
-
message.get(code) - Get message by code
(Synchronous)
This will load data from file into virtual table. This is work as synchronous, so if you have only one language, it's better just call this once on top of your script.
Example:
var message = MessageDictionary().init();
//...
This will reload data from file into virtual table. Actualy reload is already running automatically when the new data is added, updated or deleted. You are able to run this manually to make sure virtual table has been updated.
Example:
var message = MessageDictionary().init();
// you add new message
message.add('XX01','This is a new message','',function(err, data) {
if(err) return console.log(err);
console.log(data);
});
// Actualy the message.list() is already reload automatically
// To make sure you can call
message.reload(function(err) {
if(err) return console.log(err);
})
Add new message.
-
code
{string} : ID of your message. -
message
{string} : Your message. -
extend
{object} : [optional] Add more data into message. -
callback
: callback error and status.
Example:
message.add('XX012','This is your message',{user:'john doe'}, function(err, data) {
if(err) return console.log(err);
console.log(data);
});
Update message.
-
code
{string} : ID of your message. -
message
{string} : Your message. -
extend
{object} : [optional] Add more data into message. -
callback
: callback error and status.
Example:
message.update('XX012','This is your updated message',{modified_by:'admin'}, function(err, data) {
if(err) return console.log(err);
console.log(data);
});
Delete message.
-
code
{string} : ID of your message. -
callback
: callback error and status.
Example:
message.delete('XX012', function(err, data) {
if(err) return console.log(err);
console.log(data);
});
This will delete all message in current locale and namespace only.
-
callback
- [Optional] callback error.
Example:
message.drop(function(err) {
if(err) return console.log(err)
})
Get list message in current locale and namespace only.
Example:
message.list();
Get message by code
-
code
- ID of message.
Example:
message.get('XX012');
- isCallable(fn) - Determine if parameter is callable function
- getFilename() - Get full path of file message
- writeStream(file,opt,data,callback) - Write file in stream
- readStream(file,callback) - Read file in stream
- promisify(fn) - Make asynchronous process with Promise
Determine if parameter is callable function or not.
-
fn
: function
Example:
message.isCallable(callback)
// return boolean
Get full path of file message.
Example:
message.getFilename();
// default will return
// /home/your/path/to/app/locales/app/en.js
Write file in stream way.
-
file
{string} : File path. -
opt
{object} : Flag write stream. Default is{ flag:'w' }
. -
data
{string} : Content to be write in file. -
callback
: callback (error, status)
Example:
message.writeSream('./file/to/the/path.js',{flag:'w'},'your content', function(err, data) {
if(err) return console.log(err);
console.log(data);
});
Read file in stream way.
-
file
{string} : File path. -
callback
: callback (error, content)
Example:
message.readSream('./file/to/the/path.js', function(err, content) {
if(err) return console.log(err);
console.log(content);
});
Make asynchoronous process with promise.
-
fn
: Function
Example:
message.promisify(builder => {return builder}).then(msg => {
msg.init();
console.log(msg.get('XX012'));
});
- This library is using static file to store the data message. Currently not support to be use with any database engine.