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

feat(i18n): angular i18n native implementation #1442

merged 14 commits into from
Oct 10, 2016

Conversation

ruffiem
Copy link
Contributor

@ruffiem ruffiem commented Oct 8, 2016

  • angular i18n native implementation for JiT processing
  • added systemjs-text-plugin to allow systemjs deal with XLIFF files
  • application entry point at app.through.ts in seed/tasks folder
  • npm run i18n task to extract content and create former messages.xlf
  • translation files to be stored in src/client/assets/locale

@mgechev
Copy link
Owner

mgechev commented Oct 8, 2016

@ruffiem I took only a brief look at your changes but it looks great so far! Thanks for the PR, I'll merge the it tonight or tomorrow morning.

@ruffiem
Copy link
Contributor Author

ruffiem commented Oct 8, 2016

Thanks! Just saw there's something wrong with the production build, it completes but breaks at runtime on "System.import is not a function" in i18n.providers.ts. 😠

@ruffiem
Copy link
Contributor Author

ruffiem commented Oct 8, 2016

Working on the issue, hope to fix it by the end of the day. Does systemjs text plugin may cause such error ?

@mgechev
Copy link
Owner

mgechev commented Oct 8, 2016

Yes, in production we are excluding SystemJS from the bundle. I'd recommend you to use the Angular's http service instead. Otherwise it looks good to me!

@ruffiem
Copy link
Contributor Author

ruffiem commented Oct 8, 2016

Great, thanks. I didn't know System.import was just an xhr :) I will refactor a little, run a few tests and push when everything seems correct.

@mgechev
Copy link
Owner

mgechev commented Oct 8, 2016

@ruffiem another small thing - we also support AoT compilation so you'd have to update main-prod.ts because it has different bootstrap logic.

@ruffiem
Copy link
Contributor Author

ruffiem commented Oct 9, 2016

Ok now it does work on both dev and prod but we have a bigger challenge : when scanning the code for i18n attributes, angular gives a specific id based on the textnode format (white spaces, returns, breaking spaces...). Thing is, with minification/uglification we lose the format, the id is changing and the translation is lost. Can't think of anything "easy" to fix it right now.

EDIT: Or maybe we can minify the XLF as well to have the same formatting ?

@mgechev
Copy link
Owner

mgechev commented Oct 9, 2016

The id is changed by uglify performing mangling? If so, we can disable mangling for build.prod and leave it only for build.prod.exp where we may not have such problem because of the AoT.

@ruffiem
Copy link
Contributor Author

ruffiem commented Oct 9, 2016

Exactly.

Also, mangling the XLF is not possible since ids are hardcoded in the file.

I created an issue on the angular repo as they should rely on content instead of formatting IMO.

@ruffiem
Copy link
Contributor Author

ruffiem commented Oct 9, 2016

I have updated the text content to avoid translation to break from the seed on prod, ids won't change but the text has to be written inline.

<h1 i18n>Howdy!</h1>

not

<h1 i18n>
     Howdy
!
</h1>

Also, TP from main.ts were causing ng-xi18n fn to break. It's fixed now.

@mgechev
Copy link
Owner

mgechev commented Oct 9, 2016

@ruffiem you can also disable mangling, which should be fine.

@ruffiem
Copy link
Contributor Author

ruffiem commented Oct 9, 2016

I have tried to disable mangling but the result was the same (except that the ids were changing but still not matching the ones from the XLF). Did you try too ?

@mgechev
Copy link
Owner

mgechev commented Oct 9, 2016

@ruffiem I'll give it a try later today. I'm releasing beta.1 of codelyzer today with bug fixes.

@mgechev mgechev changed the title (enhancement) i18n: angular i18n native implementation feat(i18n): angular i18n native implementation Oct 9, 2016
@mgechev
Copy link
Owner

mgechev commented Oct 9, 2016

I took a final look. It looks great, thanks for the effort one more time. My suggestion will be to keep it disabled by default because a lot of people will be confused by the bootstrap process.

Lets keep main.ts like this:

/**
 * Bootstraps the application and makes the ROUTER_PROVIDERS and the APP_BASE_HREF available to it.
 * @see https://angular.io/docs/ts/latest/api/platform-browser-dynamic/index/bootstrap-function.html
 */
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 with i18n providers
// let TP = new TranslationProviders();
// TP.getTranslationFile().then((providers: any) => {
//   const options: any = { providers };
platformBrowserDynamic().bootstrapModule(AppModule/*, options*/);
// });

I've also removed the comments for the Service Workers that I added in the past because they are out of date. We can add a wiki article of how the i18n can be enabled (basically uncomment the TranslationProviders and the xlf file generation).

Finally commenting in the language switching logic. Currently directly accessing window and localStorage will prevent the app from running in Web Workers, so it might be better to not have this in master.

What do you think about this?

@ruffiem
Copy link
Contributor Author

ruffiem commented Oct 9, 2016

Sounds great, I will make a wiki page about how to use i18n if you allow me to. And thanks to you for the seed, do not hesitate to tell me if you need help or something 👍

@mgechev
Copy link
Owner

mgechev commented Oct 9, 2016

Sounds good! Looking for the updates in the PR and the wiki page, then I'll be ready to merge.

@ruffiem
Copy link
Contributor Author

ruffiem commented Oct 10, 2016

Ok, should be fine now, I've also updated the package.json to match current master.

@ruffiem
Copy link
Contributor Author

ruffiem commented Oct 10, 2016

Since i18n is an option, maybe we should create a few options in project.config.ts seed.config.ts. The language directory is hardcoded in the AoT task which is not a great option 👎 what do you think ?

Copy link
Contributor

@Bigous Bigous left a comment

Choose a reason for hiding this comment

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

Great Job @ruffiem . Just a minor change and a wish :D

);
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 :)

<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

@mgechev mgechev merged commit 4c8ad43 into mgechev:master Oct 10, 2016
@mgechev
Copy link
Owner

mgechev commented Oct 10, 2016

@ruffiem good wiki page!

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants