Last active
August 29, 2015 14:08
-
-
Save SlexAxton/7a447440cb2aa5b8c10f to your computer and use it in GitHub Desktop.
msgpack requirejs plugin
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
{ | |
"somekey" : { | |
"message" : "Submit", | |
"description" : "This is the label next to the button where the form is sent to the server." | |
}, | |
"somekey2" : { | |
"message" : "Cancel", | |
"description" : "This is the text on the button that clears the form and sends you back to the page beforehand." | |
}, | |
"excited_somekey" : { | |
"message" : "GO GO GO!!!", | |
"description" : "This is the label next to the button where the form is sent to the server, but in a really enthusiastic manner." | |
}, | |
} |
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
define([ | |
//>>excludeStart('excludeDev', pragmas.excludeDev) | |
'underscore', 'vendor/messageformat', 'vendor/json2' | |
//>>excludeEnd('excludeDev') | |
], | |
function ( | |
//>>excludeStart('excludeDev', pragmas.excludeDev) | |
_, MessageFormat, JSON | |
//>>excludeEnd('excludeDev') | |
) { | |
//>>excludeStart('excludeDev', pragmas.excludeDev) | |
var fs; | |
var getXhr; | |
var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0']; | |
var fetchText = function () { | |
throw new Error('Environment unsupported.'); | |
}; | |
var customNameExtension = "@mf"; | |
var fileExtension = ".mf.json"; | |
var buildMap = {}; | |
var cache = {}; | |
var fetchOrGetCached; | |
if (typeof window !== "undefined" && window.navigator && window.document) { | |
// Browser action | |
getXhr = function () { | |
//Would love to dump the ActiveX crap in here. Need IE 6 to die first. | |
var xhr; | |
var i; | |
var progId; | |
if (typeof XMLHttpRequest !== "undefined") { | |
return new XMLHttpRequest(); | |
} | |
else { | |
for (i = 0; i < 3; i++) { | |
progId = progIds[i]; | |
try { | |
xhr = new ActiveXObject(progId); | |
} | |
catch (e) {} | |
if (xhr) { | |
progIds = [progId]; // so faster next time | |
break; | |
} | |
} | |
} | |
if (!xhr) { | |
throw new Error("getXhr(): XMLHttpRequest not available"); | |
} | |
return xhr; | |
}; | |
fetchText = function (url, callback) { | |
var xhr = getXhr(); | |
if ( ! ~url.indexOf('http') ) { | |
url = window.location.protocol + url; | |
} | |
xhr.open('GET', url, true); | |
xhr.onreadystatechange = function (evt) { | |
if (xhr.readyState === 4 ) { | |
if ( xhr.status === 200) { | |
callback(xhr.responseText); | |
} | |
else if (require.onError) { | |
require.onError({ | |
"hbs" : true, | |
"status" : xhr.status, | |
"url" : url | |
}); | |
} | |
} | |
}; | |
xhr.send(null); | |
}; | |
} | |
else if (typeof process !== "undefined" && process.versions && !!process.versions.node) { | |
//Using special require.nodeRequire, something added by r.js. | |
fs = require.nodeRequire('fs'); | |
fetchText = function (path, callback) { | |
callback(fs.readFileSync(path, 'utf8')); | |
}; | |
} | |
fetchOrGetCached = function (name, parentRequire, config, callback) { | |
if (config.turbo) { | |
require(['mfMap'], function (map) { | |
callback(map[name]); | |
}); | |
return; | |
} | |
var path = parentRequire.toUrl(name + fileExtension); | |
if (cache[path]){ | |
callback(cache[path]); | |
} | |
else { | |
fetchText(path, function (data) { | |
cache[path] = data; | |
callback.call(this, data); | |
}); | |
} | |
}; | |
//>>excludeEnd('excludeDev') | |
return { | |
load : function (name, parentRequire, load, config) { | |
//>>excludeStart('excludeDev', pragmas.excludeeDev) | |
// TODO :: actually take locale arguments | |
var mf = new MessageFormat(); | |
// No collisions this way | |
var compiledName = name + customNameExtension; | |
var defaultTs = {}; | |
fetchOrGetCached(name, parentRequire, config, function (rawMf) { | |
var messages = JSON.parse(rawMf); | |
var compiledPairs = []; | |
_(messages).forEach(function (msg, key) { | |
// Handle individual overrides | |
if (config.localeMapping && config.localeMapping[key]) { | |
msg = config.localeMapping[key]; | |
} | |
else if (defaultTs[key]) { | |
msg = defaultTs[key]; | |
} | |
else { | |
msg = msg.message; | |
} | |
// Just precompile here | |
var precompileFunc; | |
try { | |
precompileFunc = mf.precompile( mf.parse(msg) ); | |
} | |
catch (e) { | |
precompileFunc = 'function () { throw ' + JSON.stringify( e.toString() ) + '; }'; | |
} | |
var precompileFuncStr = 'function (x) { try { return (' + precompileFunc + ')(x||{}); } catch(e){ throw new Error(\'MF error on `' + key + '`: \' + e.toString()); return ""; } }'; | |
compiledPairs.push('"' + key.replace(/"/g, '\"') + '" : ' + precompileFuncStr); | |
}); | |
// Create the module as a string | |
var text = "define(['vendor/messageformat'], function (MessageFormat) { \n" + | |
" return {\n" + compiledPairs.join(',') + "\n};\n" + | |
"});\n"; | |
buildMap[compiledName] = text; | |
load.fromText(text); | |
//Give result to load. Need to wait until the module | |
//is fully parse, which will happen after this | |
//execution. | |
parentRequire([name], function (value) { | |
load(value); | |
}); | |
}); | |
//>>excludeEnd('excludeDev') | |
}, | |
write : function (pluginName, moduleName, write) { | |
//>>excludeStart('excludeDev', pragmas.excludeDev) | |
if ((moduleName + customNameExtension) in buildMap) { | |
var text = buildMap[moduleName + customNameExtension]; | |
write.asModule(pluginName + "!" + moduleName, text); | |
} | |
//>>excludeEnd('excludeDev') | |
} | |
}; | |
}); |
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
// Assumes file extension `.mf.json` | |
define(['mf!path/to/messagefile'], function (msgPack) { | |
console.log(msgPack.somekey({data: 'here if needed'})); | |
}); |
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
define(['vendor/handlebars/runtime', 'underscore', 'json!path/to/localeMapping'], function (Handlebars, _, localeMapping) { | |
function extmsg (key, msgpack, data, options) { | |
var prefix = options.hash.prefix; | |
if (options.hash.prefixNum) { | |
var i; | |
for (i = 1; i < options.hash.prefixNum; i ++) { | |
var index = 'prefix' + i; | |
prefix = prefix + options.hash[index]; | |
} | |
} | |
var matchKey = (!prefix) ? key : prefix + key; | |
var matchKeyAndComponent; | |
if (options.hash.includeComponentId) { | |
matchKeyAndComponent = (!prefix) ? key + '_' + data.componentId : prefix + key + '_' + data.componentId; | |
} | |
var defaultString = options.hash.defaultDisplay || ''; | |
var value; | |
if ( (!matchKey && !matchKeyAndComponent) || ( !msgpack && _(localeMapping).isEmpty() ) ) { | |
return new Handlebars.SafeString(defaultString); | |
} | |
var temp; | |
if (msgpack && options.hash.includeComponentId) { | |
temp = msgpack[matchKeyAndComponent]; | |
} | |
if (!temp && msgpack) { | |
temp = msgpack[matchKey]; | |
} | |
if (!temp && localeMapping) { | |
temp = localeMapping[matchKey]; | |
} | |
if (!temp) { | |
return new Handlebars.SafeString(defaultString); | |
} | |
value = _(temp).isFunction() ? temp(data) : temp; | |
return _(value).isEmpty() ? new Handlebars.SafeString(defaultString) : new Handlebars.SafeString(value); | |
} | |
Handlebars.registerHelper('extmsg', extmsg); | |
return extmsg; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment