Skip to content

Instantly share code, notes, and snippets.

@ca0v
Last active May 31, 2016 19:22
Show Gist options
  • Save ca0v/b96e4e8d009112fc5f0159940fef7e08 to your computer and use it in GitHub Desktop.
Save ca0v/b96e4e8d009112fc5f0159940fef7e08 to your computer and use it in GitHub Desktop.
Experimental localization management using prototypical inheritence
/**
* Usage:
import Resx = require("i18n");
let en = Resx("en");
en.foo = "bar";
let enUs = Resx("en-US");
should.equal(en.foo, "bar");
should.equal(enUs.foo, "bar");
en.foo = "en-foo";
should.equal(en.foo, "en-foo", "en test 1");
should.equal(enUs.foo, "en-foo", "en-us test 1");
enUs.foo = "en-us-foo";
should.equal(en.foo, "en-foo", "en test 2");
should.equal(enUs.foo, "en-us-foo", "en-us test 2");
*/
class i18n {
static get(locale: string) {
let factory = i18n[locale];
if (!factory) {
switch (locale.length) {
case 5:
factory = i18n[locale] = Object.create(i18n.get(locale.substring(0, 2)));
break;
case 2:
default:
factory = i18n[locale] = {};
break;
}
}
return factory;
}
}
export = i18n.get;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment