Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/attempt-loading/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/out
40 changes: 40 additions & 0 deletions examples/attempt-loading/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Example of i18n bundles not specified in the master module
==========================================================

Specifying all available bundles in the master file when a product is released
may not be convenient. If a localization is provided afterwards, the master
module has to be modified, which gets even more difficult when it is compiled
to a single output file.

This sample does not specify any locales in the master file, which makes the
i18n plugin attempt loading the bundles from the nls directory during the
execution time dynamically.

nls/strings.js
define({ "root": true }); // "empty" master
nls/root/strings.js
define({ English localization });
nls/de/strings.js
define({ German localization });

Otherwise the usage of bundles in the source code is the same.

Testing the example
-------------------

Make sure that you clone the [`requirejs`](https://github.com/jrburke/r.js.git)
and [`r.js`](https://github.com/jrburke/requirejs.git) directories as siblings
of the `i18n` directory where you cloned the i18n plugin. Some files are loaded
by relative parts from them (`require.js and r.js).

The debug version of this example, which loads every module separately, can
be tested by the following commands:

open index-debug.html in the web browser

The release version of this example, which loads all modules compiled to the
single file `zoo.js`, can be compiled and tested by the following commands:

cd build
node ../../../../r.js/dist/r.js -o build.js
open out/index-release.html in the web browser
15 changes: 15 additions & 0 deletions examples/attempt-loading/build/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
appDir: "..",
baseUrl: ".",
mainConfigFile: "../config.js",
dir: "out",

optimize: "none",

modules: [
{
name: "zoo",
include: [ "../../../../../requirejs/require" ]
}
]
}
5 changes: 5 additions & 0 deletions examples/attempt-loading/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require.config({
paths: {
i18n: "../../i18n"
}
});
25 changes: 25 additions & 0 deletions examples/attempt-loading/index-debug.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>ZOO</title>

<script src="../../../requirejs/require.js"></script>
<script src="config.js"></script>
<script src="main.js"></script>

</head>

<body>

<p>Open the console to watch the life.</p>

</body>

</html>

24 changes: 24 additions & 0 deletions examples/attempt-loading/index-release.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>ZOO</title>

<script src="zoo.js"></script>
<script src="main.js"></script>

</head>

<body>

<p>Open the console to watch the life.</p>

</body>

</html>

12 changes: 12 additions & 0 deletions examples/attempt-loading/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require.config({
config: {
i18n: { locale: "de-de" }
}
});

require(["model/animal"], function (Animal) {

var cat = new Animal('Cat');
cat.die();

});
20 changes: 20 additions & 0 deletions examples/attempt-loading/model/animal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
define(["i18n!nls/strings"], function (strings) {

function Animal(name) {
this.name = name || "Unknown";
console.log(strings.AnimalBorn.replace("{0}", this.name));
}

Animal.prototype = {

name: undefined,

die: function () {
console.log(strings.AnimalDied.replace("{0}", this.name));
}

};

return Animal;

});
4 changes: 4 additions & 0 deletions examples/attempt-loading/nls/cs/strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
define({
AnimalBorn: "{0} se narodilo.",
AnimalDied: "{0} zemřelo."
});
4 changes: 4 additions & 0 deletions examples/attempt-loading/nls/de/strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
define({
AnimalBorn: "Das {0} ist geboren.",
AnimalDied: "Das {0} ist gestorben."
});
4 changes: 4 additions & 0 deletions examples/attempt-loading/nls/root/strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
define({
AnimalBorn: "The {0} was born.",
AnimalDied: "The {0} died."
});
3 changes: 3 additions & 0 deletions examples/attempt-loading/nls/strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define({
"root": true
});
1 change: 1 addition & 0 deletions examples/attempt-loading/zoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
define("zoo", ["model/animal"]);
61 changes: 48 additions & 13 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,32 +149,67 @@
//First, fetch the master bundle, it knows what locales are available.
req([masterName], function (master) {
//Figure out the best fit
var needed = [],
var needed = [], unknown = [],
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);
if (master[current] !== undefined) {
addPart(current, master, needed, toLoad,
prefix, suffix);
} else {
unknown.push(current);
}
}

//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);
function loadKnownParts() {
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);
}
mixin(value, partBundle);

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

//Try loading one by one parts not specified in the
//master module and when all were processed, load the
//known rest and build the requested bundle.
function loadUnknownParts() {
current = unknown.shift();
if (current) {
req([ prefix + current + '/' + suffix ],
function () {
needed.push(current);
master[current] = true;
loadUnknownParts();
}, function () {
master[current] = false;
loadUnknownParts();
});
} else {
loadKnownParts();
}
}

//All done, notify the loader.
onLoad(value);
});
//If at least one of the fallback locales was specified
// in the master module, do not try the unspecified.
if (needed.length > 1) {
loadKnownParts();
} else {
loadUnknownParts();
}
});
}
}
Expand Down