Skip to content

Commit 4c8ad43

Browse files
authored
Merge pull request #1442 from ruffiem/master
feat(i18n): angular i18n native implementation
2 parents 730fb41 + 37ac59d commit 4c8ad43

File tree

14 files changed

+92
-24
lines changed

14 files changed

+92
-24
lines changed

gulpfile.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ gulp.task('test', (done: any) =>
138138
'karma.run',
139139
done));
140140

141+
// --------------
142+
// Clean directories after i18n
143+
// TODO: find a better way to do it
144+
gulp.task('clean.i18n', (done: any) =>
145+
runSequence('clear.files',
146+
done));
147+
141148
// --------------
142149
// Clean dev/coverage that will only run once
143150
// this prevents karma watchers from being broken when directories are deleted

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"e2e": "protractor",
1818
"e2e.live": "protractor --elementExplorer",
1919
"gulp": "gulp",
20+
"i18n": "ng-xi18n && gulp clean.i18n",
2021
"lint": "gulp tslint",
2122
"karma": "karma",
2223
"karma.start": "karma start",
@@ -72,6 +73,7 @@
7273
"connect-livereload": "^0.5.4",
7374
"cssnano": "^3.7.3",
7475
"deep-extend": "^0.4.1",
76+
"del": "^2.2.2",
7577
"doiuse": "^2.4.1",
7678
"event-stream": "^3.3.3",
7779
"express": "~4.14.0",

src/client/app/about/about.component.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
<p>
2-
Angular 2 Seed is a starter project that implements best practices in
3-
coding, building and testing Angular 2 apps.
4-
</p>
1+
<p>Angular 2 Seed is a starter project that implements best practices in coding, building and testing Angular 2 apps.</p>
52

63
<h2>Features</h2>
74
<ul>

src/client/app/app.component.e2e-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('App', () => {
1717
});
1818

1919
it('should have correct nav text for About', () => {
20-
expect(element(by.css('sd-app sd-navbar nav a:last-child')).getText()).toEqual('ABOUT');
20+
expect(element(by.css('sd-app sd-navbar nav a:nth-child(2)')).getText()).toEqual('ABOUT');
2121
});
2222

2323
});

src/client/app/home/home.component.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<p>
2-
Howdy! Here's a list of awesome computer scientists. Do you know any others? Add to the list yourself.
3-
</p>
1+
<p>Howdy! Here's a list of awesome computer scientists. Do you know any others? Add to the list yourself.</p>
42

53
<form (submit)="addName()">
64
<input [(ngModel)]="newName" name="newName" placeholder="Awesome Computer Scientist">

src/client/app/i18n.providers.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID } from '@angular/core';
2+
3+
export class TranslationProviders {
4+
5+
public getTranslationFile = (): Promise<any> => {
6+
let noProviders: Object[] = [];
7+
8+
// Define a way to retrieve the local information
9+
let locale: string = 'en-US';
10+
11+
// Set the directory to the translation files
12+
let file: string = `../assets/locale/messages.${locale}.xlf`;
13+
14+
if(!locale || locale === 'en-US') return Promise.resolve(noProviders);
15+
16+
return new Promise(function (resolve, reject) {
17+
let xhr = new XMLHttpRequest;
18+
xhr.open('GET', file);
19+
xhr.onload = (data: any) => resolve(
20+
[
21+
{ provide: TRANSLATIONS, useValue: data.target.response },
22+
{ provide: TRANSLATIONS_FORMAT, useValue: 'xlf' },
23+
{ provide: LOCALE_ID, useValue: locale }
24+
]
25+
);
26+
xhr.onerror = () => reject(noProviders);
27+
xhr.send();
28+
});
29+
}
30+
};

src/client/app/main.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,17 @@
55
import { enableProdMode } from '@angular/core';
66
// The browser platform with a compiler
77
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
8+
// Load i18n providers
9+
// import { TranslationProviders } from './i18n.providers';
810

911
// The app module
1012
import { AppModule } from './app.module';
1113

1214
if (String('<%= ENV %>') === 'prod') { enableProdMode(); }
1315

14-
// Compile and launch the module
15-
platformBrowserDynamic().bootstrapModule(AppModule);
16-
17-
// In order to start the Service Worker located at "./worker.js"
18-
// uncomment this line. More about Service Workers here
19-
// https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
20-
//
21-
// if ('serviceWorker' in navigator) {
22-
// (<any>navigator).serviceWorker.register('./worker.js').then((registration: any) =>
23-
// console.log('ServiceWorker registration successful with scope: ', registration.scope))
24-
// .catch((err: any) =>
25-
// console.log('ServiceWorker registration failed: ', err));
26-
// }
16+
// Compile and launch the module with i18n providers
17+
// let TP = new TranslationProviders();
18+
// TP.getTranslationFile().then((providers: any) => {
19+
// const options: any = { providers };
20+
platformBrowserDynamic().bootstrapModule(AppModule/*, options*/);
21+
// });

src/client/app/shared/navbar/navbar.component.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ nav a {
1515
margin-right: 20px;
1616
text-decoration: none;
1717
vertical-align: middle;
18+
cursor: pointer;
1819
}
1920

2021
nav a.router-link-active {

src/client/app/shared/navbar/navbar.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ import { Component } from '@angular/core';
1010
styleUrls: ['navbar.component.css'],
1111
})
1212

13-
export class NavbarComponent {}
13+
export class NavbarComponent { }

src/client/app/system-config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
declare var System: SystemJSLoader.System;
22

33
System.config(JSON.parse('<%= SYSTEM_CONFIG_DEV %>'));
4-

tools/manual_typings/seed/del.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare module 'del' {
2+
var del: IDel;
3+
export = del;
4+
interface IDel {
5+
sync: {
6+
(patterns: any): void;
7+
};
8+
}
9+
}

tools/tasks/seed/app.through.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '../../../src/client/app/main';

tools/tasks/seed/clear.files.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as del from 'del';
2+
import { join } from 'path';
3+
4+
import Config from '../../config';
5+
6+
/**
7+
* Removes all the js, js.map and metadata.json from the src and tools directories
8+
*/
9+
export = () => {
10+
let source = [
11+
'gulpfile.js',
12+
'gulpfile.js.map',
13+
join(Config.TOOLS_DIR, '**/*.js'),
14+
join(Config.TOOLS_DIR, '**/*.js.map'),
15+
join(Config.TOOLS_DIR, '**/*.metadata.json'),
16+
join(Config.APP_SRC, '**/*.js'),
17+
join(Config.APP_SRC, '**/*.js.map'),
18+
join(Config.APP_SRC, '**/*.metadata.json')
19+
];
20+
21+
return del.sync(source);
22+
};

tools/tasks/seed/compile.ahead.prod.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ export = (done: any) => {
2828
return JSON.stringify(parsed, null, 2);
2929
});
3030
const args = argv;
31+
32+
// If a translation, tell the compiler
33+
if(args.lang) {
34+
args['i18nFile'] = `./src/client/assets/locale/messages.${args.lang}.xlf`;
35+
args['locale'] = args.lang;
36+
args['i18nFormat'] = 'xlf';
37+
}
38+
3139
const cliOptions = new tsc.NgcCliOptions(args);
3240
tsc.main(join(Config.TMP_DIR, Config.BOOTSTRAP_DIR), cliOptions, codegen)
3341
.then(done)
@@ -37,4 +45,3 @@ export = (done: any) => {
3745
process.exit(1);
3846
});
3947
};
40-

0 commit comments

Comments
 (0)