Commit Graph

73 Commits

Author SHA1 Message Date
Pete Bacon Darwin cf9a47ba53 feat(localize): allow duplicate messages to be handled during extraction (#38082)
Previously, the i18n message extractor just quietly ignored messages that
it extracted that had the same id. It can be helpful to identify these
to track down messages that have the same id but different message text.

Now the messages are checked for duplicate ids with different message text.
Any that are found can be reported based on the new `--duplicateMessageHandling`
command line option (or `duplicateMessageHandling` API options property).

* "ignore" - no action is taken
* "warning" - a diagnostic warning is written to the logger
* "error" - the extractor throws an error and exits

Fixes #38077

PR Close #38082
2020-07-16 13:53:38 -07:00
Pete Bacon Darwin 56dd3e77ac refactor(localize): remove unnecessary ! operator (#38082)
The messages property always exists so there is no need for the ! operator.

PR Close #38082
2020-07-16 13:53:38 -07:00
Pete Bacon Darwin 6b311552f0 fix(compiler-cli): ensure file_system handles mixed Windows drives (#37959)
The `fs.relative()` method assumed that the file-system is a single tree,
which is not the case in Windows, where you can have multiple drives,
e.g. `C:`, `D:` etc.

This commit changes `fs.relative()` so that it no longer forces the result
to be a `PathSegment` and then flows that refactoring through the rest of
the compiler-cli (and ngcc).  The main difference is that now, in some cases,
we needed to check whether the result is "rooted", i.e an `AbsoluteFsPath`,
rather than a `PathSegment`, before using it.

Fixes #36777

PR Close #37959
2020-07-13 12:05:21 -07:00
Pete Bacon Darwin ec32eba02c feat(localize): expose `canParse()` diagnostics (#37909)
When loading a translation file we ask each `TranslationParser`
whether it can parse the file. Occasionally, this check can find
errors in the file that would be useful to the developer. For example
if the file has invalid XML.

This commit deprecates the previous `canParse()` method and replaces it
with a new `analyze()` method. This returns an object that includes a
boolean `canParse` and then either a `hint` if it can parse the file,
or a `diagnostics` object filled with any messages that can be used to
diagnose problems with the format of the file.

Closes #37901

PR Close #37909
2020-07-06 13:56:39 -07:00
Pete Bacon Darwin 190561d8a6 feat(localize): implement message extraction tool (#32912)
This tool, which can be run from the node_modules bin folder, can parse
the source files in your compiled app and generate a translation file
formatted with the configured syntax.

For example:

```
./node_modules/.bin/localize-extract -s 'dist/**/*.js' -f xliff1 -o dist/messages.en.xlf
```

PR Close #32912
2020-06-25 14:10:03 -07:00
Pete Bacon Darwin ddb0a4e2e5 refactor(localize): handle location endings correctly (#32912)
Previously source locations required an ending position but this was not
being computed effectively. Now ending position is optional and it is
computed from an `endPath` passed to `getLocation()`.

PR Close #32912
2020-06-25 14:10:03 -07:00
Joey Perrott d1ea1f4c7f build: update license headers to reference Google LLC (#37205)
Update the license headers throughout the repository to reference Google LLC
rather than Google Inc, for the required license headers.

PR Close #37205
2020-05-26 14:26:58 -04:00
Pete Bacon Darwin 141fcb95a4 refactor(localize): use the `FileSystem` from ngtsc (#36843)
This commit makes the leap from its own custom baked `FileUtils`
solution to the fully formed `FileSystem` that is used in the compiler-cli.

This makes testing more straightforward and helps to ensure that the tool
will work across operatings systems.

Also, going forward, it will allow the localize project access to other useful
code from the compiler-cli, such as source-map handling.

PR Close #36843
2020-05-13 15:52:48 -07:00
Pete Bacon Darwin 70b25a3d4f fix(localize): ensure `getLocation()` works (#36853)
The `getLocation()` method was not working as there were typos in the
properties it was reading. This was not picked up because there were
neither typings for these properties nor unit tests to check it worked.

PR Close #36853
2020-05-05 11:51:46 -07:00
Pete Bacon Darwin 72f534f7f8 feat(localize): support merging multiple translation files (#36792)
Previously only one translation file per locale could be loaded.

Now the user can specify multiple files per locale, and the translations
from each of these files will be merged together by message id.
The merging is on a first-wins approach. So if to you have three files
to be merged:

```
['a.xlf', 'b.xmb', 'c.json']
```

Then any message from `a.xlf` will be used rather than a message from `b.xmb`
or `c.json` and so on. In practice this means that you should put the files
in order of most important first, with "fallback" translations later.

PR Close #36792
2020-05-01 09:46:12 -07:00
Pete Bacon Darwin 3cb9b43851 refactor(localize): hide merging diagnostics messages behind a method (#36792)
This commit introduces `Diagnostics.merge(other)` which will take the messages from
`other` and append them to the messages of `this`.

The translation loader is updated to use this new method.

PR Close #36792
2020-05-01 09:46:12 -07:00
Pete Bacon Darwin bf0c520f2e refactor(localize): simplify adding condition diagnostics (#36792)
Previously the `missingTranslation` option had to be checked before calling
`warn` or `error` on the `diagnostics` object.

Now this boilerplate is hidden inside the `Diagnostics.add()` method, which
will open it up to being used for other conditional diagnostics.

PR Close #36792
2020-05-01 09:46:11 -07:00
Pete Bacon Darwin 113c75dd27 refactor(localize): remove unused code (#36745)
PR Close #36745
2020-04-23 12:07:33 -04:00
Pete Bacon Darwin b7acf07a70 refactor(localize): consolidate message/translation metadata (#36745)
PR Close #36745
2020-04-23 12:07:33 -04:00
Pete Bacon Darwin 519f2baff0 test(localize): tidy up translation parser tests (#36745)
There was a lot of duplication and multiline backtick
strings that made it hard to maintain.

PR Close #36745
2020-04-23 12:07:33 -04:00
Pete Bacon Darwin 3962908d36 refactor(localize): tighten up recognition of simple JSON translations (#36745)
PR Close #36745
2020-04-23 12:07:33 -04:00
Pete Bacon Darwin afd2cbc3c4 refactor(localize): move `source_file_utils.ts` up (#36745)
This will allow the utilities in this file to be shared outside
`translate` code.

PR Close #36745
2020-04-23 12:07:33 -04:00
Pete Bacon Darwin 47f98673c7 fix(localize): include legacy ids when describing messages (#36761)
Previously, we only displayed the new `$localize` id, which is not
currently what most people have in their translation files.
Until people migrate to the new message id system it is confusing
not to display the legacy ids.

PR Close #36761
2020-04-22 16:31:47 -04:00
Joey Perrott 698b0288be build: reformat repo to new clang@1.4.0 (#36613)
PR Close #36613
2020-04-14 12:08:36 -07:00
Alan Agius aad02e8337 fix(localize): show helpful error when providing an invalid cli option (#36010)
PR Close #36010
2020-03-11 14:42:16 -04:00
Pete Bacon Darwin fc4c3c3eb5 fix(localize): merge translation from all XLIFF `<file>` elements (#35936)
XLIFF translation files can contain multiple `<file>` elements,
each of which contains translations. In ViewEngine all these
translations are merged into a single translation bundle.

Previously in Ivy only the translations from the last `<file>`
element were being loaded. Now all the translations from each
`<file>` are merged into a single translation bundle.

Fixes #35839

PR Close #35936
2020-03-09 13:06:05 -04:00
Pete Bacon Darwin 0e2a577b42 fix(localize): improve matching and parsing of XTB translation files (#35793)
This commit improves the `canParse()` method to check that the file is
valid XML and has the expected root node. Previously it was relying upon
a regular expression to do this.

PR Close #35793
2020-03-09 12:11:58 -04:00
Pete Bacon Darwin 08071e5634 fix(localize): improve matching and parsing of XLIFF 2.0 translation files (#35793)
Previously, the `Xliff2TranslationParser` only matched files that had a narrow
choice of extensions (e.g. `xlf`) and also relied upon a regular expression
match of an optional XML namespace directive.

This commit relaxes the requirement on both of these and, instead, relies
upon parsing the file into XML and identifying an element of the form
`<xliff version="2.0">` which is the minimal requirement for such files.

PR Close #35793
2020-03-09 12:11:58 -04:00
Pete Bacon Darwin 350ac11554 fix(localize): improve matching and parsing of XLIFF 1.2 translation files (#35793)
Previously, the `Xliff1TranslationParser` only matched files that had a narrow
choice of extensions (e.g. `xlf`) and also relied upon a regular expression
match of an optional XML namespace directive.

This commit relaxes the requirement on both of these and, instead, relies
upon parsing the file into XML and identifying an element of the form
`<xliff version="1.2">` which is the minimal requirement for such files.

PR Close #35793
2020-03-09 12:11:58 -04:00
Pete Bacon Darwin cd32085a75 refactor(localize): allow hints from `canParse()` to `parse()` (#35793)
This enables complex work to be done in `TranslationParser.canParse()`
without duplicating the work in `TranslationParser.parse()`.

PR Close #35793
2020-03-09 12:11:58 -04:00
Pete Bacon Darwin d88b237f1e refactor(localize): add support for TranslationParser diagnostics (#35793)
This modifies the internal (but shared with CLI) API for loading/parsing
translation files. Now the parsers will return a new `Diagnostics` object
along with any translations and locale extracted from the file.

It is up to the caller to decide what to do about this, if there are errors
it is suggested that an error is thrown, which is what the `TranslationLoader`
class does.

PR Close #35793
2020-03-09 12:11:58 -04:00
Pete Bacon Darwin 53f059ee8f fix(localize): improve placeholder mismatch error message (#35593)
The original error message was confusing since often it is the
translation that is at fault not the message.

PR Close #35593
2020-02-24 09:11:21 -08:00
Pete Bacon Darwin df75451a0c fix(localize): support minified ES5 `$localize` calls (#35562)
Some minification tooling modifies `$localize` calls
to contain a comma sequence of items, where the
cooked and raw values are assigned to variables.

This change improves our ability to recognize these
structures.

Fixes #35376

PR Close #35562
2020-02-20 10:55:53 -08:00
Pete Bacon Darwin 7069a83727 fix(localize): re-enable filename in code-frame error messages (#34994)
These were removed in 4c3a543f7f
due to a breaking change in an update to `babel-core`. See
dcf7d89b8e

This commit adds back in the filename to these messages.

Resolves https://github.com/angular/angular/pull/34974/files#r371034476

PR Close #34994
2020-01-27 12:25:40 -08:00
Igor Minar 4c3a543f7f test(localize): fix broken localize/translate tests (#34974)
the test failures most likely result from the babel updates in the previous commit.

it does look like we lost the file path from the error message, which is something that we

should follow up no in a separate change.

PR Close #34974
2020-01-26 14:53:47 -08:00
Igor Minar 7360e1ea2c build: add missing @babel/core bazel dep to fix //packages/localize/src/tools/test:test_lib (#34974)
Previously we relied on this package being hoisted and available, which is error prone and it would
be just a matter of time before the build would break due rehoisting of deps upon future npm package.json
changes.

PR Close #34974
2020-01-26 14:53:47 -08:00
Igor Minar a58034b316 build: add missing @babel/generator bazel dep to fix //packages/localize/src/tools/test:test (#34974)
It's not clear how this ever worked (npm hoisting is a suspect though), but this dep is required because
one of the tests imports @babel/generator in the ts sources.

The lack of this dep is breaking builds on the master branch.

More discussion about this issue on Slack: https://angular-team.slack.com/archives/C07DT5M6V/p1579934766007500

PR Close #34974
2020-01-26 14:53:46 -08:00
Greg Magolan aee67f08d9 test: handle bootstrap templated_args in jasmine_node_test defaults.bzl (#34736)
PR Close #34736
2020-01-15 14:58:07 -05:00
Greg Magolan dcff76e8b9 refactor: handle breaking changes in rules_nodejs 1.0.0 (#34736)
The major one that affects the angular repo is the removal of the bootstrap attribute in nodejs_binary, nodejs_test and jasmine_node_test in favor of using templated_args --node_options=--require=/path/to/script. The side-effect of this is that the bootstrap script does not get the require.resolve patches with explicitly loading the targets _loader.js file.

PR Close #34736
2020-01-15 14:58:07 -05:00
Greg Magolan 93c2df23bf build: upgrade to rules_nodejs 1.0.0 (first stable release) (#34736)
Brings in the fix for stamping which was preventing many targets from getting cached.

PR Close #34736
2020-01-15 14:58:07 -05:00
atscott e88d652f2a Revert "build: upgrade to rules_nodejs 1.0.0 (first stable release) (#34589)" (#34730)
This reverts commit cb6ffa1211.

PR Close #34730
2020-01-10 14:12:15 -08:00
atscott 538d0446b5 Revert "refactor: handle breaking changes in rules_nodejs 1.0.0 (#34589)" (#34730)
This reverts commit 9bb349e1c8.

PR Close #34730
2020-01-10 14:12:15 -08:00
atscott 5e60215470 Revert "test: handle bootstrap templated_args in jasmine_node_test defaults.bzl (#34589)" (#34730)
This reverts commit da4782e67f.

PR Close #34730
2020-01-10 14:12:15 -08:00
Greg Magolan da4782e67f test: handle bootstrap templated_args in jasmine_node_test defaults.bzl (#34589)
PR Close #34589
2020-01-10 08:31:59 -08:00
Greg Magolan 9bb349e1c8 refactor: handle breaking changes in rules_nodejs 1.0.0 (#34589)
The major one that affects the angular repo is the removal of the bootstrap attribute in nodejs_binary, nodejs_test and jasmine_node_test in favor of using templated_args --node_options=--require=/path/to/script. The side-effect of this is that the bootstrap script does not get the require.resolve patches with explicitly loading the targets _loader.js file.

PR Close #34589
2020-01-10 08:31:59 -08:00
Greg Magolan cb6ffa1211 build: upgrade to rules_nodejs 1.0.0 (first stable release) (#34589)
Brings in the fix for stamping which was preventing many targets from getting cached.

PR Close #34589
2020-01-10 08:31:58 -08:00
Pete Bacon Darwin 6ce940315b fix(ivy): i18n - remove `translate` function when clearing translations (#34346)
The `loadTranslations()` function will attach the `translate()` function
to `$localize.translate` to cause runtime translation to occur.

We should cleanup after ourselves by unattaching this function when
we call `clearTranslations()`.

Fixes #32781

PR Close #34346
2019-12-11 13:19:22 -08:00
Pete Bacon Darwin 8e96b450e2 refactor(ivy): i18n - store legacy message ids separately to custom ids (#34135)
This change will enable the Angular compiler to provide these legacy
message ids by default, which will solve problems with ngcc not knowing
whether to generate legacy ids or not.

PR Close #34135
2019-12-03 10:15:53 -08:00
Pete Bacon Darwin 80d326bd49 refactor(ivy): i18n - remove hack around TS tagged literals (#34135)
When first written there was no way to specify the raw text when
programmatically creating a template tagged literal AST node.

This is now fixed in TS and so the hack is no longer needed.

PR Close #34135
2019-12-03 10:15:52 -08:00
Pete Bacon Darwin d529b55651 fix(ivy): i18n - correctly parse XLIFF placeholders (#34155)
The ViewEngine translation extractor does not convert `-` to `_` for
placeholders that represent custom elements. For example `<app-component>`
gets converted to placeholders like `START_TAG_APP-COMPONENT`.

In `$localize` placeholders are expected to be snake-case, not kebab-case.
So we must normalize them when parsing a translation file that might have
been created via the View Engine translation extractor.

The `$localize` extraction code will normalize these placeholders when
creating translation files in the first place.

Fixes #34151

PR Close #34155
2019-12-02 11:24:48 -08:00
Igor Minar ea37e82e69 build: consolidate @angular-devkit/build-angular to the root package.json (#34002)
This allows us to update the version of the package in a single place for all tests.

Notable exemption of this is aio which currently doesn't depend on anything installed in the root.

PR Close #34002
2019-12-02 10:49:12 -08:00
Pete Bacon Darwin 936700ad9f fix(compiler): i18n - ignore `alt-trans` tags in XLIFF 1.2 (#33450)
The parser was accidentally reading the `target` tag
below the `alt-trans` target and overriding the correct
`target` tag.

(This already worked in `$localize` but a test has been
added to confirm.)

Fixes #33161

PR Close #33450
2019-10-29 11:49:32 -07:00
Pete Bacon Darwin 2c623fde16 feat(ivy): i18n - support inlining of XTB formatted translation files (#33444)
This commit implements the `XtbTranslationParser`, which can read XTB
formatted files.

PR Close #33444
2019-10-28 16:20:33 -07:00
Pete Bacon Darwin c2f13a1e3a refactor(ivy): i18n - share `MessageSerializer` across `TranslationParsers` (#33444)
Each of the XML based `TranslationParsers` was providing its own
`MessageSerializer`, but they are all very similar. So these have been
consolidated into a single more generic `MessageSerializer.

As a result of this, the extra layers of folders in the project seemed
unnecessary, so they have been flattened.

PR Close #33444
2019-10-28 16:20:33 -07:00
Pete Bacon Darwin 41979d6a27 fix(ivy): i18n - update `localize-translate` to accept target-locales (#33381)
The `localize-translate` command line tool can now accept an array of
target locales to support the case where translation files do not
contain them. Specify this array via the `--target-locales` option.

NOTE to early adopters: in order to support this, the original `-t`
option for the binary has changed from being a glob pattern to an array
of paths, which will have matching indices to any provided target-locales.

PR Close #33381
2019-10-28 10:09:15 -07:00