Skip to content

Instantly share code, notes, and snippets.

@phlik
Created January 4, 2014 02:36
Show Gist options
  • Save phlik/8250770 to your computer and use it in GitHub Desktop.
Save phlik/8250770 to your computer and use it in GitHub Desktop.
var termDictionay = {"first":'text goes here'};
var localization = (function(terms) {
var _terms = terms;
var retVal = {
stampValues : function (template, replacements) {
/// <summary>Replaes tokens in a template string</summary>
/// <param name="template">string that is to have values replaced in</param>
/// <param name="replacements" optional="true"> Values to be replaced in a string template. In json formate {term:value}</param>
/// <returns>string with all changes made to it.</returns>
for (var tag in replacements) {
var reg = new RegExp('\{' + tag + '\}', 'g');
template = template.replace(reg, replacements[tag]);
}
return template;
},
getString : function(key, replacemetns) {
/// <summary>Gets a localized string value.</summary>
/// <param name="key">Key of the value to retrieve.</param>
/// <param name="replacements" optional="true"> Values to be replaced in a string template.</param>
/// <returns>Value if found; otherwise empty string.</returns>
var value = '';
// Verify data store exists.
if (_terms) {
value = _terms[key];
}
// Default to key if not found.
if (!value || value.length === 0) {
value = key;
}
if (replacements) {
value = instance.stampValues(value, replacements);
}
return value;
}
}
return retVal;
};)(termDictionay)
String.prototype.stampValues = function (replacements) {
return localization.stampValues(this, replacements);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment