npm install @rushstack/localization-plugin --save-dev
This plugin produces webpack bundles that have multiple locales' variants of strings embedded. It also
has out-of-box support for RESX files in addition to JSON strings files (with the extension .loc.json), including
support for generating typings.
There are three example projects in this repository that make use of this plugin:
- Project 1
- This project contains two webpack entrypoints (one with an async chunk, one without), without any localized resources
- The output is a single, non-localized variant
- Project 2
- This project contains three webpack entrypoints:
indexA.tsdirectly references two.loc.jsonfiles and one.resxfile, and dynamically imports an async chunk with localized data, and an async chunk without localized dataindexB.tsdirectly references two.loc.jsonfilesindexC.tsdirectly references no localized resources, and dynamically imports an async chunk without localized data
- The webpack config contains Spanish translations for most of the English strings in the resource files
- The output contains English, Spanish, and "passthrough" localized variants of files that contain localized data, and a non-localized variant of the files that do not contain localized data
- This project contains three webpack entrypoints:
- Project 3
- This project contains four webpack entrypoints:
indexA.tsdirectly references two.loc.jsonfiles and one.resxfile, and dynamically imports an async chunk with localized data, and an async chunk without localized dataindexB.tsdirectly references two.loc.jsonfilesindexC.tsdirectly references no localized resources, and dynamically imports an async chunk with localized dataindexD.tsdirectly references no localized resources, and dynamically imports an async chunk without localized data
- The webpack config contains Spanish translations for all of the English strings in the resource files
- The output contains English, Spanish, "passthrough," and two pseudo-localized variants of files that contain localized data, and a non-localized variant of the files that do not contain localized data
- This project contains four webpack entrypoints:
.resx
is an XML format for resource data. It is primarily used in .NET development, and it is supported by
some translation services. See an example of a .resx file
here.
Note that the <xsd:schema> and <resheader> elements are not required. Also note that although the
.resx supports many different types of localized data including strings and binary data, only strings
are supported by this plugin.
.loc.json is a very simple JSON schema for specifying localized string and translator comments.
See an example of a .loc.json file
here.
For most projects, .loc.json is a simpler format to use. However for large projects, projects that already use
translation services that support .resx, or engineers who are already experienced .NET developers, .resx
may be more convenient.
To use the plugin, add it to the plugins array of your Webpack config. For example:
import { LocalizationPlugin } from '@rushstack/localization-plugin';
{
plugins: [
new LocalizationPlugin( /* options */ )
]
}A note about the dev server: When Webpack is being run by the Webpack dev server, this plugin pipes
the strings in the loc files in the source (the .loc.json and the .resx files) to the output without
any translations.
This option has a required property (localeName), to specify the name of the locale used in the
.resx and .loc.json files in the source.
If this option is set to true, strings that are missing from localizedData.translatedStrings will be
provided by the default locale (the strings in the .resx and .loc.json files in the source). If
this option is unset or set to false, an error will be emitted if a string is missing from
localizedData.translatedStrings.
This option is used to specify the localization data to be used in the build. This object has the following structure:
- Locale name
- Compilation context-relative or absolute localization file path
- Translated strings
- Compilation context-relative or absolute localization file path
For example:
translatedStrings: {
"en-us": {
"./src/strings1.loc.json": {
"string1": "the first string"
}
},
"es-es": {
"./src/strings1.loc.json": {
"string1": "la primera cadena"
}
}
}This option is used to specify how and if a passthrough locale should be generated. A passthrough locale is a generated locale in which each string's value is its name. This is useful for debugging and for identifying cases where a locale is missing.
This option takes two optional properties:
If passthroughLocale.usePassthroughLocale is set to true, a passthrough locale will be included in the output.
By default, the passthrough locale's name is "passthrough."
If passthroughLocale.usePassthroughLocale is set to true, the "passthrough" locale name can be overridden
by setting a value on passthroughLocale.passthroughLocaleName.
This option allows pseudolocales to be generated from the strings in the default locale. This option takes an option with pseudolocales as keys and options for the pseudolocale package as values.
This option is used to specify .resx and .loc.json files that should not be processed by this plugin.
By default, every .resx and .loc.json file import is intercepted by this plugin, and an error occurs
if translations aren't provided for an intercepted file and the
localizedData.defaultLocale.fillMissingTranslationStrings option is set to false To avoid that error,
list files that should be ignored by this plugin in this property. Files should be specified as either
absolute paths or paths relative to the Webpack compilation context.
The value to replace the [locale] token with for chunks without localized strings. Defaults to "none"
This option is used to designate a path at which a JSON file describing the localized assets produced should be written. If this property is omitted, the stats file won't be written.
The file has the following format:
{
"entrypoints": {
"<BUNDLE NAME>": {
"localizedAssets": {
"<LOCALE NAME>": "<ASSET NAME>",
"<LOCALE NAME>": "<ASSET NAME>"
}
},
"<BUNDLE NAME>": {
"localizedAssets": {
"<LOCALE NAME>": "<ASSET NAME>",
"<LOCALE NAME>": "<ASSET NAME>"
}
}
},
"namedChunkGroups": {
"<CHUNK NAME>": {
"localizedAssets": {
"<LOCALE NAME>": "<ASSET NAME>",
"<LOCALE NAME>": "<ASSET NAME>"
}
},
"<CHUNK NAME>": {
"localizedAssets": {
"<LOCALE NAME>": "<ASSET NAME>",
"<LOCALE NAME>": "<ASSET NAME>"
}
}
}
}
This option is used to specify a callback to be called with the stats data that would be dropped at
localizationStats.dropPath after compilation completes.
This option is used to specify how and if TypeScript typings should be generated for loc files.
It takes two options:
This property specifies the folder in which .d.ts files for loc files should be dropped. It is recommended
that this be a folder parallel to the source folder, specified in addition to the source folder in the
rootDirs tsconfig.json option.
The folder specified by this option is emptied when compilation is started.
This property is required if typingsOptions is set.
This optional property overrides the compiler context for discovery of localization files for which typings should be generated.
If this option is set to true, loc modules typings will be exported wrapped in a default property. This
allows strings to be imported by using the import strings from './strings.loc.json'; syntax instead of
the import { string1 } from './strings.loc.json'; or the import * as strings from './strings.loc.json';
syntax. This option is not recommended.