Skip to content
M ABD AZIZ ALFIAN edited this page Oct 23, 2019 · 34 revisions

Welcome to the message-dictionary wiki!

Table of Contents

Get Started

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);
})

Configuration

Example:

// Default config
var config = {
    dirPath: '',
    namespace: 'app'
}
  • dirPath is the location file of message-dictionary. This is required.
  • namespace is like a category or scope message to be used in your application. Default is app.

Method

message.init()

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();
//...

message.reload(callback)

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);
})

message.add(code,message,extend,callback)

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);
});

message.update(code,message,extend,callback)

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);
});

message.delete(code,callback)

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);
});

message.drop(callback)

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)
})

message.list()

Get list message in current locale and namespace only.
Example:

message.list();

message.get(code)

Get message by code

  • code - ID of message.
    Example:
message.get('XX012');

Helper

isCallable(fn)

Determine if parameter is callable function or not.

  • fn : function
    Example:
message.isCallable(callback)
// return boolean

getFilename()

Get full path of file message.
Example:

message.getFilename();

// default will return
// /home/your/path/to/app/locales/app/en.js

writeStream(file,opt,data,callback)

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);
});

readStream(file,callback)

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);
});

promisify(fn)

Make asynchoronous process with promise.

  • fn : Function
    Example:
message.promisify(builder => {return builder}).then(msg => {
  msg.init();
  console.log(msg.get('XX012'));
});

Limitation

  • This library is using static file to store the data message. Currently not support to be use with any database engine.
Clone this wiki locally