Skip to content

Commit

Permalink
Updated to latest master version of i18n plugin fork, see #16
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed May 1, 2013
1 parent b56de80 commit 9aed7ba
Showing 1 changed file with 159 additions and 161 deletions.
320 changes: 159 additions & 161 deletions contrib/i18n/i18n.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @license RequireJS i18n 2.0.1+ Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* @license RequireJS i18n 2.0.2 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/requirejs/i18n for details
*/
Expand Down Expand Up @@ -35,175 +35,173 @@
* for the nls/fr-fr/colors bundle to be that mixed in locale.
*/
(function () {
'use strict';

//PhET.SRR Added this code on 11/27/2012
//PhET.CM Modified this code on 11/28/2012
//Look for a query parameter of the form "locale=value", where the format of value is described in RFC 4646.
//The value of this parameter will be used to override the browser's language.
//Adapted from http://geekswithblogs.net/PhubarBaz/archive/2011/11/21/getting-query-parameters-in-javascript.aspx
var localeQueryParameter = (function () {
var value;
if ( typeof window != 'undefined' && window.location.search ) {
// look for first occurrence of "locale" query parameter
var params = window.location.search.slice( 1 ).split( "&" );
for ( var i = 0; i < params.length; i++ ) {
var nameValuePair = params[i].split( "=" );
if ( nameValuePair[0] === 'locale' ) {
value = decodeURI( nameValuePair[1] ).toLowerCase();
break;
}
}
}
return value;
}());

//regexp for reconstructing the master bundle name from parts of the regexp match
//nlsRegExp.exec("foo/bar/baz/nls/en-ca/foo") gives:
//["foo/bar/baz/nls/en-ca/foo", "foo/bar/baz/nls/", "/", "/", "en-ca", "foo"]
//nlsRegExp.exec("foo/bar/baz/nls/foo") gives:
//["foo/bar/baz/nls/foo", "foo/bar/baz/nls/", "/", "/", "foo", ""]
//so, if match[5] is blank, it means this is the top bundle definition.
var nlsRegExp = /(^.*(^|\/)nls(\/|$))([^\/]*)\/?([^\/]*)/;

//Helper function to avoid repeating code. Lots of arguments in the
//desire to stay functional and support RequireJS contexts without having
//to know about the RequireJS contexts.
function addPart(locale, master, needed, toLoad, prefix, suffix) {
if (master[locale]) {
needed.push(locale);
if (master[locale] === true || master[locale] === 1) {
toLoad.push(prefix + locale + '/' + suffix);
}
'use strict';

//PhET.SRR Added this code on 11/27/2012
//PhET.CM Modified this code on 11/28/2012
//Look for a query parameter of the form "locale=value", where the format of value is described in RFC 4646.
//The value of this parameter will be used to override the browser's language.
//Adapted from http://geekswithblogs.net/PhubarBaz/archive/2011/11/21/getting-query-parameters-in-javascript.aspx
var localeQueryParameter = (function () {
var value;
if ( typeof window != 'undefined' && window.location.search ) {
// look for first occurrence of "locale" query parameter
var params = window.location.search.slice( 1 ).split( "&" );
for ( var i = 0; i < params.length; i++ ) {
var nameValuePair = params[i].split( "=" );
if ( nameValuePair[0] === 'locale' ) {
value = decodeURI( nameValuePair[1] ).toLowerCase();
break;
}
}
}
return value;
}());

function addIfExists(req, locale, toLoad, prefix, suffix) {
var fullName = prefix + locale + '/' + suffix;
if (require._fileExists(req.toUrl(fullName))) {
toLoad.push(fullName);
}
//regexp for reconstructing the master bundle name from parts of the regexp match
//nlsRegExp.exec("foo/bar/baz/nls/en-ca/foo") gives:
//["foo/bar/baz/nls/en-ca/foo", "foo/bar/baz/nls/", "/", "/", "en-ca", "foo"]
//nlsRegExp.exec("foo/bar/baz/nls/foo") gives:
//["foo/bar/baz/nls/foo", "foo/bar/baz/nls/", "/", "/", "foo", ""]
//so, if match[5] is blank, it means this is the top bundle definition.
var nlsRegExp = /(^.*(^|\/)nls(\/|$))([^\/]*)\/?([^\/]*)/;

//Helper function to avoid repeating code. Lots of arguments in the
//desire to stay functional and support RequireJS contexts without having
//to know about the RequireJS contexts.
function addPart(locale, master, needed, toLoad, prefix, suffix) {
if (master[locale]) {
needed.push(locale);
if (master[locale] === true || master[locale] === 1) {
toLoad.push(prefix + locale + '/' + suffix);
}
}
}

/**
* Simple function to mix in properties from source into target,
* but only if target does not already have a property of the same name.
* This is not robust in IE for transferring methods that match
* Object.prototype names, but the uses of mixin here seem unlikely to
* trigger a problem related to that.
*/
function mixin(target, source, force) {
var prop;
for (prop in source) {
if (source.hasOwnProperty(prop) && (!target.hasOwnProperty(prop) || force)) {
target[prop] = source[prop];
} else if (typeof source[prop] === 'object') {
mixin(target[prop], source[prop], force);
}
}
function addIfExists(req, locale, toLoad, prefix, suffix) {
var fullName = prefix + locale + '/' + suffix;
if (require._fileExists(req.toUrl(fullName + '.js'))) {
toLoad.push(fullName);
}
}

define(['module'], function (module) {
var masterConfig = module.config ? module.config() : {};
/**
* Simple function to mix in properties from source into target,
* but only if target does not already have a property of the same name.
* This is not robust in IE for transferring methods that match
* Object.prototype names, but the uses of mixin here seem unlikely to
* trigger a problem related to that.
*/
function mixin(target, source, force) {
var prop;
for (prop in source) {
if (source.hasOwnProperty(prop) && (!target.hasOwnProperty(prop) || force)) {
target[prop] = source[prop];
} else if (typeof source[prop] === 'object') {
mixin(target[prop], source[prop], force);
}
}
}

return {
version: '2.0.1+',
/**
* Called when a dependency needs to be loaded.
*/
load: function (name, req, onLoad, config) {
config = config || {};
define(['module'], function (module) {
var masterConfig = module.config ? module.config() : {};

if (config.locale) {
masterConfig.locale = config.locale;
}
return {
version: '2.0.1+',
/**
* Called when a dependency needs to be loaded.
*/
load: function (name, req, onLoad, config) {
config = config || {};

var masterName,
match = nlsRegExp.exec(name),
prefix = match[1],
locale = match[4],
suffix = match[5],
parts = locale.split("-"),
toLoad = [],
value = {},
i, part, current = "";

//If match[5] is blank, it means this is the top bundle definition,
//so it does not have to be handled. Locale-specific requests
//will have a match[4] value but no match[5]
if (match[5]) {
//locale-specific bundle
prefix = match[1];
masterName = prefix + suffix;
} else {
//Top-level bundle.
masterName = name;
suffix = match[4];
locale = masterConfig.locale;
// console.log("preloacle",locale);
if (!locale) {
locale = masterConfig.locale =
typeof navigator === "undefined" ? "root" :
(navigator.language ||
navigator.userLanguage || "root").toLowerCase();


}
//PhET.CM added this block on 11/28/2012
//Override the browser's language using the optional query parameter.
if ( typeof localeQueryParameter === 'string' ) {
locale = localeQueryParameter;
}
parts = locale.split("-");
}
if (config.locale) {
masterConfig.locale = config.locale;
}

if (config.isBuild) {
//Check for existence of all locale possible files and
//require them if exist.
toLoad.push(masterName);
addIfExists(req, "root", toLoad, prefix, suffix);
for (i = 0; i < parts.length; i++) {
part = parts[i];
current += (current ? "-" : "") + part;
addIfExists(req, current, toLoad, prefix, suffix);
}

req(toLoad, function () {
onLoad();
});
} else {
//First, fetch the master bundle, it knows what locales are available.
req([masterName], function (master) {
//Figure out the best fit
var needed = [],
part;

//Always allow for root, then do the rest of the locale parts.
addPart("root", master, needed, toLoad, prefix, suffix);
for (i = 0; i < parts.length; i++) {
part = parts[i];
current += (current ? "-" : "") + part;
addPart(current, master, needed, toLoad, prefix, suffix);
}

//Load all the parts missing.
req(toLoad, function () {
var i, partBundle, part;
for (i = needed.length - 1; i > -1 && needed[i]; i--) {
part = needed[i];
partBundle = master[part];
if (partBundle === true || partBundle === 1) {
partBundle = req(prefix + part + '/' + suffix);
}
mixin(value, partBundle);
}

//All done, notify the loader.
onLoad(value);
});
});
}
var masterName,
match = nlsRegExp.exec(name),
prefix = match[1],
locale = match[4],
suffix = match[5],
parts = locale.split("-"),
toLoad = [],
value = {},
i, part, current = "";

//If match[5] is blank, it means this is the top bundle definition,
//so it does not have to be handled. Locale-specific requests
//will have a match[4] value but no match[5]
if (match[5]) {
//locale-specific bundle
prefix = match[1];
masterName = prefix + suffix;
} else {
//Top-level bundle.
masterName = name;
suffix = match[4];
locale = masterConfig.locale;
if (!locale) {
locale = masterConfig.locale =
typeof navigator === "undefined" ? "root" :
(navigator.language ||
navigator.userLanguage || "root").toLowerCase();
}
//PhET.CM added this block on 11/28/2012
//Override the browser's language using the optional query parameter.
if ( typeof localeQueryParameter === 'string' ) {
locale = localeQueryParameter;
}

parts = locale.split("-");
}

if (config.isBuild) {
//Check for existence of all locale possible files and
//require them if exist.
toLoad.push(masterName);
addIfExists(req, "root", toLoad, prefix, suffix);
for (i = 0; i < parts.length; i++) {
part = parts[i];
current += (current ? "-" : "") + part;
addIfExists(req, current, toLoad, prefix, suffix);
}

req(toLoad, function () {
onLoad();
});
} else {
//First, fetch the master bundle, it knows what locales are available.
req([masterName], function (master) {
//Figure out the best fit
var needed = [],
part;

//Always allow for root, then do the rest of the locale parts.
addPart("root", master, needed, toLoad, prefix, suffix);
for (i = 0; i < parts.length; i++) {
part = parts[i];
current += (current ? "-" : "") + part;
addPart(current, master, needed, toLoad, prefix, suffix);
}
};
});
}());

//Load all the parts missing.
req(toLoad, function () {
var i, partBundle, part;
for (i = needed.length - 1; i > -1 && needed[i]; i--) {
part = needed[i];
partBundle = master[part];
if (partBundle === true || partBundle === 1) {
partBundle = req(prefix + part + '/' + suffix);
}
mixin(value, partBundle);
}

//All done, notify the loader.
onLoad(value);
});
});
}
}
};
});
}());

0 comments on commit 9aed7ba

Please sign in to comment.