popcorn-js/modules/locale/popcorn.locale.js

66 строки
1.6 KiB
JavaScript

(function( global, Popcorn ) {
var navigator = global.navigator;
// 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( navigator.userLanguage || navigator.language );
Popcorn.locale = {
// Popcorn.locale.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 );
}
}
}
};
})( this, this.Popcorn );