Skip to content

Commit b4f23cc

Browse files
committed
add locale configuration for docx export
allows setting the default language in generated .docx files
1 parent 8a8c5c5 commit b4f23cc

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

packages/xl-docx-exporter/src/docx/docxExporter.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,22 @@ export class DOCXExporter<
187187
];
188188
}
189189

190-
protected async createDefaultDocumentOptions(): Promise<DocumentOptions> {
191-
const externalStyles = (await import("./template/word/styles.xml?raw"))
190+
protected async createDefaultDocumentOptions(
191+
locale?: string,
192+
): Promise<DocumentOptions> {
193+
let externalStyles = (await import("./template/word/styles.xml?raw"))
192194
.default;
193195

196+
// Replace the default language in styles.xml with the provided locale.
197+
// If not provided, default to en-US.
198+
const resolvedLocale = (locale && locale.trim()) || "en-US";
199+
// Replace w:lang w:val="..." with the desired locale, leaving other attributes intact
200+
// This targets the run default language defined in styles.xml
201+
externalStyles = externalStyles.replace(
202+
/(<w:lang\b[^>]*\bw:val=")([^"]+)("[^>]*\/>)/g,
203+
`$1${resolvedLocale}$3`,
204+
);
205+
194206
const bullets = ["•"]; //, "◦", "▪"]; (these don't look great, just use solid bullet for now)
195207
return {
196208
numbering: {
@@ -247,6 +259,11 @@ export class DOCXExporter<
247259
options: {
248260
sectionOptions: Omit<ISectionOptions, "children">;
249261
documentOptions: DocumentOptions;
262+
/**
263+
* The document locale in OOXML format (e.g. en-US, fr-FR, de-DE).
264+
* If omitted, defaults to en-US.
265+
*/
266+
locale?: string;
250267
} = {
251268
sectionOptions: {},
252269
documentOptions: {},
@@ -276,13 +293,18 @@ export class DOCXExporter<
276293
options: {
277294
sectionOptions: Omit<ISectionOptions, "children">;
278295
documentOptions: DocumentOptions;
296+
/**
297+
* The document locale in OOXML format (e.g. en-US, fr-FR, de-DE).
298+
* If omitted, defaults to en-US.
299+
*/
300+
locale?: string;
279301
} = {
280302
sectionOptions: {},
281303
documentOptions: {},
282304
},
283305
) {
284306
const doc = new Document({
285-
...(await this.createDefaultDocumentOptions()),
307+
...(await this.createDefaultDocumentOptions(options.locale)),
286308
...options.documentOptions,
287309
sections: [
288310
{

0 commit comments

Comments
 (0)