Giter Site home page Giter Site logo

franciscoknebel / ngx-translate-check Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 3.0 318 KB

CLI to test ngx-translate Angular projects to find missing and unused i18n strings.

License: MIT License

JavaScript 100.00%
angular angular2 cli ngx-translate i18n

ngx-translate-check's Introduction

ngx-translate-check

CLI to test an Angular project using ng-translate to find missing and unused i18n strings.

Issues

Prerequisites

  • Node & NPM

Installing

  $ npm install -g ngx-translate-check

Commands

  • files: Parses project files and i18n files to find amount of usage of i18n strings, outputting to stdout or a json file.

    • Output exports a JSON file, in the format below.
    • The input key shows all i18n strings found in your project .ts, .js and .html files, the amount of times they were found and in which files.
    • The translations key shows all the translation strings defined in each translation file.
    "files-output.json"
    {
      "input": {
        "string1": {
          "amount": 1,
          "files": [ "src/test1.html" ]
        },
        "string2": {
          "amount": 2,
          "files": [ "src/test1.html", "src/test2.html" ]
        },
      },
      "translations": {
        "path/to/translation/en.json": {
          "string1": 1,
          "string2": 1,
          "string3": 1
        },
        "path/to/translation/pt-BR.json": {
          "string1": 1,
          "string2": 1,
          "string4": 1
        },
        ...
      }
    }
    
  • diff: Receives a JSON file generated by the files command and shows difference between translation files (missing strings between files).

  • diff-input: Receives a JSON file generated by the files command and shows difference between translation and input files (strings in input files but not in translations, strings in translations but not in input files).

More info can be obtained by using the --help argument of the CLI.

Example command usage:

files

  • Wrote to output.json the data obtained from test-project, which includes all strings used in the project and all the strings defined in the translation files.

files

  • Used output.json from the files command to obtain the difference between the defined translation files.

TO-DO List

More commands

  • Difference between translation and project files.

  • Improve project file parser to obtain missing strings that the current regex does not catch.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

Initial project implementation by Francisco Knebel.

Project idea credits to Arthur Jacobs, who suffered along with Angular development, while we survived reading broken JSON files due to a missing comma.

See the full list of contributors who participated in this project.

License

MIT License. Click here for more information.

ngx-translate-check's People

Contributors

dependabot[bot] avatar franciscoknebel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ngx-translate-check's Issues

Error when using diff and diff-input options

I was trying the project and I couldn't use. files option work without any problem but other options give errors

SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at /usr/local/lib/node_modules/ngx-translate-check/bin/ngx-translate-check:24:30467
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:52:12)
SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at /usr/local/lib/node_modules/ngx-translate-check/bin/ngx-translate-check:24:29619
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:52:12)

Translations are not detected in *.ts files

Hello !

First, thanks a lot for your work, it allowed me to detect a ton of unused translations :)

I have quite a few translations that are handled directly in typescript files. As per the documentation (and some comments in the code), they should be detected, but they are not.

It comes from the parseFile method:

function parseFile(fileName) {
  const file = readFileSync(fileName, { encoding: 'utf8' });

  // May start with '{{' and end with '}}', but not necessary.
  // Internal string is separated by \' or \".
  // String is filled with multiple dot separated words.
  // After the string, whitespace with a pipe | separator.
  // After the operator, the translate pipe.
  // Ending the regex, whitespace and the optional '}}'.
  const matched = file.match(
    /({{)?( )*(\'|\")((\w|\-)\.?)*(\'|\")( )*\|( )*translate( )*(}})?/g
  );

  // Only return internal string of matches
  let foundStrings = [];
  if (matched && matched.length > 0) {
    foundStrings = matched.map(str => str.match(/(\'|\")(.*)?(\'|\")/)[2]);
  }
  return foundStrings;
}

The first and second regex are not compatible with following piece of code for example:

        private openErrorMessage(): void {
		const errorMessage: string = this.translateService.instant('ERROR_UNKNOWN');
		this.snackBar.open(errorMessage, null, { panelClass: 'error' });
	}

Another case where it doesn't work is when the translate pipe is on the next line in an html template.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.