Skip to content

Commit 9814362

Browse files
authored
[localization-plugin] Add a feature for injecting custom localized values into a compilation (#5262)
* Add a feature for injecting custom localized values into a compilation * Include compilation and chunk. * Remove the compilation. * fixup! Remove the compilation. * Require a unique ID in getCustomDataPlaceholderForValueFunction. * fixup! Require a unique ID in getCustomDataPlaceholderForValueFunction. * Mark getCustomDataPlaceholderForValueFunction as beta.
1 parent 120409b commit 9814362

File tree

11 files changed

+406
-96
lines changed

11 files changed

+406
-96
lines changed

.vscode/launch.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
"--inspect-brk",
3939
"${workspaceFolder}/apps/heft/lib/start.js",
4040
"--debug",
41-
"test-watch"
41+
"test",
42+
"--test-path-pattern",
43+
"${fileBasenameNoExtension}"
4244
],
4345
"skipFiles": ["<node_internals>/**"],
4446
"outFiles": [],
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/webpack5-localization-plugin",
5+
"comment": "Add a feature for injecting custom localized values into a compilation via the `getCustomDataPlaceholderForValueFunction` function on an instance of the `LocalizationPlugin`.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/webpack5-localization-plugin"
10+
}

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import type { IPseudolocaleOptions } from '@rushstack/localization-utilities';
1414
import type { LoaderContext } from 'webpack';
1515
import type { WebpackPluginInstance } from 'webpack';
1616

17+
// @internal (undocumented)
18+
export interface _ICustomDataPlaceholder extends IValuePlaceholderBase {
19+
valueForLocaleFn: ValueForLocaleFn;
20+
}
21+
1722
// @public (undocumented)
1823
export interface IDefaultLocaleOptions {
1924
fillMissingTranslationStrings?: boolean;
@@ -121,30 +126,40 @@ export interface IPseudolocalesOptions {
121126
export type IResolvedMissingTranslations = ReadonlyMap<string, ILocaleFileData>;
122127

123128
// @public (undocumented)
124-
export interface _IStringPlaceholder {
129+
interface IStringPlaceholder extends IValuePlaceholderBase {
125130
locFilePath: string;
126131
stringName: string;
127-
suffix: string;
128132
translations: ReadonlyMap<string, ReadonlyMap<string, string>>;
129-
value: string;
130133
}
134+
export { IStringPlaceholder }
135+
export { IStringPlaceholder as _IStringPlaceholder }
131136

132137
// @public (undocumented)
133138
export interface ITrueHashPluginOptions {
134139
hashFunction?: (contents: string | Buffer) => string;
135140
stageOverride?: number;
136141
}
137142

143+
// @public (undocumented)
144+
export interface IValuePlaceholderBase {
145+
suffix: string;
146+
value: string;
147+
}
148+
138149
// @public
139150
export class LocalizationPlugin implements WebpackPluginInstance {
140151
constructor(options: ILocalizationPluginOptions);
141152
// (undocumented)
142153
addDefaultLocFileAsync(context: LoaderContext<{}>, localizedFileKey: string, localizedResourceData: ILocalizationFile): Promise<Record<string, string>>;
143154
apply(compiler: Compiler): void;
144155
// @internal (undocumented)
145-
getDataForSerialNumber(serialNumber: string): _IStringPlaceholder | undefined;
156+
_getCustomDataForSerialNumber(suffix: string): _ICustomDataPlaceholder | undefined;
157+
// @beta (undocumented)
158+
getCustomDataPlaceholderForValueFunction(valueForLocaleFn: ValueForLocaleFn, placeholderUniqueId: string): string;
146159
// (undocumented)
147-
getPlaceholder(localizedFileKey: string, stringName: string): _IStringPlaceholder | undefined;
160+
getPlaceholder(localizedFileKey: string, stringName: string): IStringPlaceholder | undefined;
161+
// @internal (undocumented)
162+
_getStringDataForSerialNumber(suffix: string): IStringPlaceholder | undefined;
148163
// @internal (undocumented)
149164
readonly _options: ILocalizationPluginOptions;
150165
}
@@ -156,6 +171,9 @@ export class TrueHashPlugin implements WebpackPluginInstance {
156171
apply(compiler: Compiler): void;
157172
}
158173

174+
// @public (undocumented)
175+
export type ValueForLocaleFn = (locale: string, chunk: Chunk) => string;
176+
159177
// (No @packageDocumentation comment for this package)
160178

161179
```

webpack/webpack5-localization-plugin/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ use the true hash of the content, rather than an intermediate hash that is share
209209
Note that this option is not compatible with the `runtimeLocaleExpression` option and will cause an error if
210210
both are set.
211211

212+
## Custom Localized Data
213+
214+
If you need to provide custom localized data, you can use the `getCustomDataPlaceholderForValueFunction` method
215+
of the plugin. This method takes a function that receives a locale name and the chunk
216+
that the placeholder is used in and returns a string that will be used as a placeholder for the localized data.
217+
The provided function will be called for each locale that is used in the build and the returned value will replace
218+
the returned placeholder in the output.
219+
220+
Note that this may produce unexpected results if there are no other localized values in the chunk that the
221+
placeholder is used in.
222+
212223
## Links
213224

214225
- [CHANGELOG.md](https://github.com/microsoft/rushstack/blob/main/webpack/localization-plugin/CHANGELOG.md) - Find

0 commit comments

Comments
 (0)