-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
242 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
node_modules | ||
coverage | ||
exlib/sax.js | ||
exlib | ||
test/fixtures |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
var EOL = require('os').EOL, | ||
tanker = require('../tanker'); | ||
|
||
/** | ||
* Compiles code of i18n function for bem-bl localization system. | ||
* | ||
* Important: XML strings translates with tanker. | ||
* | ||
* @param {String} core — code that contains i18n core from bem-bl library. | ||
* @param {Object} keysets — a sets of keys and their values (translations) for a language to internationalization. | ||
* @param {String} lang — language of keysets. | ||
* @returns {String} | ||
*/ | ||
module.exports = function (core, keysets, lang) { | ||
var scopes = Object.keys(keysets).sort(); | ||
|
||
return [ | ||
'(function () {', | ||
'var __bem__ = {};', | ||
'(function (BEM) {', | ||
core, | ||
'}(__bem__));', | ||
'var i18n = __bem__.I18N;', | ||
// initialize i18n with keysets | ||
scopes.map(function (scope) { | ||
var keyset = keysets[scope]; | ||
|
||
return buildCodeWithDecl(keyset, scope, lang); | ||
}).join(EOL), | ||
'i18n.lang(\'' + lang + '\');', | ||
'return i18n;', | ||
'}())' | ||
].join(EOL); | ||
}; | ||
|
||
/** | ||
* Returns code with `i18n.decl` function that initializes an `i18n` instance with keyset. | ||
* | ||
* @param {Object} keyset — a set of keys and their values (translations) for a language to internationalization. | ||
* @param {String} scope — scope of keyset. | ||
* @param {String} lang — language of keyset. | ||
* @returns {String|*} | ||
* @private | ||
* @example | ||
* i18n.decl('scope', { | ||
* key: 'translation', | ||
* hello: function(params) { return "Hello " + params["who"] + "!" }' | ||
* }, { lang: 'en' }); | ||
*/ | ||
function buildCodeWithDecl(keyset, scope, lang) { | ||
var keys = Object.keys(keyset), | ||
lastIndex = keys.length - 1; | ||
|
||
return [ | ||
'i18n.decl(\'' + scope + '\', {', | ||
keys.map(function (key, i) { | ||
var rawValue = keyset[key], // value can be string with translation | ||
// or xml code for parameterization | ||
value = tanker.xmlToJs(rawValue), // string with translation | ||
// or code of function for parameterization | ||
endSymbol = i === lastIndex ? '' : ','; | ||
|
||
return JSON.stringify(key) + ':' + value + endSymbol; | ||
}).join(EOL), | ||
'}, { "lang": "' + lang + '" });' | ||
].join(EOL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var serialize = require('serialize-javascript'); | ||
|
||
/** | ||
* Compiles code of i18n function for bem-core localization system. | ||
* | ||
* @param {String} core — code that contains i18n core from bem-core library | ||
* @param {Object} keysets — a sets of keys and their values (translations) for a language to internationalization | ||
* | ||
* @returns {String} | ||
*/ | ||
module.exports = function (core, keysets) { | ||
return '((' + core + ')()).decl(' + serialize(keysets) + ')'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var compileForBemBL = require('./bem-bl'), | ||
compileForBemCore = require('./bem-core'); | ||
|
||
/** | ||
* Compiles code of i18n for `bem-core` or `bem-bl` localization system. | ||
* | ||
* Important: XML strings for `bem-bl` translates with `tanker`. `bem-core` not support XML strings. | ||
* | ||
* @param {String} core — code that contains i18n core from bem-core or bem-bl library. | ||
* @param {Object} keysets — a sets of keys and their values (translations) for a language to internationalization. | ||
* @param {Object} opts | ||
* @param {String} opts.version — version of core: `bem-bl` or `bem-core`. | ||
* @param {String} [opts.language] — language of keysets. | ||
* | ||
* @returns {String} | ||
*/ | ||
module.exports = function (core, keysets, opts) { | ||
if (opts.version === 'bem-core') { | ||
return compileForBemCore(core, keysets); | ||
} | ||
|
||
if (opts.version === 'bem-bl') { | ||
return compileForBemBL(core, keysets, opts.language); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.