Skip to content

Instantly share code, notes, and snippets.

@maggiben
Last active January 11, 2021 23:52
Show Gist options
  • Save maggiben/9413979 to your computer and use it in GitHub Desktop.
Save maggiben/9413979 to your computer and use it in GitHub Desktop.
I8N (Internacionalization) Module fast easy and jQuery independent
////////////////////////////////////////////////////////////////////////////////
// @file : i8n.js //
// @summary : Internacionalization module //
// @version : 0.1 //
// @project : i8n //
// @description : //
// @author : Benjamin Maggi //
// @email : [email protected] //
// @date : 7 Mar 2014 //
// -------------------------------------------------------------------------- //
// //
// License: //
// (The MIT License) //
// //
// Permission is hereby granted, free of charge, to any person obtaining a //
// copy of this software and associated documentation files (the 'Software'), //
// to deal in the Software without restriction, including without limitation //
// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
// and/or sell copies of the Software, and to permit persons to whom the //
// Software is furnished to do so, subject to the following conditions: //
// //
// The above copyright notice and this permission notice shall be included //
// in all copies or substantial portions of the Software. //
// //
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
// DEALINGS IN THE SOFTWARE. //
////////////////////////////////////////////////////////////////////////////////
(function (root, factory) {
"use strict";
if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else {
// Browser globals (root is window)
root.I8N = factory();
}
}(this, function () {
'use strict';
////////////////////////////////////////////////////////////////////////////
// Private helpers //
////////////////////////////////////////////////////////////////////////////
String.prototype.format = function() {
var formatted = this;
var arg = arguments[0];
var regexp = null;
var key = null;
if(arg === Object(arg) && arg instanceof Array !== true) {
for(key in arg) {
if(arg.hasOwnProperty(key)) {
regexp = new RegExp('\\{{' + key + '\\}}', 'gi');
formatted = formatted.replace(regexp, arg[key]);
}
}
} else {
for (var i = 0; i < arguments.length; i++) {
var regexp = new RegExp('\\{{' + i + '\\}}', 'gi');
formatted = formatted.replace(regexp, arguments[i]);
}
}
return formatted;
};
////////////////////////////////////////////////////////////////////////////
// Constructor //
////////////////////////////////////////////////////////////////////////////
var I8N = (function() {
function I8N(file, cb) {
var that = this;
this.version = '1.0';
this.language = 'en';
this.data = null;
this.isReady = false;
console.log("init: ", file);
var self = this;
var xhr = new XMLHttpRequest();
xhr.open('GET', file);
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
self.data = JSON.parse(this.responseText);
self.isReady = true;
self.updateUi(document);
if (typeof cb !== "undefined") {
cb(self);
}
}
};
xhr.send(null);
return this;
}
I8N.prototype.get = function(key) {
var localized = null;
if (this.data) {
localized = this.data[key];
}
if(!!localized) {
return localized;
} else {
return key;
}
}
I8N.prototype.updateUi = function(element) {
var self = this;
var nodes = element.querySelectorAll('[data-i8n]');
// update i8n values
for(var i = 0; i < nodes.length; i++) {
var node = nodes[i];
var key = node.getAttribute('data-i8n');
var keyTokens = key.split(':');
if (keyTokens.length == 2) {
if (keyTokens[0] == 'html') {
// html
node.innerHTML = self.get(keyTokens[1]);
} else {
// other attribute
node.setAttribute(keyTokens[0], self.get(keyTokens[1]));
}
} else {
// text
if(node.textContent.match(',')) {
var args = node.textContent.split(',');
var text = self.get(key);
var result = String.prototype.format.apply(text, args);
node.textContent = result;
} else {
node.textContent = self.get(key);
}
}
node.removeAttribute('data-i8n');
}
// remove i8n initialization attributes
var elems = element.querySelectorAll('[data-i8n-show=onReady]');
if(elems.length > 0) {
elems.setAttribute('data-i8n-show', null);
elems.setAttribute('data-i8n-ready', true);
}
return element;
}
I8N.prototype.getVersion = function() {
console.log(this.version);
return this.version;
};
return I8N;
}());
// Just return a value to define the module export.
// This example returns an object, but the module
// can return a function as the exported value.
return I8N;
}));
@maggiben
Copy link
Author

maggiben commented Mar 7, 2014

Just added a callback so that the creator knows when the translations are ready to be used

@maggiben
Copy link
Author

Now you can do:

KEY: "the {{0}} is under the {{1}}"

cat,table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment