Skip to content

Commit 2fd3b15

Browse files
committed
Replace the default locale options with passthrough options.
1 parent 298770b commit 2fd3b15

File tree

7 files changed

+47
-79
lines changed

7 files changed

+47
-79
lines changed

build-tests/localization-plugin-test-01/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = function(env) {
4444
new webpack.optimize.ModuleConcatenationPlugin(),
4545
new LocalizationPlugin({
4646
localizedStrings: {},
47-
defaultLocale: {
47+
passthroughLocale: {
4848
usePassthroughLocale: true
4949
},
5050
typingsOptions: {

build-tests/localization-plugin-test-02/webpack.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ module.exports = function(env) {
8585
}
8686
}
8787
},
88-
defaultLocale: {
89-
usePassthroughLocale: true
88+
passthroughLocale: {
89+
usePassthroughLocale: true,
90+
passthroughLocaleName: 'default'
9091
},
9192
typingsOptions: {
9293
generatedTsFolder: path.resolve(__dirname, 'temp', 'loc-json-ts'),

build-tests/localization-plugin-test-03/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module.exports = function(env) {
8585
}
8686
}
8787
},
88-
defaultLocale: {
88+
passthroughLocale: {
8989
usePassthroughLocale: true
9090
},
9191
exportAsDefault: true,

common/reviews/api/localization-plugin.api.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@
77
import { Terminal } from '@microsoft/node-core-library';
88
import * as Webpack from 'webpack';
99

10-
// @public (undocumented)
11-
export interface IDefaultLocaleOptions {
12-
// (undocumented)
13-
locale?: string;
14-
passthroughLocaleName?: string;
15-
// (undocumented)
16-
usePassthroughLocale?: boolean;
17-
}
18-
1910
// @public (undocumented)
2011
export interface ILocale {
2112
// (undocumented)
@@ -36,8 +27,6 @@ export interface ILocales {
3627

3728
// @public
3829
export interface ILocalizationPluginOptions {
39-
// (undocumented)
40-
defaultLocale: IDefaultLocaleOptions;
4130
// (undocumented)
4231
exportAsDefault?: boolean;
4332
// (undocumented)
@@ -46,8 +35,8 @@ export interface ILocalizationPluginOptions {
4635
localizationStatsCallback?: (stats: ILocalizationStats) => void;
4736
// (undocumented)
4837
localizationStatsDropPath?: string;
49-
// (undocumented)
5038
localizedStrings: ILocales;
39+
passthroughLocale?: IPassthroughLocaleOptions;
5140
// (undocumented)
5241
typingsOptions?: ITypingsGenerationOptions;
5342
}
@@ -81,17 +70,10 @@ export interface ILocalizationStatsEntrypoint {
8170
}
8271

8372
// @public (undocumented)
84-
export interface ILocFilePreprocessorOptions {
85-
// (undocumented)
86-
exportAsDefault?: boolean;
87-
// (undocumented)
88-
filesToIgnore?: string[];
89-
// (undocumented)
90-
generatedTsFolder: string;
91-
// (undocumented)
92-
srcFolder: string;
73+
export interface IPassthroughLocaleOptions {
74+
passthroughLocaleName?: string;
9375
// (undocumented)
94-
terminal?: Terminal;
76+
usePassthroughLocale?: boolean;
9577
}
9678

9779
// @internal (undocumented)
@@ -110,6 +92,20 @@ export interface ITypingsGenerationOptions {
11092
sourceRoot?: string;
11193
}
11294

95+
// @public (undocumented)
96+
export interface ITypingsGeneratorOptions {
97+
// (undocumented)
98+
exportAsDefault?: boolean;
99+
// (undocumented)
100+
filesToIgnore?: string[];
101+
// (undocumented)
102+
generatedTsFolder: string;
103+
// (undocumented)
104+
srcFolder: string;
105+
// (undocumented)
106+
terminal?: Terminal;
107+
}
108+
113109
// @public
114110
export class LocalizationPlugin implements Webpack.Plugin {
115111
constructor(options: ILocalizationPluginOptions);
@@ -120,8 +116,8 @@ export class LocalizationPlugin implements Webpack.Plugin {
120116
}
121117

122118
// @public
123-
export class LocFilePreprocessor {
124-
constructor(options: ILocFilePreprocessorOptions);
119+
export class TypingsGenerator {
120+
constructor(options: ITypingsGeneratorOptions);
125121
// (undocumented)
126122
generateTypings(): void;
127123
// (undocumented)

webpack/localization-plugin/src/LocalizationPlugin.ts

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export class LocalizationPlugin implements Webpack.Plugin {
6565
private _passthroughStringsMap: Map<string, string>;
6666
private _locales: Set<string>;
6767
private _localeNamePlaceholder: IStringPlaceholder;
68-
private _defaultLocale: string;
6968

7069
/**
7170
* The outermost map's keys are the locale names.
@@ -369,18 +368,10 @@ export class LocalizationPlugin implements Webpack.Plugin {
369368
}
370369
}
371370

372-
let newAsset: IAsset;
373-
if (locale === this._defaultLocale) {
374-
newAsset = asset;
375-
} else {
376-
newAsset = lodash.clone(asset);
377-
}
378-
379-
// TODO:
380-
// - Fixup source maps
381371
const resultFilename: string = assetName.replace(Constants.LOCALE_FILENAME_PLACEHOLDER_REGEX, locale);
382372
const newAssetSource: string = reconstruction.join('');
383373
const newAssetSize: number = asset.size() + sizeDiff;
374+
const newAsset: IAsset = lodash.clone(asset);
384375
newAsset.source = () => newAssetSource;
385376
newAsset.size = () => newAssetSize;
386377
result.set(
@@ -556,46 +547,20 @@ export class LocalizationPlugin implements Webpack.Plugin {
556547
}
557548
// END options.localizedStrings
558549

559-
// START options.defaultLocale
550+
// START options.passthroughLocale
560551
{ // eslint-disable-line no-lone-blocks
561-
if (
562-
!this._options.defaultLocale ||
563-
(!this._options.defaultLocale.locale && !this._options.defaultLocale.usePassthroughLocale)
564-
) {
565-
if (this._locales.size === 1) {
566-
this._defaultLocale = this._locales.entries[0];
567-
} else {
568-
errors.push(new Error(
569-
'Either options.defaultLocale.locale must be provided or options.defaultLocale.usePassthroughLocale ' +
570-
'must be set to true if more than one locale\'s data is provided'
571-
));
572-
}
573-
} else {
574-
const { locale, usePassthroughLocale, passthroughLocaleName } = this._options.defaultLocale;
575-
if (locale && usePassthroughLocale) {
576-
errors.push(new Error(
577-
'Either options.defaultLocale.locale must be provided or options.defaultLocale.usePassthroughLocale ' +
578-
'must be set to true, but not both'
579-
));
580-
} else if (usePassthroughLocale) {
581-
this._defaultLocale = passthroughLocaleName || 'passthrough';
582-
this._locales.add(this._defaultLocale);
583-
this._stringPlaceholderMap.get(this._localeNamePlaceholder.suffix)![this._defaultLocale] =
584-
this._defaultLocale;
552+
if (this._options.passthroughLocale) {
553+
const { usePassthroughLocale, passthroughLocaleName = 'passthrough' } = this._options.passthroughLocale;
554+
if (usePassthroughLocale) {
555+
this._locales.add(passthroughLocaleName);
556+
this._stringPlaceholderMap.get(this._localeNamePlaceholder.suffix)![passthroughLocaleName] = passthroughLocaleName;
585557
this._passthroughStringsMap.forEach((stringName: string, stringKey: string) => {
586-
this._stringPlaceholderMap.get(stringKey)![this._defaultLocale] = stringName;
558+
this._stringPlaceholderMap.get(stringKey)![passthroughLocaleName] = stringName;
587559
});
588-
} else if (locale) {
589-
this._defaultLocale = locale;
590-
if (!this._locales.has(locale)) {
591-
errors.push(new Error(`The specified default locale "${locale}" was not provided in the localized data`));
592-
}
593-
} else {
594-
errors.push(new Error('Unknown error occurred processing default locale.'));
595560
}
596561
}
597562
}
598-
// END options.defaultLocale
563+
// END options.passthroughLocale
599564

600565
return errors;
601566
}

webpack/localization-plugin/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export {
77
} from './LocalizationPlugin';
88

99
export {
10-
IDefaultLocaleOptions,
10+
IPassthroughLocaleOptions,
1111
ILocaleFileData,
1212
ILocale,
1313
ILocales,
@@ -19,6 +19,6 @@ export {
1919
} from './interfaces';
2020

2121
export {
22-
ITypingsGeneratorOptions as ILocFilePreprocessorOptions,
23-
TypingsGenerator as LocFilePreprocessor
22+
ITypingsGeneratorOptions,
23+
TypingsGenerator
2424
} from './TypingsGenerator';

webpack/localization-plugin/src/interfaces.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
/**
55
* @public
66
*/
7-
export interface IDefaultLocaleOptions {
8-
locale?: string;
7+
export interface IPassthroughLocaleOptions {
98
usePassthroughLocale?: boolean;
109

1110
/**
12-
* If {@link IDefaultLocaleOptions.usePassthroughLocale} is set, use this name for the passthrough locale.
11+
* If {@link IPassthroughLocaleOptions.usePassthroughLocale} is set, use this name for the passthrough locale.
1312
* Defaults to "passthrough"
1413
*/
1514
passthroughLocaleName?: string;
@@ -29,8 +28,15 @@ export interface ITypingsGenerationOptions {
2928
* @public
3029
*/
3130
export interface ILocalizationPluginOptions {
31+
/**
32+
* Use this parameter to specify the translated data.
33+
*/
3234
localizedStrings: ILocales;
33-
defaultLocale: IDefaultLocaleOptions;
35+
36+
/**
37+
* Options around including a passthrough locale.
38+
*/
39+
passthroughLocale?: IPassthroughLocaleOptions;
3440
exportAsDefault?: boolean;
3541
filesToIgnore?: string[];
3642
localizationStatsDropPath?: string;

0 commit comments

Comments
 (0)