-
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(config) - Set configuration
-
message.load() - Load data from file into memory
(Synchronous)
- message.reload(callback) - Reload data from file to memory
- message.addMessage(code,message,extend,callback) - Add new message
- message.updateMessage(code,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. 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 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.
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) {
if(err) return console.log(err);
})
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
: callback error and 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
: callback error and 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
: callback error and 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
: callback error and 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
- [Optional] callback error.
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
Determine if parameter is callable function or not.
-
fn
: function
Example:
message._isCallable(callback)
// return boolean
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._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
: callback (error, content)
Example:
message._readStream('./file/to/the/path.js', function(err, content) {
if(err) return console.log(err);
console.log(content);
});
- 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/en.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 using static file to store the data message. Currently not support to be use with any database engine.