0.1.2 • Published 10 years ago
resource-bundle v0.1.2
resource-bundle
resource-bundle is a locale-specific resource loader based on co.
NOTE:
Resource-bundle must be used in co enviroment,such as koa.
##Installation
$ npm install resource-bundle
Examples
'use strict';
var co = require("co");
var loader = require('resource-bundle');
co(function*(){
var message = yield loader("en_US" ,__dirname+"/resources/i18n","message");
var value = message.get('key');
var intro = message.get('intro' , 'jim',1); // intro should be 'my name is jim,in class 1'
var mk = message.get('mk' , [1,2,3]); // mk should be '123'
})();
message_en_US.js
like this
module.exports = {
key:"value",
intro:"my name is %s,in class %d",
mk:"%s%s%s"
}
API
loader
var loader = require('resource-bundle');
var bundle = loader(locale,dir,baseName);
loader will return ResourceBundle object ,if no resource matched ,it will return null
.
locale
- the locale of the resource. eg. "en_US",'zh_CN'.dir
- specify the root dir of the resource file. It is best to use absolute path.baseName
- the baseName of the resource.
Resource name should to be baseName_en_US.js
or baseName_en_US.json
or baseName_en.js
or baseName_en.json
or baseName.js or baseName.json format.
if no locale resource matched,it will use baseName.js or baseName.json by default.
ResourceBundle Methods
get(key,...)
Returns a corresponding format value. format method ref util.format.