UNPKG

sugar

Version:

A Javascript utility library for working with native objects.

74 lines (60 loc) 1.66 kB
'use strict'; var LazyLoadedLocales = require('./LazyLoadedLocales'), AmericanEnglishDefinition = require('./AmericanEnglishDefinition'), getNewLocale = require('../internal/getNewLocale'); var English, localeManager; function buildLocales() { function LocaleManager(loc) { this.locales = {}; this.add(loc); } LocaleManager.prototype = { get: function(code, fallback) { var loc = this.locales[code]; if (!loc && LazyLoadedLocales[code]) { loc = this.add(code, LazyLoadedLocales[code]); } else if (!loc && code) { loc = this.locales[code.slice(0, 2)]; } return loc || fallback === false ? loc : this.current; }, getAll: function() { return this.locales; }, set: function(code) { var loc = this.get(code, false); if (!loc) { throw new TypeError('Invalid Locale: ' + code); } return this.current = loc; }, add: function(code, def) { if (!def) { def = code; code = def.code; } else { def.code = code; } var loc = def.compiledFormats ? def : getNewLocale(def); this.locales[code] = loc; if (!this.current) { this.current = loc; } return loc; }, remove: function(code) { if (this.current.code === code) { this.current = this.get('en'); } return delete this.locales[code]; } }; // Sorry about this guys... English = getNewLocale(AmericanEnglishDefinition); localeManager = new LocaleManager(English); } buildLocales(); module.exports = { English: English, localeManager: localeManager };