Angular 2 internalization (ng-xi18n tool) without code compilation

1.5k Views Asked by At

I create i18n files using ng-xi18n tool. When I execute "./node_modules/.bin/ng-xi18n" messages.xlf file is created. The problem is that all code is compiled. js, js.map, metadata.json files are created, I don't need them. Hoe to create messages.xlf without js, js.map and metadta.json files?

Here is my tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "noEmitHelpers": true,
    "strictNullChecks": false,
    "baseUrl": "./src",
    "paths": {
    },
    "lib": [
      "dom",
      "es6"
    ],
    "types": [
      "hammerjs",
      "jasmine",
      "node",
      "protractor",
      "selenium-webdriver",
      "source-map",
      "uglify-js",
      "webpack"
    ]
  },
  "exclude": [
    "node_modules",
    "dist"
  ],
  "awesomeTypescriptLoaderOptions": {
    "forkChecker": true,
    "useWebpackText": true
  },
  "compileOnSave": false,
  "buildOnSave": false,
  "atom": { "rewriteTsconfig": false }
}
1

There are 1 best solutions below

1
On

This is actually an open issue in the tool:

https://github.com/angular/angular/issues/13567

As a workaround, I've implemented an npm script to run ng-xi18n and then remove the generated files.

In your package.json add the following scripts section:

"scripts": {
    "i18n": "ng-xi18n && npm run clean:i18n",
    "clean:i18n": "rimraf ClientApp/**/*.js ClientApp/**/*.js.map ClientApp/**/*.metadata.json"
}

Then you can issue npm run i18n to perform the extraction and cleanup.

Notes:

  • In my setup, I've all the angular related code in a ClientApp folder at the root of the project folder, hence I'm targeting the deletions starting from that folder.
  • I'm not depending on any .js files in that folder, therefore I can delete all of them safely. Take that into account if you have some javascript files that you want to preserve and adjust the globbing pattern accordingly.
  • You will need to add rimraf as a dependency (or dev dependency preferably) for it to work.

Hope that helps until the bug gets fixed.

Cheers!