@rushstack/webpack4-localization-plugin 0.18.9 → 0.18.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -24
- package/README.md +292 -292
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/AssetProcessor.js.map +1 -1
- package/lib/LocalizationPlugin.js.map +1 -1
- package/lib/WebpackConfigurationUpdater.js.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/interfaces.js.map +1 -1
- package/lib/loaders/InPlaceLocFileLoader.js.map +1 -1
- package/lib/loaders/LoaderFactory.js.map +1 -1
- package/lib/loaders/LocLoader.js.map +1 -1
- package/lib/utilities/Constants.js.map +1 -1
- package/lib/utilities/EntityMarker.js.map +1 -1
- package/lib/utilities/LoaderTerminalProvider.js.map +1 -1
- package/lib/webpackInterfaces.js.map +1 -1
- package/package.json +5 -5
package/LICENSE
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
@rushstack/webpack4-localization-plugin
|
|
2
|
-
|
|
3
|
-
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
4
|
-
|
|
5
|
-
MIT License
|
|
6
|
-
|
|
7
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
|
8
|
-
a copy of this software and associated documentation files (the
|
|
9
|
-
"Software"), to deal in the Software without restriction, including
|
|
10
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
|
12
|
-
permit persons to whom the Software is furnished to do so, subject to
|
|
13
|
-
the following conditions:
|
|
14
|
-
|
|
15
|
-
The above copyright notice and this permission notice shall be
|
|
16
|
-
included in all copies or substantial portions of the Software.
|
|
17
|
-
|
|
18
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
22
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
23
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
24
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1
|
+
@rushstack/webpack4-localization-plugin
|
|
2
|
+
|
|
3
|
+
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
4
|
+
|
|
5
|
+
MIT License
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
8
|
+
a copy of this software and associated documentation files (the
|
|
9
|
+
"Software"), to deal in the Software without restriction, including
|
|
10
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
12
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
13
|
+
the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be
|
|
16
|
+
included in all copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
22
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
23
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
24
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,292 +1,292 @@
|
|
|
1
|
-
# @rushstack/webpack4-localization-plugin
|
|
2
|
-
|
|
3
|
-
## Installation
|
|
4
|
-
|
|
5
|
-
`npm install @rushstack/webpack4-localization-plugin --save-dev`
|
|
6
|
-
|
|
7
|
-
## Overview
|
|
8
|
-
|
|
9
|
-
This Webpack plugin produces bundles that have multiple locales' variants of strings embedded. It also
|
|
10
|
-
has out-of-box support for RESX files in addition to JSON strings files (with the extension `.loc.json`), including
|
|
11
|
-
support for generating typings.
|
|
12
|
-
|
|
13
|
-
### Example Plugin Usage
|
|
14
|
-
|
|
15
|
-
There are three example projects in this repository that make use of this plugin:
|
|
16
|
-
|
|
17
|
-
- [Project 1](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-01)
|
|
18
|
-
- This project contains two webpack entrypoints (one with an async chunk, one without), without any localized
|
|
19
|
-
resources
|
|
20
|
-
- The output is a single, non-localized variant
|
|
21
|
-
- [Project 2](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02)
|
|
22
|
-
- This project contains three webpack entrypoints:
|
|
23
|
-
- [`indexA.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02/src/indexA.ts)
|
|
24
|
-
directly references two `.loc.json` files and one `.resx` file, and dynamically imports an async chunk with
|
|
25
|
-
localized data, and an async chunk without localized data
|
|
26
|
-
- [`indexB.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02/src/indexB.ts)
|
|
27
|
-
directly references two `.loc.json` files
|
|
28
|
-
- [`indexC.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02/src/indexC.ts)
|
|
29
|
-
directly references no localized resources, and dynamically imports an async chunk without localized data
|
|
30
|
-
- The webpack config contains and references Spanish translations for most of the English strings in the resource files
|
|
31
|
-
- The output contains English, Spanish, and "passthrough" localized variants of files that contain
|
|
32
|
-
localized data, and a non-localized variant of the files that do not contain localized data
|
|
33
|
-
- [Project 3](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-03)
|
|
34
|
-
- This project contains four webpack entrypoints:
|
|
35
|
-
- [`indexA.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-03/src/indexA.ts)
|
|
36
|
-
directly references one `.loc.json` file, one `.resx.json` file, one `.resx` file, and one `.resjson` file, and dynamically imports an async chunk with
|
|
37
|
-
localized data, and an async chunk without localized data
|
|
38
|
-
- [`indexB.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-03/src/indexB.ts)
|
|
39
|
-
directly references one `.loc.json` file and one `.resx.json` file
|
|
40
|
-
- [`indexC.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-03/src/indexC.ts)
|
|
41
|
-
directly references no localized resources, and dynamically imports an async chunk with localized data
|
|
42
|
-
- [`indexD.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-03/src/indexD.ts)
|
|
43
|
-
directly references no localized resources, and dynamically imports an async chunk without localized data
|
|
44
|
-
- The webpack config contains or references Spanish translations for some of the English strings in the resource files
|
|
45
|
-
- The output contains English, Spanish, "passthrough," and two pseudo-localized variants of files that contain
|
|
46
|
-
localized data, and a non-localized variant of the files that do not contain localized data
|
|
47
|
-
|
|
48
|
-
### `.resx` vs `.loc.json` vs `.resjson`
|
|
49
|
-
|
|
50
|
-
[`.resx`](https://docs.microsoft.com/en-us/dotnet/framework/resources/creating-resource-files-for-desktop-apps#resources-in-resx-files)
|
|
51
|
-
is an XML format for resource data. It is primarily used in .NET development, and it is supported by
|
|
52
|
-
some translation services. See an example of a `.resx` file
|
|
53
|
-
[here](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02/src/strings5.resx).
|
|
54
|
-
Note that the `<xsd:schema>` and `<resheader>` elements are not required. Also note that although the
|
|
55
|
-
`.resx` supports many different types of localized data including strings and binary data, **only strings**
|
|
56
|
-
are supported by this plugin.
|
|
57
|
-
|
|
58
|
-
`.loc.json` is a very simple `JSON` schema for specifying localized string and translator comments.
|
|
59
|
-
See an example of a `.loc.json` file
|
|
60
|
-
[here](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02/src/strings3.loc.json).
|
|
61
|
-
|
|
62
|
-
`.resjson` is another simple `JSON` schema for specifying localized string and translator comments.
|
|
63
|
-
See [here](https://lingohub.com/developers/resource-files/resjson-localization) for documentation on `.resjson`
|
|
64
|
-
|
|
65
|
-
For most projects, `.loc.json` is a simpler format to use. However for large projects, projects that already use
|
|
66
|
-
translation services that support `.resx`, or engineers who are already experienced .NET developers, `.resx`
|
|
67
|
-
may be more convenient.
|
|
68
|
-
|
|
69
|
-
# Plugin
|
|
70
|
-
|
|
71
|
-
To use the plugin, add it to the `plugins` array of your Webpack config. For example:
|
|
72
|
-
|
|
73
|
-
```JavaScript
|
|
74
|
-
import { LocalizationPlugin } from '@rushstack/webpack4-localization-plugin';
|
|
75
|
-
|
|
76
|
-
{
|
|
77
|
-
plugins: [
|
|
78
|
-
new LocalizationPlugin( /* options */ )
|
|
79
|
-
]
|
|
80
|
-
}
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
***A note about the dev server:*** When Webpack is being run by the Webpack dev server, this plugin pipes
|
|
84
|
-
the strings in the loc files in the source (the `.loc.json` and the `.resx` files) to the output without
|
|
85
|
-
any translations.
|
|
86
|
-
|
|
87
|
-
## Options
|
|
88
|
-
|
|
89
|
-
### `localizedData = { }`
|
|
90
|
-
|
|
91
|
-
#### `localizedData.defaultLocale = { }`
|
|
92
|
-
|
|
93
|
-
This option has a required property (`localeName`), to specify the name of the locale used in the
|
|
94
|
-
`.resx` and `.loc.json` files in the source.
|
|
95
|
-
|
|
96
|
-
##### `localizedData.defaultLocale.fillMissingTranslationStrings = true | false`
|
|
97
|
-
|
|
98
|
-
If this option is set to `true`, strings that are missing from `localizedData.translatedStrings` will be
|
|
99
|
-
provided by the default locale (the strings in the `.resx` and `.loc.json` files in the source). If
|
|
100
|
-
this option is unset or set to `false`, an error will be emitted if a string is missing from
|
|
101
|
-
`localizedData.translatedStrings`.
|
|
102
|
-
|
|
103
|
-
#### `localizedData.translatedStrings = { }`
|
|
104
|
-
|
|
105
|
-
This option is used to specify the localization data to be used in the build. This object has the following
|
|
106
|
-
structure:
|
|
107
|
-
|
|
108
|
-
- Locale name
|
|
109
|
-
- Compilation context-relative or absolute localization file path
|
|
110
|
-
- Translated strings
|
|
111
|
-
|
|
112
|
-
For example:
|
|
113
|
-
|
|
114
|
-
```JavaScript
|
|
115
|
-
translatedStrings: {
|
|
116
|
-
"en-us": {
|
|
117
|
-
"./src/strings1.loc.json": {
|
|
118
|
-
"string1": "the first string"
|
|
119
|
-
}
|
|
120
|
-
},
|
|
121
|
-
"es-es": {
|
|
122
|
-
"./src/strings1.loc.json": {
|
|
123
|
-
"string1": "la primera cadena"
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
Alternatively, instead of directly specifying the translations, a path to a translated resource file can be
|
|
130
|
-
specified. For example:
|
|
131
|
-
|
|
132
|
-
```JavaScript
|
|
133
|
-
translatedStrings: {
|
|
134
|
-
"en-us": {
|
|
135
|
-
"./src/strings1.loc.json": "./localization/en-us/strings1.loc.json"
|
|
136
|
-
},
|
|
137
|
-
"es-es": {
|
|
138
|
-
"./src/strings1.loc.json": "./localization/es-es/strings1.loc.json"
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
#### `localizedData.resolveMissingTranslatedStrings = (locales: string[], filePath: string) => { ... }`
|
|
144
|
-
|
|
145
|
-
This optional option can be used to resolve translated data that is missing from data that is provided
|
|
146
|
-
in the `localizedData.translatedStrings` option. Set this option with a function expecting two parameters:
|
|
147
|
-
the first, an array of locale names, and second, a fully-qualified path to the localized file in source. The
|
|
148
|
-
function should return an object with locale names as keys and localized data as values. The localized data
|
|
149
|
-
value should either be:
|
|
150
|
-
|
|
151
|
-
- a string: The absolute path to the translated data in `.resx` or `.loc.json` format
|
|
152
|
-
- an object: An object containing the translated data
|
|
153
|
-
|
|
154
|
-
Note that these values are the same as the values that can be specified for translations for a localized
|
|
155
|
-
resource in `localizedData.translatedStrings`.
|
|
156
|
-
|
|
157
|
-
If the function returns data that is missing locales or individual strings, the plugin will fall back to the
|
|
158
|
-
default locale if `localizedData.defaultLocale.fillMissingTranslationStrings` is set to `true`. If
|
|
159
|
-
`localizedData.defaultLocale.fillMissingTranslationStrings` is set to `false`, an error will result.
|
|
160
|
-
|
|
161
|
-
#### `localizedData.passthroughLocale = { }`
|
|
162
|
-
|
|
163
|
-
This option is used to specify how and if a passthrough locale should be generated. A passthrough locale
|
|
164
|
-
is a generated locale in which each string's value is its name. This is useful for debugging and for identifying
|
|
165
|
-
cases where a locale is missing.
|
|
166
|
-
|
|
167
|
-
This option takes two optional properties:
|
|
168
|
-
|
|
169
|
-
##### `localizedData.passthroughLocale.usePassthroughLocale = true | false`
|
|
170
|
-
|
|
171
|
-
If `passthroughLocale.usePassthroughLocale` is set to `true`, a passthrough locale will be included in the output.
|
|
172
|
-
By default, the passthrough locale's name is "passthrough."
|
|
173
|
-
|
|
174
|
-
##### `localizedData.passthroughLocale.passthroughLocaleName = '...'`
|
|
175
|
-
|
|
176
|
-
If `passthroughLocale.usePassthroughLocale` is set to `true`, the "passthrough" locale name can be overridden
|
|
177
|
-
by setting a value on `passthroughLocale.passthroughLocaleName`.
|
|
178
|
-
|
|
179
|
-
#### `localizedData.pseudolocales = { }`
|
|
180
|
-
|
|
181
|
-
This option allows pseudolocales to be generated from the strings in the default locale. This option takes
|
|
182
|
-
an option with pseudolocales as keys and options for the
|
|
183
|
-
[pseudolocale package](https://www.npmjs.com/package/pseudolocale) as values.
|
|
184
|
-
|
|
185
|
-
#### `localizedData.normalizeResxNewlines = 'crlf' | 'lf'`
|
|
186
|
-
|
|
187
|
-
This option allows normalization of newlines in RESX files. RESX files are XML, so newlines can be
|
|
188
|
-
specified by including a newline in the `<value>` element. For files stored on source control systems,
|
|
189
|
-
clones on Windows can end up with CRLF newlines and clones on 'nix operating systems can end up with LF
|
|
190
|
-
newlines. This option can be used to help make compilations run on different platforms produce the same
|
|
191
|
-
result.
|
|
192
|
-
|
|
193
|
-
#### `localizedData.ignoreMissingResxComments = true | false | undefined
|
|
194
|
-
|
|
195
|
-
If set to true, do not warn on missing RESX <data> element comments.
|
|
196
|
-
|
|
197
|
-
### `globsToIgnore = [ ]`
|
|
198
|
-
|
|
199
|
-
This option is used to specify `.resx` and `.loc.json` files that should not be processed by this plugin.
|
|
200
|
-
By default, every `.resx`, `.resx.json`, and `.loc.json` file import is intercepted by this plugin, and an
|
|
201
|
-
error occurs if translations aren't provided for an intercepted file and the
|
|
202
|
-
`localizedData.defaultLocale.fillMissingTranslationStrings` option is set to falsy, or if the
|
|
203
|
-
file is in an unexpected format. To avoid an error, specify files that should be ignored by this plugin in
|
|
204
|
-
this property. This is useful if a dependency uses files with a `.resx`, `.resx.json`, or `.loc.json`
|
|
205
|
-
extension, but are processed in a different way from how this plugin handles localization.
|
|
206
|
-
For example: `globsToIgnore: [ 'node_modules/some-dependency-name/lib/**/*.loc.json' ]`
|
|
207
|
-
|
|
208
|
-
### `noStringsLocaleName = '...'`
|
|
209
|
-
|
|
210
|
-
The value to replace the `[locale]` token with for chunks without localized strings. Defaults to "none"
|
|
211
|
-
|
|
212
|
-
### `localizationStats = { }`
|
|
213
|
-
|
|
214
|
-
#### `localizationStats.dropPath = '...'`
|
|
215
|
-
|
|
216
|
-
This option is used to designate a path at which a JSON file describing the localized assets produced should be
|
|
217
|
-
written. If this property is omitted, the stats file won't be written.
|
|
218
|
-
|
|
219
|
-
The file has the following format:
|
|
220
|
-
|
|
221
|
-
```JSON
|
|
222
|
-
{
|
|
223
|
-
"entrypoints": {
|
|
224
|
-
"<BUNDLE NAME>": {
|
|
225
|
-
"localizedAssets": {
|
|
226
|
-
"<LOCALE NAME>": "<ASSET NAME>",
|
|
227
|
-
"<LOCALE NAME>": "<ASSET NAME>"
|
|
228
|
-
}
|
|
229
|
-
},
|
|
230
|
-
"<BUNDLE NAME>": {
|
|
231
|
-
"localizedAssets": {
|
|
232
|
-
"<LOCALE NAME>": "<ASSET NAME>",
|
|
233
|
-
"<LOCALE NAME>": "<ASSET NAME>"
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
},
|
|
237
|
-
"namedChunkGroups": {
|
|
238
|
-
"<CHUNK NAME>": {
|
|
239
|
-
"localizedAssets": {
|
|
240
|
-
"<LOCALE NAME>": "<ASSET NAME>",
|
|
241
|
-
"<LOCALE NAME>": "<ASSET NAME>"
|
|
242
|
-
}
|
|
243
|
-
},
|
|
244
|
-
"<CHUNK NAME>": {
|
|
245
|
-
"localizedAssets": {
|
|
246
|
-
"<LOCALE NAME>": "<ASSET NAME>",
|
|
247
|
-
"<LOCALE NAME>": "<ASSET NAME>"
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
#### `localizationStats.callback = (stats) => { ... }`
|
|
256
|
-
|
|
257
|
-
This option is used to specify a callback to be called with the stats data that would be dropped at
|
|
258
|
-
[`localizationStats.dropPath`](#localizationStats.DropPath--) after compilation completes.
|
|
259
|
-
|
|
260
|
-
### `typingsOptions = { }`
|
|
261
|
-
|
|
262
|
-
This option is used to specify how and if TypeScript typings should be generated for loc files.
|
|
263
|
-
|
|
264
|
-
It takes two options:
|
|
265
|
-
|
|
266
|
-
#### `typingsOptions.generatedTsFolder = '...'`
|
|
267
|
-
|
|
268
|
-
This property specifies the folder in which `.d.ts` files for loc files should be dropped. It is recommended
|
|
269
|
-
that this be a folder parallel to the source folder, specified in addition to the source folder in the
|
|
270
|
-
[`rootDirs` `tsconfig.json` option](https://www.typescriptlang.org/docs/handbook/compiler-options.html).
|
|
271
|
-
**The folder specified by this option is emptied when compilation is started.**
|
|
272
|
-
|
|
273
|
-
This property is required if `typingsOptions` is set.
|
|
274
|
-
|
|
275
|
-
#### `typingsOptions.sourceRoot = '...'`
|
|
276
|
-
|
|
277
|
-
This optional property overrides the compiler context for discovery of localization files for which
|
|
278
|
-
typings should be generated.
|
|
279
|
-
|
|
280
|
-
#### `typingsOptions.exportAsDefault = true | false`
|
|
281
|
-
|
|
282
|
-
If this option is set to `true`, loc modules typings will be exported wrapped in a `default` property. This
|
|
283
|
-
allows strings to be imported by using the `import strings from './strings.loc.json';` syntax instead of
|
|
284
|
-
the `import { string1 } from './strings.loc.json';` or the `import * as strings from './strings.loc.json';`
|
|
285
|
-
syntax. This option is not recommended.
|
|
286
|
-
|
|
287
|
-
## Links
|
|
288
|
-
|
|
289
|
-
- [CHANGELOG.md](https://github.com/microsoft/rushstack/blob/main/webpack/localization-plugin/CHANGELOG.md) - Find
|
|
290
|
-
out what's new in the latest version
|
|
291
|
-
|
|
292
|
-
`@rushstack/webpack4-localization-plugin` is part of the [Rush Stack](https://rushstack.io/) family of projects.
|
|
1
|
+
# @rushstack/webpack4-localization-plugin
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
`npm install @rushstack/webpack4-localization-plugin --save-dev`
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
|
|
9
|
+
This Webpack plugin produces bundles that have multiple locales' variants of strings embedded. It also
|
|
10
|
+
has out-of-box support for RESX files in addition to JSON strings files (with the extension `.loc.json`), including
|
|
11
|
+
support for generating typings.
|
|
12
|
+
|
|
13
|
+
### Example Plugin Usage
|
|
14
|
+
|
|
15
|
+
There are three example projects in this repository that make use of this plugin:
|
|
16
|
+
|
|
17
|
+
- [Project 1](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-01)
|
|
18
|
+
- This project contains two webpack entrypoints (one with an async chunk, one without), without any localized
|
|
19
|
+
resources
|
|
20
|
+
- The output is a single, non-localized variant
|
|
21
|
+
- [Project 2](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02)
|
|
22
|
+
- This project contains three webpack entrypoints:
|
|
23
|
+
- [`indexA.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02/src/indexA.ts)
|
|
24
|
+
directly references two `.loc.json` files and one `.resx` file, and dynamically imports an async chunk with
|
|
25
|
+
localized data, and an async chunk without localized data
|
|
26
|
+
- [`indexB.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02/src/indexB.ts)
|
|
27
|
+
directly references two `.loc.json` files
|
|
28
|
+
- [`indexC.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02/src/indexC.ts)
|
|
29
|
+
directly references no localized resources, and dynamically imports an async chunk without localized data
|
|
30
|
+
- The webpack config contains and references Spanish translations for most of the English strings in the resource files
|
|
31
|
+
- The output contains English, Spanish, and "passthrough" localized variants of files that contain
|
|
32
|
+
localized data, and a non-localized variant of the files that do not contain localized data
|
|
33
|
+
- [Project 3](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-03)
|
|
34
|
+
- This project contains four webpack entrypoints:
|
|
35
|
+
- [`indexA.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-03/src/indexA.ts)
|
|
36
|
+
directly references one `.loc.json` file, one `.resx.json` file, one `.resx` file, and one `.resjson` file, and dynamically imports an async chunk with
|
|
37
|
+
localized data, and an async chunk without localized data
|
|
38
|
+
- [`indexB.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-03/src/indexB.ts)
|
|
39
|
+
directly references one `.loc.json` file and one `.resx.json` file
|
|
40
|
+
- [`indexC.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-03/src/indexC.ts)
|
|
41
|
+
directly references no localized resources, and dynamically imports an async chunk with localized data
|
|
42
|
+
- [`indexD.ts`](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-03/src/indexD.ts)
|
|
43
|
+
directly references no localized resources, and dynamically imports an async chunk without localized data
|
|
44
|
+
- The webpack config contains or references Spanish translations for some of the English strings in the resource files
|
|
45
|
+
- The output contains English, Spanish, "passthrough," and two pseudo-localized variants of files that contain
|
|
46
|
+
localized data, and a non-localized variant of the files that do not contain localized data
|
|
47
|
+
|
|
48
|
+
### `.resx` vs `.loc.json` vs `.resjson`
|
|
49
|
+
|
|
50
|
+
[`.resx`](https://docs.microsoft.com/en-us/dotnet/framework/resources/creating-resource-files-for-desktop-apps#resources-in-resx-files)
|
|
51
|
+
is an XML format for resource data. It is primarily used in .NET development, and it is supported by
|
|
52
|
+
some translation services. See an example of a `.resx` file
|
|
53
|
+
[here](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02/src/strings5.resx).
|
|
54
|
+
Note that the `<xsd:schema>` and `<resheader>` elements are not required. Also note that although the
|
|
55
|
+
`.resx` supports many different types of localized data including strings and binary data, **only strings**
|
|
56
|
+
are supported by this plugin.
|
|
57
|
+
|
|
58
|
+
`.loc.json` is a very simple `JSON` schema for specifying localized string and translator comments.
|
|
59
|
+
See an example of a `.loc.json` file
|
|
60
|
+
[here](https://github.com/microsoft/rushstack/tree/main/build-tests/localization-plugin-test-02/src/strings3.loc.json).
|
|
61
|
+
|
|
62
|
+
`.resjson` is another simple `JSON` schema for specifying localized string and translator comments.
|
|
63
|
+
See [here](https://lingohub.com/developers/resource-files/resjson-localization) for documentation on `.resjson`
|
|
64
|
+
|
|
65
|
+
For most projects, `.loc.json` is a simpler format to use. However for large projects, projects that already use
|
|
66
|
+
translation services that support `.resx`, or engineers who are already experienced .NET developers, `.resx`
|
|
67
|
+
may be more convenient.
|
|
68
|
+
|
|
69
|
+
# Plugin
|
|
70
|
+
|
|
71
|
+
To use the plugin, add it to the `plugins` array of your Webpack config. For example:
|
|
72
|
+
|
|
73
|
+
```JavaScript
|
|
74
|
+
import { LocalizationPlugin } from '@rushstack/webpack4-localization-plugin';
|
|
75
|
+
|
|
76
|
+
{
|
|
77
|
+
plugins: [
|
|
78
|
+
new LocalizationPlugin( /* options */ )
|
|
79
|
+
]
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
***A note about the dev server:*** When Webpack is being run by the Webpack dev server, this plugin pipes
|
|
84
|
+
the strings in the loc files in the source (the `.loc.json` and the `.resx` files) to the output without
|
|
85
|
+
any translations.
|
|
86
|
+
|
|
87
|
+
## Options
|
|
88
|
+
|
|
89
|
+
### `localizedData = { }`
|
|
90
|
+
|
|
91
|
+
#### `localizedData.defaultLocale = { }`
|
|
92
|
+
|
|
93
|
+
This option has a required property (`localeName`), to specify the name of the locale used in the
|
|
94
|
+
`.resx` and `.loc.json` files in the source.
|
|
95
|
+
|
|
96
|
+
##### `localizedData.defaultLocale.fillMissingTranslationStrings = true | false`
|
|
97
|
+
|
|
98
|
+
If this option is set to `true`, strings that are missing from `localizedData.translatedStrings` will be
|
|
99
|
+
provided by the default locale (the strings in the `.resx` and `.loc.json` files in the source). If
|
|
100
|
+
this option is unset or set to `false`, an error will be emitted if a string is missing from
|
|
101
|
+
`localizedData.translatedStrings`.
|
|
102
|
+
|
|
103
|
+
#### `localizedData.translatedStrings = { }`
|
|
104
|
+
|
|
105
|
+
This option is used to specify the localization data to be used in the build. This object has the following
|
|
106
|
+
structure:
|
|
107
|
+
|
|
108
|
+
- Locale name
|
|
109
|
+
- Compilation context-relative or absolute localization file path
|
|
110
|
+
- Translated strings
|
|
111
|
+
|
|
112
|
+
For example:
|
|
113
|
+
|
|
114
|
+
```JavaScript
|
|
115
|
+
translatedStrings: {
|
|
116
|
+
"en-us": {
|
|
117
|
+
"./src/strings1.loc.json": {
|
|
118
|
+
"string1": "the first string"
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
"es-es": {
|
|
122
|
+
"./src/strings1.loc.json": {
|
|
123
|
+
"string1": "la primera cadena"
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Alternatively, instead of directly specifying the translations, a path to a translated resource file can be
|
|
130
|
+
specified. For example:
|
|
131
|
+
|
|
132
|
+
```JavaScript
|
|
133
|
+
translatedStrings: {
|
|
134
|
+
"en-us": {
|
|
135
|
+
"./src/strings1.loc.json": "./localization/en-us/strings1.loc.json"
|
|
136
|
+
},
|
|
137
|
+
"es-es": {
|
|
138
|
+
"./src/strings1.loc.json": "./localization/es-es/strings1.loc.json"
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
#### `localizedData.resolveMissingTranslatedStrings = (locales: string[], filePath: string) => { ... }`
|
|
144
|
+
|
|
145
|
+
This optional option can be used to resolve translated data that is missing from data that is provided
|
|
146
|
+
in the `localizedData.translatedStrings` option. Set this option with a function expecting two parameters:
|
|
147
|
+
the first, an array of locale names, and second, a fully-qualified path to the localized file in source. The
|
|
148
|
+
function should return an object with locale names as keys and localized data as values. The localized data
|
|
149
|
+
value should either be:
|
|
150
|
+
|
|
151
|
+
- a string: The absolute path to the translated data in `.resx` or `.loc.json` format
|
|
152
|
+
- an object: An object containing the translated data
|
|
153
|
+
|
|
154
|
+
Note that these values are the same as the values that can be specified for translations for a localized
|
|
155
|
+
resource in `localizedData.translatedStrings`.
|
|
156
|
+
|
|
157
|
+
If the function returns data that is missing locales or individual strings, the plugin will fall back to the
|
|
158
|
+
default locale if `localizedData.defaultLocale.fillMissingTranslationStrings` is set to `true`. If
|
|
159
|
+
`localizedData.defaultLocale.fillMissingTranslationStrings` is set to `false`, an error will result.
|
|
160
|
+
|
|
161
|
+
#### `localizedData.passthroughLocale = { }`
|
|
162
|
+
|
|
163
|
+
This option is used to specify how and if a passthrough locale should be generated. A passthrough locale
|
|
164
|
+
is a generated locale in which each string's value is its name. This is useful for debugging and for identifying
|
|
165
|
+
cases where a locale is missing.
|
|
166
|
+
|
|
167
|
+
This option takes two optional properties:
|
|
168
|
+
|
|
169
|
+
##### `localizedData.passthroughLocale.usePassthroughLocale = true | false`
|
|
170
|
+
|
|
171
|
+
If `passthroughLocale.usePassthroughLocale` is set to `true`, a passthrough locale will be included in the output.
|
|
172
|
+
By default, the passthrough locale's name is "passthrough."
|
|
173
|
+
|
|
174
|
+
##### `localizedData.passthroughLocale.passthroughLocaleName = '...'`
|
|
175
|
+
|
|
176
|
+
If `passthroughLocale.usePassthroughLocale` is set to `true`, the "passthrough" locale name can be overridden
|
|
177
|
+
by setting a value on `passthroughLocale.passthroughLocaleName`.
|
|
178
|
+
|
|
179
|
+
#### `localizedData.pseudolocales = { }`
|
|
180
|
+
|
|
181
|
+
This option allows pseudolocales to be generated from the strings in the default locale. This option takes
|
|
182
|
+
an option with pseudolocales as keys and options for the
|
|
183
|
+
[pseudolocale package](https://www.npmjs.com/package/pseudolocale) as values.
|
|
184
|
+
|
|
185
|
+
#### `localizedData.normalizeResxNewlines = 'crlf' | 'lf'`
|
|
186
|
+
|
|
187
|
+
This option allows normalization of newlines in RESX files. RESX files are XML, so newlines can be
|
|
188
|
+
specified by including a newline in the `<value>` element. For files stored on source control systems,
|
|
189
|
+
clones on Windows can end up with CRLF newlines and clones on 'nix operating systems can end up with LF
|
|
190
|
+
newlines. This option can be used to help make compilations run on different platforms produce the same
|
|
191
|
+
result.
|
|
192
|
+
|
|
193
|
+
#### `localizedData.ignoreMissingResxComments = true | false | undefined
|
|
194
|
+
|
|
195
|
+
If set to true, do not warn on missing RESX <data> element comments.
|
|
196
|
+
|
|
197
|
+
### `globsToIgnore = [ ]`
|
|
198
|
+
|
|
199
|
+
This option is used to specify `.resx` and `.loc.json` files that should not be processed by this plugin.
|
|
200
|
+
By default, every `.resx`, `.resx.json`, and `.loc.json` file import is intercepted by this plugin, and an
|
|
201
|
+
error occurs if translations aren't provided for an intercepted file and the
|
|
202
|
+
`localizedData.defaultLocale.fillMissingTranslationStrings` option is set to falsy, or if the
|
|
203
|
+
file is in an unexpected format. To avoid an error, specify files that should be ignored by this plugin in
|
|
204
|
+
this property. This is useful if a dependency uses files with a `.resx`, `.resx.json`, or `.loc.json`
|
|
205
|
+
extension, but are processed in a different way from how this plugin handles localization.
|
|
206
|
+
For example: `globsToIgnore: [ 'node_modules/some-dependency-name/lib/**/*.loc.json' ]`
|
|
207
|
+
|
|
208
|
+
### `noStringsLocaleName = '...'`
|
|
209
|
+
|
|
210
|
+
The value to replace the `[locale]` token with for chunks without localized strings. Defaults to "none"
|
|
211
|
+
|
|
212
|
+
### `localizationStats = { }`
|
|
213
|
+
|
|
214
|
+
#### `localizationStats.dropPath = '...'`
|
|
215
|
+
|
|
216
|
+
This option is used to designate a path at which a JSON file describing the localized assets produced should be
|
|
217
|
+
written. If this property is omitted, the stats file won't be written.
|
|
218
|
+
|
|
219
|
+
The file has the following format:
|
|
220
|
+
|
|
221
|
+
```JSON
|
|
222
|
+
{
|
|
223
|
+
"entrypoints": {
|
|
224
|
+
"<BUNDLE NAME>": {
|
|
225
|
+
"localizedAssets": {
|
|
226
|
+
"<LOCALE NAME>": "<ASSET NAME>",
|
|
227
|
+
"<LOCALE NAME>": "<ASSET NAME>"
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
"<BUNDLE NAME>": {
|
|
231
|
+
"localizedAssets": {
|
|
232
|
+
"<LOCALE NAME>": "<ASSET NAME>",
|
|
233
|
+
"<LOCALE NAME>": "<ASSET NAME>"
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
"namedChunkGroups": {
|
|
238
|
+
"<CHUNK NAME>": {
|
|
239
|
+
"localizedAssets": {
|
|
240
|
+
"<LOCALE NAME>": "<ASSET NAME>",
|
|
241
|
+
"<LOCALE NAME>": "<ASSET NAME>"
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
"<CHUNK NAME>": {
|
|
245
|
+
"localizedAssets": {
|
|
246
|
+
"<LOCALE NAME>": "<ASSET NAME>",
|
|
247
|
+
"<LOCALE NAME>": "<ASSET NAME>"
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
#### `localizationStats.callback = (stats) => { ... }`
|
|
256
|
+
|
|
257
|
+
This option is used to specify a callback to be called with the stats data that would be dropped at
|
|
258
|
+
[`localizationStats.dropPath`](#localizationStats.DropPath--) after compilation completes.
|
|
259
|
+
|
|
260
|
+
### `typingsOptions = { }`
|
|
261
|
+
|
|
262
|
+
This option is used to specify how and if TypeScript typings should be generated for loc files.
|
|
263
|
+
|
|
264
|
+
It takes two options:
|
|
265
|
+
|
|
266
|
+
#### `typingsOptions.generatedTsFolder = '...'`
|
|
267
|
+
|
|
268
|
+
This property specifies the folder in which `.d.ts` files for loc files should be dropped. It is recommended
|
|
269
|
+
that this be a folder parallel to the source folder, specified in addition to the source folder in the
|
|
270
|
+
[`rootDirs` `tsconfig.json` option](https://www.typescriptlang.org/docs/handbook/compiler-options.html).
|
|
271
|
+
**The folder specified by this option is emptied when compilation is started.**
|
|
272
|
+
|
|
273
|
+
This property is required if `typingsOptions` is set.
|
|
274
|
+
|
|
275
|
+
#### `typingsOptions.sourceRoot = '...'`
|
|
276
|
+
|
|
277
|
+
This optional property overrides the compiler context for discovery of localization files for which
|
|
278
|
+
typings should be generated.
|
|
279
|
+
|
|
280
|
+
#### `typingsOptions.exportAsDefault = true | false`
|
|
281
|
+
|
|
282
|
+
If this option is set to `true`, loc modules typings will be exported wrapped in a `default` property. This
|
|
283
|
+
allows strings to be imported by using the `import strings from './strings.loc.json';` syntax instead of
|
|
284
|
+
the `import { string1 } from './strings.loc.json';` or the `import * as strings from './strings.loc.json';`
|
|
285
|
+
syntax. This option is not recommended.
|
|
286
|
+
|
|
287
|
+
## Links
|
|
288
|
+
|
|
289
|
+
- [CHANGELOG.md](https://github.com/microsoft/rushstack/blob/main/webpack/localization-plugin/CHANGELOG.md) - Find
|
|
290
|
+
out what's new in the latest version
|
|
291
|
+
|
|
292
|
+
`@rushstack/webpack4-localization-plugin` is part of the [Rush Stack](https://rushstack.io/) family of projects.
|