Skip to content

[localization-plugin] Add a feature for injecting custom localized values into a compilation #5262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
"--inspect-brk",
"${workspaceFolder}/apps/heft/lib/start.js",
"--debug",
"test-watch"
"test",
"--test-path-pattern",
"${fileBasenameNoExtension}"
],
"skipFiles": ["<node_internals>/**"],
"outFiles": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/webpack5-localization-plugin",
"comment": "Add a feature for injecting custom localized values into a compilation via the `getCustomDataPlaceholderForValueFunction` function on an instance of the `LocalizationPlugin`.",
"type": "minor"
}
],
"packageName": "@rushstack/webpack5-localization-plugin"
}
28 changes: 23 additions & 5 deletions common/reviews/api/webpack5-localization-plugin.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import type { IPseudolocaleOptions } from '@rushstack/localization-utilities';
import type { LoaderContext } from 'webpack';
import type { WebpackPluginInstance } from 'webpack';

// @internal (undocumented)
export interface _ICustomDataPlaceholder extends IValuePlaceholderBase {
valueForLocaleFn: ValueForLocaleFn;
}

// @public (undocumented)
export interface IDefaultLocaleOptions {
fillMissingTranslationStrings?: boolean;
Expand Down Expand Up @@ -121,30 +126,40 @@ export interface IPseudolocalesOptions {
export type IResolvedMissingTranslations = ReadonlyMap<string, ILocaleFileData>;

// @public (undocumented)
export interface _IStringPlaceholder {
interface IStringPlaceholder extends IValuePlaceholderBase {
locFilePath: string;
stringName: string;
suffix: string;
translations: ReadonlyMap<string, ReadonlyMap<string, string>>;
value: string;
}
export { IStringPlaceholder }
export { IStringPlaceholder as _IStringPlaceholder }

// @public (undocumented)
export interface ITrueHashPluginOptions {
hashFunction?: (contents: string | Buffer) => string;
stageOverride?: number;
}

// @public (undocumented)
export interface IValuePlaceholderBase {
suffix: string;
value: string;
}

// @public
export class LocalizationPlugin implements WebpackPluginInstance {
constructor(options: ILocalizationPluginOptions);
// (undocumented)
addDefaultLocFileAsync(context: LoaderContext<{}>, localizedFileKey: string, localizedResourceData: ILocalizationFile): Promise<Record<string, string>>;
apply(compiler: Compiler): void;
// @internal (undocumented)
getDataForSerialNumber(serialNumber: string): _IStringPlaceholder | undefined;
_getCustomDataForSerialNumber(suffix: string): _ICustomDataPlaceholder | undefined;
// @beta (undocumented)
getCustomDataPlaceholderForValueFunction(valueForLocaleFn: ValueForLocaleFn, placeholderUniqueId: string): string;
// (undocumented)
getPlaceholder(localizedFileKey: string, stringName: string): _IStringPlaceholder | undefined;
getPlaceholder(localizedFileKey: string, stringName: string): IStringPlaceholder | undefined;
// @internal (undocumented)
_getStringDataForSerialNumber(suffix: string): IStringPlaceholder | undefined;
// @internal (undocumented)
readonly _options: ILocalizationPluginOptions;
}
Expand All @@ -156,6 +171,9 @@ export class TrueHashPlugin implements WebpackPluginInstance {
apply(compiler: Compiler): void;
}

// @public (undocumented)
export type ValueForLocaleFn = (locale: string, chunk: Chunk) => string;

// (No @packageDocumentation comment for this package)

```
11 changes: 11 additions & 0 deletions webpack/webpack5-localization-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ use the true hash of the content, rather than an intermediate hash that is share
Note that this option is not compatible with the `runtimeLocaleExpression` option and will cause an error if
both are set.

## Custom Localized Data

If you need to provide custom localized data, you can use the `getCustomDataPlaceholderForValueFunction` method
of the plugin. This method takes a function that receives a locale name and the chunk
that the placeholder is used in and returns a string that will be used as a placeholder for the localized data.
The provided function will be called for each locale that is used in the build and the returned value will replace
the returned placeholder in the output.

Note that this may produce unexpected results if there are no other localized values in the chunk that the
placeholder is used in.

## Links

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