Skip to content

feat(i18n): angular i18n native implementation #1442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 10, 2016
Merged
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
7 changes: 7 additions & 0 deletions gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ gulp.task('test', (done: any) =>
'karma.run',
done));

// --------------
// Clean directories after i18n
// TODO: find a better way to do it
gulp.task('clean.i18n', (done: any) =>
runSequence('clear.files',
done));

// --------------
// Clean dev/coverage that will only run once
// this prevents karma watchers from being broken when directories are deleted
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"e2e": "protractor",
"e2e.live": "protractor --elementExplorer",
"gulp": "gulp",
"i18n": "ng-xi18n && gulp clean.i18n",
"lint": "gulp tslint",
"karma": "karma",
"karma.start": "karma start",
Expand Down Expand Up @@ -72,6 +73,7 @@
"connect-livereload": "^0.5.4",
"cssnano": "^3.7.3",
"deep-extend": "^0.4.1",
"del": "^2.2.2",
"doiuse": "^2.4.1",
"event-stream": "^3.3.3",
"express": "~4.14.0",
Expand Down
5 changes: 1 addition & 4 deletions src/client/app/about/about.component.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<p>
Angular 2 Seed is a starter project that implements best practices in
coding, building and testing Angular 2 apps.
</p>
<p>Angular 2 Seed is a starter project that implements best practices in coding, building and testing Angular 2 apps.</p>

<h2>Features</h2>
<ul>
Expand Down
2 changes: 1 addition & 1 deletion src/client/app/app.component.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('App', () => {
});

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

});
4 changes: 1 addition & 3 deletions src/client/app/home/home.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<p>
Howdy! Here's a list of awesome computer scientists. Do you know any others? Add to the list yourself.
</p>
<p>Howdy! Here's a list of awesome computer scientists. Do you know any others? Add to the list yourself.</p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some more examples on use (like how do we can translate numbers and variables and/or plurals)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, totally, but it seems like pluralization is not working well (especially 'other'). plnkr


<form (submit)="addName()">
<input [(ngModel)]="newName" name="newName" placeholder="Awesome Computer Scientist">
Expand Down
30 changes: 30 additions & 0 deletions src/client/app/i18n.providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID } from '@angular/core';

export class TranslationProviders {

public getTranslationFile = (): Promise<any> => {
let noProviders: Object[] = [];

// Define a way to retrieve the local information
let locale: string = 'en-US';

// Set the directory to the translation files
let file: string = `../assets/locale/messages.${locale}.xlf`;

if(!locale || locale === 'en-US') return Promise.resolve(noProviders);

return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest;
xhr.open('GET', file);
xhr.onload = (data: any) => resolve(
[
{ provide: TRANSLATIONS, useValue: data.target.response },
{ provide: TRANSLATIONS_FORMAT, useValue: 'xlf' },
{ provide: LOCALE_ID, useValue: locale }
]
);
xhr.onerror = () => reject(noProviders);
xhr.send();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use the Angular2 HTTP provider here instead of XMLHttpRequest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried but it was resolving to undefined maybe I was missing something...

Copy link
Owner

@mgechev mgechev Oct 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bigous this service is invoked before bootstrap so I'm not sure if it is possible since the Angular's i18n functionality is part of the compilation process (in this case JiT).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgechev True. It should be a service where the Application module depends so... or keep it as it is :)

}
};
21 changes: 8 additions & 13 deletions src/client/app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@
import { enableProdMode } from '@angular/core';
// The browser platform with a compiler
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
// Load i18n providers
// import { TranslationProviders } from './i18n.providers';

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

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

// Compile and launch the module
platformBrowserDynamic().bootstrapModule(AppModule);

// In order to start the Service Worker located at "./worker.js"
// uncomment this line. More about Service Workers here
// https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
//
// if ('serviceWorker' in navigator) {
// (<any>navigator).serviceWorker.register('./worker.js').then((registration: any) =>
// console.log('ServiceWorker registration successful with scope: ', registration.scope))
// .catch((err: any) =>
// console.log('ServiceWorker registration failed: ', err));
// }
// Compile and launch the module with i18n providers
// let TP = new TranslationProviders();
// TP.getTranslationFile().then((providers: any) => {
// const options: any = { providers };
platformBrowserDynamic().bootstrapModule(AppModule/*, options*/);
// });
1 change: 1 addition & 0 deletions src/client/app/shared/navbar/navbar.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ nav a {
margin-right: 20px;
text-decoration: none;
vertical-align: middle;
cursor: pointer;
}

nav a.router-link-active {
Expand Down
2 changes: 1 addition & 1 deletion src/client/app/shared/navbar/navbar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ import { Component } from '@angular/core';
styleUrls: ['navbar.component.css'],
})

export class NavbarComponent {}
export class NavbarComponent { }
1 change: 0 additions & 1 deletion src/client/app/system-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
declare var System: SystemJSLoader.System;

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

9 changes: 9 additions & 0 deletions tools/manual_typings/seed/del.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module 'del' {
var del: IDel;
export = del;
interface IDel {
sync: {
(patterns: any): void;
};
}
}
1 change: 1 addition & 0 deletions tools/tasks/seed/app.through.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../../../src/client/app/main';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruffiem this file doesn't seem used anywhere, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgechev it's used by ng-xi18n as an entry point to the application. Since ng-xi18n relies on the tsconfig.json and src folder it excluded, I needed to find a way in 🙄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it. Can you pass src/client/tsconfig.json as argument of ng-xi18n? For instance in ngc it'll be: ngc -p src/client/tsconfig.json.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no -p option with ng-xi18n yet but it's being discussed on the angular repo (2201).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I added this as last step in order to not pollute the seed's tasks directory.

22 changes: 22 additions & 0 deletions tools/tasks/seed/clear.files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as del from 'del';
import { join } from 'path';

import Config from '../../config';

/**
* Removes all the js, js.map and metadata.json from the src and tools directories
*/
export = () => {
let source = [
'gulpfile.js',
'gulpfile.js.map',
join(Config.TOOLS_DIR, '**/*.js'),
join(Config.TOOLS_DIR, '**/*.js.map'),
join(Config.TOOLS_DIR, '**/*.metadata.json'),
join(Config.APP_SRC, '**/*.js'),
join(Config.APP_SRC, '**/*.js.map'),
join(Config.APP_SRC, '**/*.metadata.json')
];

return del.sync(source);
};
9 changes: 8 additions & 1 deletion tools/tasks/seed/compile.ahead.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export = (done: any) => {
return JSON.stringify(parsed, null, 2);
});
const args = argv;

// If a translation, tell the compiler
if(args.lang) {
args['i18nFile'] = `./src/client/assets/locale/messages.${args.lang}.xlf`;
args['locale'] = args.lang;
args['i18nFormat'] = 'xlf';
}

const cliOptions = new tsc.NgcCliOptions(args);
tsc.main(join(Config.TMP_DIR, Config.BOOTSTRAP_DIR), cliOptions, codegen)
.then(done)
Expand All @@ -37,4 +45,3 @@ export = (done: any) => {
process.exit(1);
});
};