2011-08-10 12:55:16 +00:00
|
|
|
/*!
|
2014-09-04 18:47:48 +00:00
|
|
|
* Globalize v1.0.0-alpha.7
|
2011-08-10 12:55:16 +00:00
|
|
|
*
|
|
|
|
* http://github.com/jquery/globalize
|
|
|
|
*
|
2014-09-03 18:59:37 +00:00
|
|
|
* Copyright 2010, 2014 jQuery Foundation, Inc. and other contributors
|
2013-11-30 12:22:17 +00:00
|
|
|
* Released under the MIT license
|
2011-08-10 12:55:16 +00:00
|
|
|
* http://jquery.org/license
|
2013-11-30 12:22:17 +00:00
|
|
|
*
|
2014-09-04 18:47:48 +00:00
|
|
|
* Date: 2014-09-30T12:31Z
|
2011-08-10 12:55:16 +00:00
|
|
|
*/
|
2013-11-30 12:22:17 +00:00
|
|
|
(function( root, factory ) {
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
// UMD returnExports
|
2013-11-30 12:22:17 +00:00
|
|
|
if ( typeof define === "function" && define.amd ) {
|
2014-09-03 18:59:37 +00:00
|
|
|
|
|
|
|
// AMD
|
|
|
|
define([
|
|
|
|
"cldr",
|
|
|
|
"cldr/event"
|
|
|
|
], factory );
|
|
|
|
} else if ( typeof exports === "object" ) {
|
|
|
|
|
|
|
|
// Node, CommonJS
|
|
|
|
module.exports = factory( require( "cldrjs" ) );
|
2013-11-30 12:22:17 +00:00
|
|
|
} else {
|
2014-09-03 18:59:37 +00:00
|
|
|
|
2013-11-30 12:22:17 +00:00
|
|
|
// Global
|
2014-09-03 18:59:37 +00:00
|
|
|
root.Globalize = factory( root.Cldr );
|
2011-08-10 12:55:16 +00:00
|
|
|
}
|
2014-09-03 18:59:37 +00:00
|
|
|
}( this, function( Cldr ) {
|
2011-08-10 12:55:16 +00:00
|
|
|
|
|
|
|
|
2013-11-30 12:22:17 +00:00
|
|
|
/**
|
2014-09-03 18:59:37 +00:00
|
|
|
* A toString method that outputs meaningful values for objects or arrays and
|
|
|
|
* still performs as fast as a plain string in case variable is string, or as
|
|
|
|
* fast as `"" + number` in case variable is a number.
|
|
|
|
* Ref: http://jsperf.com/my-stringify
|
2013-11-30 12:22:17 +00:00
|
|
|
*/
|
2014-09-03 18:59:37 +00:00
|
|
|
var toString = function( variable ) {
|
|
|
|
return typeof variable === "string" ? variable : ( typeof variable === "number" ? "" +
|
|
|
|
variable : JSON.stringify( variable ) );
|
|
|
|
};
|
2011-08-10 12:55:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
/**
|
|
|
|
* formatMessage( message, data )
|
|
|
|
*
|
|
|
|
* @message [String] A message with optional {vars} to be replaced.
|
|
|
|
*
|
|
|
|
* @data [Array or JSON] Object with replacing-variables content.
|
|
|
|
*
|
|
|
|
* Return the formatted message. For example:
|
|
|
|
*
|
|
|
|
* - formatMessage( "{0} second", 1 ); // 1 second
|
|
|
|
*
|
|
|
|
* - formatMessage( "{0}/{1}", ["m", "s"] ); // m/s
|
|
|
|
*
|
|
|
|
* - formatMessage( "{name} <{email}>", {
|
|
|
|
* name: "Foo",
|
|
|
|
* email: "bar@baz.qux"
|
|
|
|
* }); // Foo <bar@baz.qux>
|
|
|
|
*/
|
|
|
|
var formatMessage = function( message, data ) {
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
// Replace {attribute}'s
|
|
|
|
message = message.replace( /{[0-9a-zA-Z-_. ]+}/g, function( name ) {
|
|
|
|
name = name.replace( /^{([^}]*)}$/, "$1" );
|
|
|
|
return toString( data[ name ] );
|
|
|
|
});
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
return message;
|
|
|
|
};
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
var createError = function( code, message, attributes ) {
|
|
|
|
var error;
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
message = code + ( message ? ": " + formatMessage( message, attributes ) : "" );
|
|
|
|
error = new Error( message );
|
|
|
|
error.code = code;
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
// extend( error, attributes );
|
|
|
|
Object.keys( attributes ).forEach(function( attribute ) {
|
|
|
|
error[ attribute ] = attributes[ attribute ];
|
|
|
|
});
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
return error;
|
|
|
|
};
|
2011-08-10 12:55:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
var validate = function( code, message, check, attributes ) {
|
|
|
|
if ( !check ) {
|
|
|
|
throw createError( code, message, attributes );
|
|
|
|
}
|
|
|
|
};
|
2011-08-10 12:55:16 +00:00
|
|
|
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2011-08-10 12:55:16 +00:00
|
|
|
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
var alwaysArray = function( stringOrArray ) {
|
|
|
|
return Array.isArray( stringOrArray ) ? stringOrArray : stringOrArray ? [ stringOrArray ] : [];
|
|
|
|
};
|
2011-08-10 12:55:16 +00:00
|
|
|
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
2011-08-10 12:55:16 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
var validateCldr = function( path, value, options ) {
|
|
|
|
var skipBoolean;
|
|
|
|
options = options || {};
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
skipBoolean = alwaysArray( options.skip ).some(function( pathRe ) {
|
|
|
|
return pathRe.test( path );
|
|
|
|
});
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
validate( "E_MISSING_CLDR", "Missing required CLDR content `{path}`.", value || skipBoolean, {
|
|
|
|
path: path
|
|
|
|
});
|
|
|
|
};
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-10 12:55:16 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
var validateDefaultLocale = function( value ) {
|
|
|
|
validate( "E_DEFAULT_LOCALE_NOT_DEFINED", "Default locale has not been defined.",
|
|
|
|
value !== undefined, {} );
|
|
|
|
};
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
var validateParameterPresence = function( value, name ) {
|
|
|
|
validate( "E_MISSING_PARAMETER", "Missing required parameter `{name}`.",
|
|
|
|
value !== undefined, { name: name });
|
|
|
|
};
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
/**
|
|
|
|
* range( value, name, minimum, maximum )
|
|
|
|
*
|
|
|
|
* @value [Number].
|
|
|
|
*
|
|
|
|
* @name [String] name of variable.
|
|
|
|
*
|
|
|
|
* @minimum [Number]. The lowest valid value, inclusive.
|
|
|
|
*
|
|
|
|
* @maximum [Number]. The greatest valid value, inclusive.
|
|
|
|
*/
|
|
|
|
var validateParameterRange = function( value, name, minimum, maximum ) {
|
|
|
|
validate(
|
|
|
|
"E_PAR_OUT_OF_RANGE",
|
|
|
|
"Parameter `{name}` has value `{value}` out of range [{minimum}, {maximum}].",
|
|
|
|
value === undefined || value >= minimum && value <= maximum,
|
|
|
|
{
|
|
|
|
maximum: maximum,
|
|
|
|
minimum: minimum,
|
|
|
|
name: name,
|
|
|
|
value: value
|
2011-08-10 12:55:16 +00:00
|
|
|
}
|
2014-09-03 18:59:37 +00:00
|
|
|
);
|
|
|
|
};
|
2011-08-10 12:55:16 +00:00
|
|
|
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
var validateParameterType = function( value, name, check, expected ) {
|
|
|
|
validate(
|
|
|
|
"E_INVALID_PAR_TYPE",
|
|
|
|
"Invalid `{name}` parameter ({value}). {expected} expected.",
|
|
|
|
check,
|
|
|
|
{
|
|
|
|
expected: expected,
|
|
|
|
name: name,
|
|
|
|
value: value
|
2011-08-10 12:55:16 +00:00
|
|
|
}
|
2014-09-03 18:59:37 +00:00
|
|
|
);
|
|
|
|
};
|
2011-08-10 12:55:16 +00:00
|
|
|
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2011-08-10 12:55:16 +00:00
|
|
|
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
var validateParameterTypeLocale = function( value, name ) {
|
|
|
|
validateParameterType(
|
|
|
|
value,
|
|
|
|
name,
|
|
|
|
value === undefined || typeof value === "string" || value instanceof Cldr,
|
|
|
|
"String or Cldr instance"
|
|
|
|
);
|
|
|
|
};
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
/**
|
|
|
|
* Function inspired by jQuery Core, but reduced to our use case.
|
|
|
|
*/
|
|
|
|
var isPlainObject = function( obj ) {
|
|
|
|
return obj !== null && "" + obj === "[object Object]";
|
|
|
|
};
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
var validateParameterTypePlainObject = function( value, name ) {
|
|
|
|
validateParameterType(
|
|
|
|
value,
|
|
|
|
name,
|
|
|
|
value === undefined || isPlainObject( value ),
|
|
|
|
"Plain Object"
|
|
|
|
);
|
|
|
|
};
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
var alwaysCldr = function( localeOrCldr ) {
|
|
|
|
return localeOrCldr instanceof Cldr ? localeOrCldr : new Cldr( localeOrCldr );
|
|
|
|
};
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
function validateLikelySubtags( cldr ) {
|
|
|
|
cldr.once( "get", validateCldr );
|
|
|
|
cldr.get( "supplemental/likelySubtags" );
|
|
|
|
}
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
/**
|
|
|
|
* [new] Globalize( locale|cldr )
|
|
|
|
*
|
|
|
|
* @locale [String]
|
|
|
|
*
|
|
|
|
* @cldr [Cldr instance]
|
|
|
|
*
|
|
|
|
* Create a Globalize instance.
|
|
|
|
*/
|
|
|
|
function Globalize( locale ) {
|
|
|
|
if ( !( this instanceof Globalize ) ) {
|
|
|
|
return new Globalize( locale );
|
2011-08-10 12:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
validateParameterPresence( locale, "locale" );
|
|
|
|
validateParameterTypeLocale( locale, "locale" );
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
this.cldr = alwaysCldr( locale );
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
validateLikelySubtags( this.cldr );
|
|
|
|
}
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
/**
|
|
|
|
* Globalize.load( json )
|
|
|
|
*
|
|
|
|
* @json [JSON]
|
|
|
|
*
|
|
|
|
* Load resolved or unresolved cldr data.
|
|
|
|
* Somewhat equivalent to previous Globalize.addCultureInfo(...).
|
|
|
|
*/
|
|
|
|
Globalize.load = function( json ) {
|
|
|
|
validateParameterPresence( json, "json" );
|
|
|
|
validateParameterTypePlainObject( json, "json" );
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
Cldr.load( json );
|
|
|
|
};
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
/**
|
|
|
|
* Globalize.locale( [locale|cldr] )
|
|
|
|
*
|
|
|
|
* @locale [String]
|
|
|
|
*
|
|
|
|
* @cldr [Cldr instance]
|
|
|
|
*
|
|
|
|
* Set default Cldr instance if locale or cldr argument is passed.
|
|
|
|
*
|
|
|
|
* Return the default Cldr instance.
|
|
|
|
*/
|
|
|
|
Globalize.locale = function( locale ) {
|
|
|
|
validateParameterTypeLocale( locale, "locale" );
|
2013-11-30 12:22:17 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
if ( arguments.length ) {
|
|
|
|
this.cldr = alwaysCldr( locale );
|
|
|
|
validateLikelySubtags( this.cldr );
|
2011-08-10 12:55:16 +00:00
|
|
|
}
|
2014-09-03 18:59:37 +00:00
|
|
|
return this.cldr;
|
|
|
|
};
|
2011-08-10 12:55:16 +00:00
|
|
|
|
2014-09-03 18:59:37 +00:00
|
|
|
/**
|
|
|
|
* Optimization to avoid duplicating some internal functions across modules.
|
|
|
|
*/
|
|
|
|
Globalize._alwaysArray = alwaysArray;
|
|
|
|
Globalize._createError = createError;
|
|
|
|
Globalize._formatMessage = formatMessage;
|
|
|
|
Globalize._isPlainObject = isPlainObject;
|
|
|
|
Globalize._validate = validate;
|
|
|
|
Globalize._validateCldr = validateCldr;
|
|
|
|
Globalize._validateDefaultLocale = validateDefaultLocale;
|
|
|
|
Globalize._validateParameterPresence = validateParameterPresence;
|
|
|
|
Globalize._validateParameterRange = validateParameterRange;
|
|
|
|
Globalize._validateParameterTypePlainObject = validateParameterTypePlainObject;
|
|
|
|
Globalize._validateParameterType = validateParameterType;
|
|
|
|
|
|
|
|
return Globalize;
|
2013-11-30 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}));
|