Comparing version 0.0.1 to 0.1.0
@@ -14,29 +14,3 @@ var vsprintf = require('sprintf-js').vsprintf; | ||
i18n.prototype.load = function(lang, data){ | ||
this.data[lang] = flattenObject(data); | ||
}; | ||
function flattenObject(data, obj, parent){ | ||
obj = obj || {}; | ||
parent = parent || ''; | ||
var keys = Object.keys(data); | ||
var key = ''; | ||
var item; | ||
for (var i = 0, len = keys.length; i < len; i++){ | ||
key = keys[i]; | ||
item = data[key]; | ||
if (typeof item === 'object'){ | ||
flattenObject(item, obj, parent + key + '.'); | ||
} else { | ||
obj[parent + key] = item; | ||
} | ||
} | ||
return obj; | ||
} | ||
i18n.prototype._compile = function(languages){ | ||
i18n.prototype.get = function(languages){ | ||
var data = this.data; | ||
@@ -70,4 +44,39 @@ var result = {}; | ||
i18n.prototype.set = function(lang, data){ | ||
if (typeof lang !== 'string') throw new TypeError('lang must be a string!'); | ||
if (typeof data !== 'object') throw new TypeError('data is required!'); | ||
this.data[lang] = flattenObject(data); | ||
}; | ||
i18n.prototype.remove = function(lang){ | ||
if (typeof lang !== 'string') throw new TypeError('lang must be a string!'); | ||
this.data[lang] = null; | ||
}; | ||
function flattenObject(data, obj, parent){ | ||
obj = obj || {}; | ||
parent = parent || ''; | ||
var keys = Object.keys(data); | ||
var key = ''; | ||
var item; | ||
for (var i = 0, len = keys.length; i < len; i++){ | ||
key = keys[i]; | ||
item = data[key]; | ||
if (typeof item === 'object'){ | ||
flattenObject(item, obj, parent + key + '.'); | ||
} else { | ||
obj[parent + key] = item; | ||
} | ||
} | ||
return obj; | ||
} | ||
i18n.prototype.__ = function(lang){ | ||
var data = this._compile(lang); | ||
var data = this.get(lang); | ||
@@ -92,3 +101,3 @@ return function(){ | ||
i18n.prototype._p = function(lang){ | ||
var data = this._compile(lang); | ||
var data = this.get(lang); | ||
@@ -95,0 +104,0 @@ return function(){ |
{ | ||
"name": "hexo-i18n", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "i18n module for Hexo.", | ||
@@ -5,0 +5,0 @@ "main": "lib/i18n", |
@@ -15,6 +15,79 @@ # i18n | ||
### Example | ||
``` js | ||
var i18n = require('hexo-i18n'); | ||
var i18n = new require('hexo-i18n')({ | ||
languages: ['zh-TW', 'en'] | ||
}); | ||
i18n.set('en', { | ||
ok: 'OK', | ||
name: 'My name is %1$s %2$s.', | ||
index: { | ||
title: 'Home' | ||
}, | ||
video: { | ||
zero: 'No videos', | ||
one: 'A video', | ||
other: '%d videos' | ||
} | ||
}); | ||
i18n.set('zh-TW', { | ||
name: '我的名字是 %2$s %1$s。', | ||
index: { | ||
title: '首頁' | ||
}, | ||
video: { | ||
zero: '沒有影片', | ||
one: '一部影片', | ||
other: '%d 部影片' | ||
} | ||
}); | ||
var __ = i18n.__(); | ||
var _p = i18n._p(); | ||
__('ok') // OK | ||
__('index.title') // 首頁 | ||
__('name', '大呆', '王') // 我的名字是王大呆 | ||
_p('video', 0) // 沒有影片 | ||
_p('video', 1) // 一部影片 | ||
_p('video', 10) // 10 部影片 | ||
``` | ||
### new i18n([options]) | ||
Creates a new i18n instance. | ||
Option | Description | Default | ||
--- | --- | --- | ||
`languages` | Default languages. It can be an array or a string | `default` | ||
### i18n.get([lang]) → Object | ||
Returns a set of localization data. `lang` can be an array or a string, or the default language defined in constructor if not set. This method will build the data in order of languages. | ||
### i18n.set(lang, data) | ||
Loads localization data. | ||
### i18n.remove(lang) | ||
Unloads localization data. | ||
### i18n.__() → Function(key, arg...) | ||
Returns a function for localization. | ||
### i18n._p() → Function(key, count, ...) | ||
This method is similar to `i18n.__`, but it returns pluralized string based on the second parameter. For example: | ||
``` js | ||
_p('video', 0) = __('video.zero', 0) | ||
_p('video', 1) = __('video.one', 1) | ||
_p('video', 10) = __('video.other', 10) | ||
``` | ||
## License | ||
@@ -21,0 +94,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6737
96
96