Skip to content

add locale configuration for docx export #1937

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 2 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
8 changes: 7 additions & 1 deletion packages/xl-docx-exporter/src/docx/docxExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ describe("exporter", () => {
}),
docxDefaultSchemaMappings,
);
const doc = await exporter.toDocxJsDocument(testDocument);
const doc = await exporter.toDocxJsDocument(testDocument, {
sectionOptions: {},
documentOptions: {},
locale: "en-US",
});

const blob = await Packer.toBlob(doc);
const zip = new ZipReader(new BlobReader(blob));
Expand Down Expand Up @@ -56,6 +60,7 @@ describe("exporter", () => {
);

const doc = await exporter.toDocxJsDocument(testDocument, {
locale: "en-US",
documentOptions: {
creator: "John Doe",
},
Expand Down Expand Up @@ -181,6 +186,7 @@ describe("exporter", () => {
],
},
]),
{ sectionOptions: {}, documentOptions: {}, locale: "en-US" },
);

const blob = await Packer.toBlob(doc);
Expand Down
27 changes: 24 additions & 3 deletions packages/xl-docx-exporter/src/docx/docxExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,21 @@ export class DOCXExporter<
];
}

protected async createDefaultDocumentOptions(): Promise<DocumentOptions> {
const externalStyles = (await import("./template/word/styles.xml?raw"))
protected async createDefaultDocumentOptions(
locale?: string,
): Promise<DocumentOptions> {
let externalStyles = (await import("./template/word/styles.xml?raw"))
.default;

// Replace the default language in styles.xml with the provided locale.
// If not provided, default to en-US.
const resolvedLocale = (locale && locale.trim()) || "en-US";

externalStyles = externalStyles.replace(
/(<w:lang\b[^>]*\bw:val=")([^"]+)("[^>]*\/>)/g,
`$1${resolvedLocale}$3`,
);

const bullets = ["•"]; //, "◦", "▪"]; (these don't look great, just use solid bullet for now)
return {
numbering: {
Expand Down Expand Up @@ -247,6 +258,11 @@ export class DOCXExporter<
options: {
sectionOptions: Omit<ISectionOptions, "children">;
documentOptions: DocumentOptions;
/**
* The document locale in OOXML format (e.g. en-US, fr-FR, de-DE).
* If omitted, defaults to en-US.
*/
locale?: string;
} = {
sectionOptions: {},
documentOptions: {},
Expand Down Expand Up @@ -276,13 +292,18 @@ export class DOCXExporter<
options: {
sectionOptions: Omit<ISectionOptions, "children">;
documentOptions: DocumentOptions;
/**
* The document locale in OOXML format (e.g. en-US, fr-FR, de-DE).
* If omitted, defaults to en-US.
*/
locale?: string;
} = {
sectionOptions: {},
documentOptions: {},
},
) {
const doc = new Document({
...(await this.createDefaultDocumentOptions()),
...(await this.createDefaultDocumentOptions(options.locale)),
...options.documentOptions,
sections: [
{
Expand Down
6 changes: 6 additions & 0 deletions shared/util/fileUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export async function loadFileBuffer(requireUrl: {
if (url.startsWith("/@fs/")) {
url = url.substring("/@fs".length);
}
// On Windows, vite/vitest may yield paths like "/C:/..." after removing /@fs
// Node on Windows treats paths starting with "/" as relative to current drive,
// which would produce "C:\C:\...". Strip leading slash when followed by a drive letter.
if (/^\/[A-Za-z]:/.test(url)) {
url = url.slice(1);
}
const buffer = fs.readFileSync(url);
return buffer;
} else {
Expand Down