Skip to content

Commit 3c8576f

Browse files
author
Chuck Dumont
authored
Merge pull request #311 from HeikoStudt/enable-i18n-tsc
Support for i18n written in typescript.
2 parents 3f46f02 + f8e0f96 commit 3c8576f

File tree

11 files changed

+133
-8
lines changed

11 files changed

+133
-8
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node/**
22
node_modules/**
33
test/js/**
4+
test/TestCases/loaders/i18nTypeScriptModules/nls/**
45
coverage/**
6+

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,36 @@ Note that you can also specify the require dependency array as a run-time identi
455455

456456
Dojo's [Auto-Require](https://dojotoolkit.org/documentation/tutorials/1.10/declarative/#auto-require) feature allows the parser to automatically require the modules for widgets that are declared by templates. This can be problematic with webpack for the reasons discussed [above](#use-of-run-time-identifiers-and-expressions-in-dependency-arrays), if your modules do not explicitly specify dependencies for the widgets that they contain. Although useful for prototyping and demo purposes, Dojo itself recommends against using Auto-Require for production code because of it's negative performance consequences, and to instead be explicit about your application's dependencies.
457457

458+
# I18n and TypeScript
459+
dojo-webpack-plugin has support for i18n using typescript. This helps for example Jetbrains IDEA to support syntax completion.
460+
461+
Please refer to [this blog](https://www.sitepen.com/blog/what-typescript-can-offer-to-dojo-1-x) for an introductory example.
462+
463+
The following i18n file format supports dialects as well:
464+
465+
nls/main.ts:
466+
467+
```typescript
468+
export = {
469+
'de-de' : true,
470+
root : { users: "Users" }
471+
}
472+
```
473+
474+
nls/de-de/main.ts:
475+
```typescript
476+
export = { users: "Benutzer" }
477+
```
478+
479+
Note: We need to import our i18n-language files explicitly inside our application, so that it is bundled with webpack.
480+
```javascript
481+
import 'nls/de-de/main';
482+
```
458483
# Dependency requirements
459484

485+
Note: There is dojo-magic around the file extension .js in the imports. Please do remove the extension as .ts is not treated like that.
486+
487+
460488
**dojo-webpack-plugin** has a peer dependency on webpack. **dojo-webpack-plugin**'s webpack dependencies must resolve to the same modules as your application's webpack dependencies, otherwise you may encounter errors similar to the following when building.
461489

462490
```

loaders/dojo/i18nEval.js

+27-8
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,45 @@
1515
* limitations under the License.
1616
*/
1717

18-
/*
19-
* Evaluates a Dojo i18n resource bundle and returns the bundle object
20-
*/
18+
/*
19+
* Evaluates a Dojo i18n resource bundle and returns the bundle object
20+
*/
2121
module.exports = function(bundle) {
2222
var result, isAmd;
2323
function define(arg1, arg2) {
2424
isAmd = true;
25+
2526
if (!arg2) {
27+
// overwriting language files may define an easy object
2628
result = arg1;
27-
} else {
28-
if (arg1.length !== 0) {
29-
throw new Error("define dependencies not supported in langauge files!");
30-
}
29+
30+
} else if (arg1.length === 0) {
31+
32+
// without dependencies we can easily call the empty factory function
3133
result = arg2(); // call factory function
34+
35+
} else if (arg1.length === 2 && arg1[0] === "require" && arg1[1] === "exports") {
36+
37+
// Special case for typescript generated i18n files
38+
// tsc is adding virtual dependencies for require and exports
39+
// we need the latter as our result
40+
var exp = {};
41+
var requireFun = function() { throw new Error("require() is not supported in language files"); };
42+
result = arg2(requireFun, exp);
43+
if (typeof result === 'undefined') {
44+
result = exp;
45+
}
46+
47+
} else {
48+
throw new Error("define dependencies not supported in langauge files!");
3249
}
3350
}
51+
3452
define.amd = true;
3553
eval(bundle);
3654
if (!isAmd) {
3755
throw new Error("Non-AMD nls bundles are not supported by dojo-webpack-plugin");
3856
}
57+
3958
return result;
40-
};
59+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# What is the test about?
2+
typescript generates with the settings AMD/es2015 module defines having two
3+
parameters, require and exports. The exports weill be filled with the exported
4+
data.
5+
6+
This needs to be addressed by dojo-webpack-plugin.
7+
8+
# How to generate out of the *.ts typescript files?
9+
Install Typescript (>3) and call tsc here.
10+
11+
# EsLintIgnore
12+
The generated files nls/* are ignored for eslint, because otherwise
13+
no-unused-vars will error.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
define(["dojo/i18n!./nls/strings"], function(strings) {
2+
it("should load the strings if generated with typescript (require, exports)", function() {
3+
strings.hello.should.be.eql("xyz-de");
4+
});
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define(["require", "exports"], function (require, exports) {
2+
"use strict";
3+
return {
4+
hello: 'xyz-de'
5+
};
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define(["require", "exports"], function (require, exports) {
2+
"use strict";
3+
return {
4+
'de-at': true,
5+
root: {
6+
hello: 'xyz'
7+
}
8+
};
9+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export = {
2+
hello : 'xyz-de'
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export = {
2+
'de-at' : true,
3+
root : {
4+
hello : 'xyz'
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./nls",
4+
"rootDir": "./ts-base",
5+
"baseUrl": ".",
6+
"strict": true,
7+
"strictNullChecks" : true,
8+
9+
"moduleResolution": "node",
10+
"module": "AMD",
11+
"target": "ES2015"
12+
},
13+
14+
"files": [
15+
"ts-base/strings.ts",
16+
"ts-base/de-at/strings.ts"
17+
]
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var path = require("path");
2+
var DojoWebpackPlugin = require("../../../../index");
3+
module.exports = {
4+
entry: "test/index",
5+
plugins: [
6+
new DojoWebpackPlugin({
7+
loaderConfig: {
8+
paths:{test: "."},
9+
locale: "de-at",
10+
has: {"host-browser": 0, "dojo-config-api": 0}
11+
},
12+
locales: ["en", "de-at"],
13+
loader: path.join(__dirname, "../../../js/dojo/dojo.js")
14+
})
15+
]
16+
};

0 commit comments

Comments
 (0)