diff --git a/src/unit/format.js b/src/unit/format.js index f8ca83b10..81cf1e5e6 100644 --- a/src/unit/format.js +++ b/src/unit/format.js @@ -25,7 +25,7 @@ define([ */ return function( value, numberFormatter, pluralGenerator, unitProperties ) { var compoundUnitPattern = unitProperties.compoundUnitPattern, dividend, dividendProperties, - formattedValue, divisor, divisorProperties, message, pluralValue; + formattedValue, divisor, divisorProperties, message, pluralValue, oneProperty; unitProperties = unitProperties.unitProperties; formattedValue = numberFormatter( value ); @@ -35,9 +35,10 @@ return function( value, numberFormatter, pluralGenerator, unitProperties ) { if ( unitProperties instanceof Array ) { dividendProperties = unitProperties[ 0 ]; divisorProperties = unitProperties[ 1 ]; + oneProperty = divisorProperties.hasOwnProperty( "one" ) ? "one" : "other"; dividend = formatMessage( dividendProperties[ pluralValue ], [ formattedValue ] ); - divisor = formatMessage( divisorProperties.one, [ "" ] ).trim(); + divisor = formatMessage( divisorProperties[oneProperty], [ "" ] ).trim(); return formatMessage( compoundUnitPattern, [ dividend, divisor ] ); } diff --git a/test/unit/unit/format.js b/test/unit/unit/format.js index a11d56b41..aca09b84b 100644 --- a/test/unit/unit/format.js +++ b/test/unit/unit/format.js @@ -4,36 +4,49 @@ define([ "src/unit/format", "src/unit/properties", "json!cldr-data/main/en/units.json", + "json!cldr-data/main/ja/units.json", "json!cldr-data/supplemental/likelySubtags.json" -], function( Cldr, Globalize, formatUnit, unitProperties, enUnits, likelySubtags ) { +], function( Cldr, Globalize, formatUnit, unitProperties, enUnits, jaUnits, likelySubtags ) { -var cldr, globalize; - -Cldr.load( enUnits ); -Cldr.load( likelySubtags ); - -globalize = new Globalize( "en" ); -cldr = globalize.cldr; +var cldr, globalize, units, pluralGenerator; QUnit.module( "Unit Format" ); -function oneOrOtherPluralGenerator( plural ) { - if ( plural === 1 ) { - return "one"; - } else { +units = { + en: enUnits, + ja: jaUnits +}; + +pluralGenerator = { + en: function oneOrOtherPluralGenerator( plural ) { + if ( plural === 1 ) { + return "one"; + } else { + return "other"; + } + }, + ja: function otherPluralGenerator() { return "other"; } -} +}; function stubNumberFormatter( number ) { return number.toString(); } -QUnit.assert.unitFormat = function ( value, unit, options, expected ) { +QUnit.assert.unitFormat = function ( value, unit, options, expected, language ) { + language = language || "en"; + + Cldr.load( units[ language ] ); + Cldr.load( likelySubtags ); + + globalize = new Globalize( language ); + cldr = globalize.cldr; + var unitProps = unitProperties( unit, options.form, cldr ); this.equal( - formatUnit( value, options.numberFormatter || stubNumberFormatter, oneOrOtherPluralGenerator, unitProps ), + formatUnit( value, options.numberFormatter || stubNumberFormatter, pluralGenerator[ language ], unitProps ), expected ); }; @@ -132,4 +145,15 @@ QUnit.test( "Compound form (narrow)", function ( assert ) { assert.unitFormat( 100, "consumption-mile-per-gallon", { form: "narrow" }, "100mpg" ); }); +QUnit.test( "Compound form (without precomputed) in language without 'one' unit", function ( assert ) { + assert.unitFormat( 1, "length-foot-per-second", { form: "long" }, "1 フィート毎秒", "ja" ); + assert.unitFormat( 100, "length-foot-per-second", { form: "long" }, "100 フィート毎秒", "ja" ); + assert.unitFormat( 1, "megabyte-per-second", { form: "narrow" }, "1MB/秒", "ja" ); + assert.unitFormat( 100, "megabyte-per-second", { form: "narrow" }, "100MB/秒", "ja" ); + + assert.unitFormat( 1.2345678910, "megabyte-per-second", + { form: "narrow", numberFormatter: function (number) { return number.toFixed(1); }}, + "1.2MB/秒", "ja" ); +}); + });