]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#674] Popcorn.locale, initial implementation
authorRick Waldron <waldron.rick@gmail.com>
Tue, 2 Aug 2011 22:42:56 +0000 (18:42 -0400)
committerRick Waldron <waldron.rick@gmail.com>
Tue, 2 Aug 2011 22:42:56 +0000 (18:42 -0400)
popcorn.js
test/popcorn.unit.js

index 9d31bc6d39b76a6f20d5362cf9eb12a78430d820..3f6448e7a1e6ed182a61680a44ba00eee25a9fda 100644 (file)
     }
   };
 
+
+  // 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;
 
index e4a4f9323714c9681b0e5bf4fbd8602879851dc6..cd57d98cbc6c70342f8bc9925983ba120f7c6d2c 100644 (file)
@@ -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" );
 
 });