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(config)

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

message.load()

This will load data from file into memory / virtual table. This is work as synchronous.
Example:

message.init(config).load();
//...

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:

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

message.addMessage(code,locale,message,extend,callback)

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

message.updateMessage(code,locale,message,extend,callback)

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

message.deleteMessageLocale(code,locale,callback)

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

message.deleteMessage(code,callback)

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

message.drop(callback)

This will delete all message in current namespace only.

  • callback - [Optional] callback error.
    Example:
message.drop(function(err) {
  if(err) return console.log(err)
})

message.list()

Get list message in current namespace only.
Example:

message.list();

message.getAll(code)

Get all message by code

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

message.get(code,locale)

Get message by code

  • code - ID of message.
  • locale {string} : Two letters of language code.
    Example:
message.get('XX012','en');

Private Method

This private method is not trully private, you still able to use this outside class.

_isCallable(fn)

Determine if parameter is callable function or not.

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

_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._writeStream('./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._readStream('./file/to/the/path.js', function(err, content) {
  if(err) return console.log(err);
  console.log(content);
});

Helper

getFilename()

Get full path of file message.
Example:

message.getFilename();

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

getDirPath()

Get current directory path of file message.
Example:

message.getDirPath();

// return
// /home/your/path/to/app/locales

getNamespace()

Get namespace of message.
Example:

message.getNamespace();

// default will return
// "app"

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