-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the message-dictionary wiki!
This documentation is for new version 2.x.x
and has no backwards compatible with 1.x.x
because we are not longer to maintain version 1.x.x
anymore.
const message = require('message-dictionary');
// set configuration
var config = {
dirPath: require('path').join('./locales'),
namespace: 'app'
}
// Load Synchronous
message.init(config).load();
// Load Asynchronous
message.init(config).reload();
// 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(config) - Set configuration
-
message.load() - Load data from file into memory
(Synchronous)
- message.reload(callback) - Reload data from file to memory
- message.addMessage(code,locale,message,extend,callback) - Add new message
- message.updateMessage(code,locale,message,extend,callback) - Update message
- message.deleteMessageLocale(code,locale,callback) - Delete message locale (only delete the specified locale)
- message.deleteMessage(code,callback) - Delete message
- message.drop(callback) - Drop all message
-
message.list() - Get list message
(Synchronous)
-
message.getAll(code) - Get message by code
(Synchronous)
-
message.get(code,locale) - Get message by code and locale
(Synchronous)
This will load data from file into virtual table.
Example:
var config = {
dirPath: require('path').join('./locales'),
namespace: 'app'
};
message.init(config);
//...
This will load data from file into memory / virtual table. This is work as synchronous.
Example:
message.init(config).load();
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.
-
callback(error, data)
: callback error and data status. This isoptional
.
Example:
// you add new message
message.addMessage('XX01','en','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, data) {
if(err) return console.log(err);
console.log(data);
})
Add new message.
-
code
{string} : ID of your message. -
locale
{string} : Two letters of language code. -
message
{string} : Your message. -
extend
{object} : [optional] Add more data into message. -
callback(error, data)
: callback error and data status.
Example:
message.addMessage('XX012','en','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. -
locale
{string} : Two letters of language code. -
message
{string} : Your message. -
extend
{object} : [optional] Add more data into message. -
callback(error, data)
: callback error and data status.
Example:
message.update('XX012','en','This is your updated message',{modified_by:'admin'}, function(err, data) {
if(err) return console.log(err);
console.log(data);
});
Delete message only the specified locale.
-
code
{string} : ID of your message. -
locale
{string} : Two letters of language code. -
callback(error, data)
: callback error and data status.
Example:
message.deleteMessageLocale('XX012','en', function(err, data) {
if(err) return console.log(err);
console.log(data);
});
Delete message.
-
code
{string} : ID of your message. -
callback(error, data)
: callback error and data status.
Example:
message.deleteMessage('XX012', function(err, data) {
if(err) return console.log(err);
console.log(data);
});
This will delete all message in current namespace only.
-
callback(error)
: callback return an info error only. This isoptional
.
Example:
message.drop(function(err) {
if(err) return console.log(err)
})
Get list message in current namespace only.
Example:
message.list();
Get all message by code
-
code
- ID of message.
Example:
message.getAll('XX012');
Get message by code
-
code
- ID of message. -
locale
{string} : Two letters of language code.
Example:
message.get('XX012','en');
This private method is not trully private, you still able to use this outside class.
- _isCallable(fn) - Determine if parameter is callable function.
- _writeStream(file,opt,data,callback) - Write file in stream.
- _readStream(file,callback) - Read file in stream.
- _setTable(data) - Replace current virtual table directly.
Determine if parameter is callable function or not.
-
fn
: function callback
Example:
message._isCallable(fn)
// return boolean
// true or false
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(error, data)
: callback error and data status.
Example:
message._writeStream('./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(error, content)
: callback error and data content.
Example:
message._readStream('./file/to/the/path.js', function(err, content) {
if(err) return console.log(err);
console.log(content);
});
Replace current virtual table directly.
-
data
{array} : Data is an array which is contains object.
Example:
message._setTable([
{
code: 'XXX',
message: {
en: 'Your new message.'
}
}
]);
Note:
- Please use this in development mode.
- getFilename() - Get full path of file message
- getDirPath() - Get current directory path of file message
- getNamespace() - Get namespace of message
Get full path of file message.
Example:
message.getFilename();
// default will return
// /home/your/path/to/app/locales/app.js
Get current directory path of file message.
Example:
message.getDirPath();
// return
// /home/your/path/to/app/locales
Get namespace of message.
Example:
message.getNamespace();
// default will return
// "app"
- This library is work as singleton, so be wisely to use this.