Skip to content

Commit

Permalink
use sessionStorage for I18N strings to save RAM, update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
chemerisuk committed Mar 24, 2014
1 parent 08de090 commit aa78ff5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
3 changes: 1 addition & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ module.exports = function(grunt) {

grunt.task.run([
"clean:build",
"browserify",
"uglify"
"browserify"
]);
});

Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@
},
"docs": "http://chemerisuk.github.io/better-dom",
"devDependencies": {
"grunt": "^0.4",
"grunt-contrib-watch": "^0.5",
"grunt-contrib-jshint": "^0.8",
"grunt-contrib-uglify": "^0.3",
"grunt-contrib-clean": "^0.5",
"grunt-contrib-connect": "^0.3",
"grunt-shell": "^0.6",
"grunt": "^0.4.3",
"grunt-contrib-watch": "^0.6.1",
"grunt-contrib-jshint": "^0.9.2",
"grunt-contrib-uglify": "^0.4.0",
"grunt-contrib-clean": "^0.5.0",
"grunt-contrib-connect": "^0.7.1",
"grunt-shell": "^0.6.4",
"grunt-browserify": "^2.0",
"platform": "1.0.0",
"benchmark": "1.0.0",
"lodash": "1.2.1",
"grunt-jsdoc": "~0.5.0",
"ink-docstrap": "~0.3.0",
"grunt-karma": "0.8.2",
"karma": "~0.12",
"karma-jasmine": "^0.2",
"karma-coverage": "^0.1",
"karma-phantomjs-launcher": "^0.1",
"karma-chrome-launcher": "^0.1",
"karma-firefox-launcher": "^0.1",
"karma-safari-launcher": "^0.1",
"karma-opera-launcher": "^0.1",
"karma": "^0.12.0",
"karma-jasmine": "^0.2.0",
"karma-coverage": "^0.1.0",
"karma-phantomjs-launcher": "^0.1.0",
"karma-chrome-launcher": "^0.1.0",
"karma-firefox-launcher": "^0.1.0",
"karma-safari-launcher": "^0.1.0",
"karma-opera-launcher": "^0.1.0",
"es6-browserify": "^0.0.7",
"browserify-es6-modules": "^0.1.1",
"coveralls": "^2.10.0"
Expand Down
32 changes: 22 additions & 10 deletions src/element.i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@ import $Element from "./element";
* @see https://github.com/chemerisuk/better-dom/wiki/Localization
*/

var strings = {},
languages = [];
var VALUE_SEPARATOR = "\n",
readValue = (key) => {
var value = sessionStorage["__" + key];

return value ? value.split(VALUE_SEPARATOR) : [];
},
languages = readValue("");

/**
* Get/set localized value
* @memberOf module:i18n
* @param {String} [value] resource string key
* @param {String} [key] resource string key
* @param {Object|Array} [varMap] resource string variables
* @return {String|$Element}
*/
$Element.prototype.i18n = function(value, varMap) {
$Element.prototype.i18n = function(key, varMap) {
if (!arguments.length) return this.get("data-i18n");

if (value && typeof value !== "string" || varMap && typeof varMap !== "object") throw _.makeError("i18n");
if (key && typeof key !== "string" || varMap && typeof varMap !== "object") throw _.makeError("i18n");
// update data-i18n-{lang} attributes
[value].concat(strings[value]).forEach((value, index) => {
var str = sessionStorage["__" + key];

str = str ? [key].concat(str.split(VALUE_SEPARATOR)) : [key];

str.forEach((value, index) => {
var attrName = "data-i18n" + (index ? "-" + languages[index - 1] : "");

if (value) this.set(attrName, varMap ? _.format(value, varMap) : value);
Expand All @@ -43,18 +52,21 @@ $Element.prototype.i18n = function(value, varMap) {
DOM.importStrings = function(lang, key, value) {
var keyType = typeof key,
attrName = "data-i18n-" + lang,
langIndex = languages.indexOf(lang);
langIndex = languages.indexOf(lang),
str;

if (keyType === "string") {
if (langIndex === -1) {
langIndex = languages.push(lang) - 1;
// add global rule for the data-i18n-{lang} attribute
DOM.importStyles("[" + attrName + "]:lang(" + lang + "):before", "content:attr(" + attrName + ")");
// persiste changed languages array
sessionStorage.__ = languages.join(VALUE_SEPARATOR);
}

if (!strings[key]) strings[key] = [];
// store localized string internally
strings[key][langIndex] = value;
str = readValue(key);
str[langIndex] = value;
sessionStorage["__" + key] = str.join(VALUE_SEPARATOR);

DOM.ready(() => DOM.findAll("[data-i18n=\"" + key + "\"]").set(attrName, value));
} else if (keyType === "object") {
Expand Down
4 changes: 4 additions & 0 deletions test/spec/element.i18n.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
describe("i18n", function() {
"use strict";

beforeEach(function() {
sessionStorage.clear();
});

it("should return data-i18n value if no arguments specified", function() {
expect(DOM.create("span[data-i18n=test]").i18n()).toBe("test");
});
Expand Down

0 comments on commit aa78ff5

Please sign in to comment.