}
};
+
+ // 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;
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" );
});