From 1eea34e20240c0c698cc46bced316bd6ec5abcff Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Tue, 2 Aug 2011 18:42:56 -0400 Subject: [PATCH] [#674] Popcorn.locale, initial implementation --- popcorn.js | 64 ++++++++++++++++++++++++++ test/popcorn.unit.js | 104 ++++++++++++++++++++++++------------------- 2 files changed, 122 insertions(+), 46 deletions(-) diff --git a/popcorn.js b/popcorn.js index 9d31bc6..3f6448e 100644 --- a/popcorn.js +++ b/popcorn.js @@ -1513,6 +1513,70 @@ } }; + + // Initialize locale data + // Based on http://en.wikipedia.org/wiki/Language_localisation#Language_tags_and_codes + function initLocale( arg ) { + + var locale = typeof arg === "string" ? arg : [ arg.language, arg.region ].join( "-" ), + parts = locale.split( "-" ); + + // Setup locale data table + return { + iso6391: locale, + language: parts[ 0 ] || "", + region: parts[ 1 ] || "" + }; + } + + // Declare locale data table + var localeData = initLocale( global.navigator.language ); + + Popcorn.locale = { + + // Popcorn.localte.get() + // returns reference to privately + // defined localeData + get: function() { + return localeData; + }, + + // Popcorn.locale.set( string|object ); + set: function( arg ) { + + localeData = initLocale( arg ); + + Popcorn.locale.broadcast(); + + return localeData; + }, + + // Popcorn.locale.broadcast( type ) + // Sends events to all popcorn media instances that are + // listening for locale events + broadcast: function( type ) { + + var instances = Popcorn.instances, + length = instances.length, + idx = 0, + instance; + + type = type || "locale:changed"; + + // Iterate all current instances + for ( ; idx < length; idx++ ) { + instance = instances[ idx ]; + + // For those instances with locale event listeners, + // trigger a locale change event + if ( type in instance.data.events ) { + instance.trigger( type ); + } + } + } + }; + + // Exposes Popcorn to global context global.Popcorn = Popcorn; diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index e4a4f93..cd57d98 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -728,74 +728,86 @@ module("Popcorn Locale"); test( "Popcorn.locale object", function() { - var api = { - get: "function", - set: "function", - broadcast: "function" - }, - locale = navigator.language, - parts = locale.split("-"), - - stub = { - i18n: locale, - language: parts[ 0 ], - region: parts[ 1 ] - }, - $pop = Popcorn("#video"), - expects = 12, - count = 0; + var api = { + get: "function", + set: "function", + broadcast: "function" + }, + locale = navigator.language, + parts = locale.split("-"), + + stub = { + iso6391: locale, + language: parts[ 0 ], + region: parts[ 1 ] + }, + $pop = Popcorn("#video"), + expects = 18, + count = 0; expect( expects ); function plus() { if ( ++count == expects ) { start(); + Popcorn.removeInstance( $pop ); } } stop( 10000 ); - ok( Popcorn.locale, "Popcorn.locale exists"); - plus(); - - equal( typeof Popcorn.locale, "object", "Popcorn.locale is an object"); - plus(); + ok( Popcorn.locale, "Popcorn.locale exists"); + plus(); - Popcorn.forEach( api, function( type, method ) { - ok( Popcorn.locale[ method ], Popcorn.locale[ method ] + " exists" ); - plus(); - equal( typeof Popcorn.locale[ method ], type, Popcorn.locale[ method ] + " is a " + type ); - plus(); - }); + equal( typeof Popcorn.locale, "object", "Popcorn.locale is an object"); + plus(); - deepEqual( Popcorn.locale.get(), stub, "Popcorn.locale.get() === navigator.language (" + JSON.stringify(stub) + ") whenever possible" ); - plus(); + Popcorn.forEach( api, function( type, method ) { + ok( Popcorn.locale[ method ], "Popcorn.locale." + method + "() exists" ); + plus(); + equal( typeof Popcorn.locale[ method ], type, "Popcorn.locale." + method + "() is a " + type ); + plus(); + }); - Popcorn.forEach( Popcorn.locale.get(), function( val, prop ) { - equal( val, stub[ prop ], "locale matches stub" ); - plus(); - }); + deepEqual( Popcorn.locale.get(), stub, "Popcorn.locale.get() === navigator.language (" + JSON.stringify(stub) + ") whenever possible" ); + plus(); + Popcorn.forEach( Popcorn.locale.get(), function( val, prop ) { + equal( val, stub[ prop ], "Popcorn.locale.get() locale matches stub" ); + plus(); + }); - Popcorn.locale.set("fr-CA"); + locale = "fr-CA"; - $pop.listen("locale:changed", function() { + // Setup "locale:changed" event listener + $pop.listen( "locale:changed", function() { - var locale = "fr-CA", - parts = locale.split("-"), + var parts = locale.split( "-" ), stub = { - i18n: locale, - language: parts[ 0 ], - region: parts[ 1 ] - }; + iso6391: locale, + language: parts[ 0 ], + region: parts[ 1 ] + }; + + + Popcorn.forEach( Popcorn.locale.get(), function( val, prop ) { + equal( val, stub[ prop ], "Popcorn.locale.set() -> get() locale matches updated stub" ); + plus(); + }); + + + if ( locale === "fr-CA" ) { + locale = "en-CA"; - Popcorn.forEach( Popcorn.locale.get(), function( val, prop ) { - equal( val, stub[ prop ], "locale matches updated stub" ); - plus(); - }); - }); + // Change locale, trigger "locale:changed" event + Popcorn.locale.set( "en-CA" ); + } + + }); + // Change locale, trigger "locale:changed" event + Popcorn.locale.set( "fr-CA" ); }); -- 2.43.0