Socket
Socket
Sign inDemoInstall

messageformat

Package Overview
Dependencies
4
Maintainers
2
Versions
51
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.0-beta.4 to 2.0.0-beta.5

142

lib/get.js

@@ -72,4 +72,13 @@ /**

* Current locale
* @member {string}
*
* One of Get#availableLocales or `null`. Partial matches of language tags
* are supported, so e.g. with an `en` locale defined, it will be selected by
* `messages.locale = 'en-US'` and vice versa.
*
* @member {string|null} locale
*/
Object.defineProperty(this, 'locale', {
get: function() { return this._locale },
set: function(lc) { this._locale = this.resolveLocale(lc); }
});
this.locale = defaultLocale;

@@ -79,5 +88,14 @@

* Default fallback locale
* @member {string|null}
*
* One of Get#availableLocales or `null`. Partial matches of language tags
* are supported, so e.g. with an `en` locale defined, it will be selected by
* `messages.defaultLocale = 'en-US'` and vice versa.
*
* @member {string|null} defaultLocale
*/
this.defaultLocale = this.locale;
Object.defineProperty(this, 'defaultLocale', {
get: function() { return this._defaultLocale },
set: function(lc) { this._defaultLocale = this.resolveLocale(lc); }
});
this._defaultLocale = this._locale;
}

@@ -88,12 +106,38 @@

/**
* Add a new locale to the accessor; useful if loading locales dynamically
* @param {string} lc Locale identifier
* @param {object} data Hierarchical map of keys to functions
* Add new messages to the accessor; useful if loading data dynamically
*
* The locale code `lc` should be an exact match for the locale being updated,
* or empty to default to the current locale. Use {@link #resolveLocale} for
* resolving partial locale strings.
*
* If `keypath` is empty, adds or sets the complete message object for the
* corresponding locale. If any keys in `keypath` do not exist, a new object
* will be created at that key.
*
* @param {function|object} data Hierarchical map of keys to functions, or a
* single message function
* @param {string} [lc] If empty or undefined, defaults to `this.locale`
* @param {string[]} [keypath] The keypath being added
* @returns {Get} The Get instance, to allow for chaining
*/
Get.prototype.addLocale = function(lc, data) {
this._data[lc] = Object.keys(data).reduce(function(map, key) {
if (key !== 'toString') map[key] = data[key];
return map
}, {});
Get.prototype.addMessages = function(data, lc, keypath) {
if (!lc) lc = this.locale;
if (typeof data !== 'function') {
data = Object.keys(data).reduce(function(map, key) {
if (key !== 'toString') map[key] = data[key];
return map
}, {});
}
if (Array.isArray(keypath) && keypath.length > 0) {
let parent = this._data[lc]
for (let i = 0; i < keypath.length - 1; ++i) {
const key = keypath[i];
if (!parent[key]) parent[key] = {}
parent = parent[key]
}
const key = keypath[keypath.length - 1]
parent[key] = data
} else {
this._data[lc] = data
}
return this;

@@ -103,2 +147,26 @@ }

/**
* Resolve `lc` to the key of an available locale or `null`, allowing for
* partial matches. For example, with an `en` locale defined, it will be
* selected by `messages.defaultLocale = 'en-US'` and vice versa.
*
* @param {string} lc Locale code
* @returns {string|null}
*/
Get.prototype.resolveLocale = function(lc) {
if (this._data[lc]) return lc;
if (lc) {
var l = String(lc);
while (l = l.replace(/[-_]?[^-_]*$/, '')) {
if (this._data[l]) return l;
}
var ll = this.availableLocales;
var re = new RegExp('^' + lc + '[-_]');
for (var i = 0; i < ll.length; ++i) {
if (re.test(ll[i])) return ll[i];
}
}
return null;
}
/**
* Get the list of fallback locales

@@ -130,28 +198,2 @@ * @param {string} [lc] If empty or undefined, defaults to `this.locale`

/** @private */
function _get(obj, key) {
if (!obj) return null;
if (Array.isArray(key)) {
for (var i = 0; i < key.length; ++i) {
obj = obj[key[i]];
if (!obj) return null;
}
return obj;
}
return obj[key];
}
/** @private */
function _has(data, lc, key, fallback, type) {
var msg = _get(data[lc], key);
if (msg) return typeof msg === type;
if (fallback) {
for (var i = 0; i < fallback.length; ++i) {
msg = _get(data[fallback[i]], key);
if (msg) return typeof msg === type;
}
}
return false;
}
/**

@@ -219,1 +261,27 @@ * Check if `key` is a message function for the locale

}
/** @private */
function _get(obj, key) {
if (!obj) return null;
if (Array.isArray(key)) {
for (var i = 0; i < key.length; ++i) {
obj = obj[key[i]];
if (!obj) return null;
}
return obj;
}
return obj[key];
}
/** @private */
function _has(data, lc, key, fallback, type) {
var msg = _get(data[lc], key);
if (msg) return typeof msg === type;
if (fallback) {
for (var i = 0; i < fallback.length; ++i) {
msg = _get(data[fallback[i]], key);
if (msg) return typeof msg === type;
}
}
return false;
}
{
"name": "messageformat",
"version": "2.0.0-beta.4",
"version": "2.0.0-beta.5",
"description": "PluralFormat and SelectFormat Message and i18n Tool - A JavaScript Implemenation of the ICU standards.",

@@ -5,0 +5,0 @@ "keywords": [

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc