/*! * Globalize v1.0.0-alpha.6 * * http://github.com/jquery/globalize * * Copyright 2010, 2014 jQuery Foundation, Inc. and other contributors * Released under the MIT license * http://jquery.org/license * * Date: 2014-09-01T21:32Z */ (function( root, factory ) { // UMD returnExports if ( typeof define === "function" && define.amd ) { // AMD define([ "cldr", "cldr/event" ], factory ); } else if ( typeof exports === "object" ) { // Node, CommonJS module.exports = factory( require( "cldrjs" ) ); } else { // Global root.Globalize = factory( root.Cldr ); } }( this, function( Cldr ) { /** * 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 */ var toString = function( variable ) { return typeof variable === "string" ? variable : ( typeof variable === "number" ? "" + variable : JSON.stringify( variable ) ); }; /** * 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 */ var formatMessage = function( message, data ) { // Replace {attribute}'s message = message.replace( /{[0-9a-zA-Z-_. ]+}/g, function( name ) { name = name.replace( /^{([^}]*)}$/, "$1" ); return toString( data[ name ] ); }); return message; }; var createError = function( code, message, attributes ) { var error; message = code + ( message ? ": " + formatMessage( message, attributes ) : "" ); error = new Error( message ); error.code = code; // extend( error, attributes ); Object.keys( attributes ).forEach(function( attribute ) { error[ attribute ] = attributes[ attribute ]; }); return error; }; var validate = function( code, message, check, attributes ) { if ( !check ) { throw createError( code, message, attributes ); } }; var alwaysArray = function( stringOrArray ) { return Array.isArray( stringOrArray ) ? stringOrArray : stringOrArray ? [ stringOrArray ] : []; }; var validateCldr = function( path, value, options ) { var skipBoolean; options = options || {}; skipBoolean = alwaysArray( options.skip ).some(function( pathRe ) { return pathRe.test( path ); }); validate( "E_MISSING_CLDR", "Missing required CLDR content `{path}`.", value || skipBoolean, { path: path }); }; var validateDefaultLocale = function( value ) { validate( "E_DEFAULT_LOCALE_NOT_DEFINED", "Default locale has not been defined.", value !== undefined, {} ); }; var validateParameterPresence = function( value, name ) { validate( "E_MISSING_PARAMETER", "Missing required parameter `{name}`.", value !== undefined, { name: name }); }; /** * 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 } ); }; 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 } ); }; var validateParameterTypeLocale = function( value, name ) { validateParameterType( value, name, value === undefined || typeof value === "string" || value instanceof Cldr, "String or Cldr instance" ); }; /** * Function inspired by jQuery Core, but reduced to our use case. */ var isPlainObject = function( obj ) { return obj !== null && "" + obj === "[object Object]"; }; var validateParameterTypePlainObject = function( value, name ) { validateParameterType( value, name, value === undefined || isPlainObject( value ), "Plain Object" ); }; var alwaysCldr = function( localeOrCldr ) { return localeOrCldr instanceof Cldr ? localeOrCldr : new Cldr( localeOrCldr ); }; function validateLikelySubtags( cldr ) { cldr.once( "get", validateCldr ); cldr.get( "supplemental/likelySubtags" ); } /** * [new] Globalize( locale|cldr ) * * @locale [String] * * @cldr [Cldr instance] * * Create a Globalize instance. */ function Globalize( locale ) { if ( !( this instanceof Globalize ) ) { return new Globalize( locale ); } validateParameterPresence( locale, "locale" ); validateParameterTypeLocale( locale, "locale" ); this.cldr = alwaysCldr( locale ); validateLikelySubtags( this.cldr ); } /** * 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" ); Cldr.load( json ); }; /** * 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" ); if ( arguments.length ) { this.cldr = alwaysCldr( locale ); validateLikelySubtags( this.cldr ); } return this.cldr; }; /** * 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; }));