Skip to content

Commit 1ebae57

Browse files
author
JelteMX
committed
Fix #14: Add translatable error messages
1 parent 811c812 commit 1ebae57

File tree

6 files changed

+65
-6
lines changed

6 files changed

+65
-6
lines changed

src/FileDropper.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,12 @@ class FileDropperContainer extends Component<FileDropperContainerProps, {}> {
321321
| undefined;
322322

323323
if (typeof verifyError !== "undefined" && verifyError !== "" && verifyError !== null) {
324+
const errorText = this.store.texts.FILESREJECTEDBYSERVER
325+
.replace(/%%FILENAME%%/g, file.name)
326+
.replace(/%%ERROR%%/g, verifyError);
327+
324328
this.store.addValidationMessage(
325-
new ValidationMessage(`File: '${file.name}' rejected: ${verifyError}`, "warning")
329+
new ValidationMessage(errorText, "warning")
326330
);
327331
this.store.deleteFile(file);
328332
return false;

src/FileDropper.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,33 @@ Note 3: To avoid any other problem, make sure this helper object is detached fro
294294
<translation lang="nl_NL">Are you sure you want to delete this file? This cannot be undone.</translation>
295295
</translations>
296296
</property>
297+
<property key="textFilesRejectedByServer" type="translatableString" required="false">
298+
<caption>Rejected by server</caption>
299+
<description>The %%FILENAME%% and %%ERROR%% are replaced by the widget</description>
300+
<translations>
301+
<translation lang="en_US">File: '%%FILENAME%%' rejected: %%ERROR%%</translation>
302+
<translation lang="en_GB">File: '%%FILENAME%%' rejected: %%ERROR%%</translation>
303+
<translation lang="nl_NL">File: '%%FILENAME%%' rejected: %%ERROR%%</translation>
304+
</translations>
305+
</property>
306+
<property key="textFilesRejected" type="translatableString" required="false">
307+
<caption>Rejected</caption>
308+
<description>Rejected files string</description>
309+
<translations>
310+
<translation lang="en_US">The following files are rejected:</translation>
311+
<translation lang="en_GB">The following files are rejected:</translation>
312+
<translation lang="nl_NL">The following files are rejected:</translation>
313+
</translations>
314+
</property>
315+
<property key="textFileRejectedSize" type="translatableString" required="false">
316+
<caption>File size rejected</caption>
317+
<description>String for when a file is rejected because of filesize. The %%FILENAME%% and %%MAXSIZE%% are replaced by the widget</description>
318+
<translations>
319+
<translation lang="en_US">File: '%%FILENAME%%' is rejected, file size exceeds %%MAXIZE%%</translation>
320+
<translation lang="en_GB">File: '%%FILENAME%%' is rejected, file size exceeds %%MAXIZE%%</translation>
321+
<translation lang="nl_NL">File: '%%FILENAME%%' is rejected, file size exceeds %%MAXIZE%%</translation>
322+
</translations>
323+
</property>
297324
</propertyGroup>
298325

299326
</properties>

src/components/FileDropper.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class FileDropper extends Component<FileDropperProps, {}> {
5858

5959
private onDrop(acceptedFiles: File[], fileRejections: FileRejection[]): void {
6060
const { store } = this.props;
61+
const { FILERECTEDSIZE, FILESREJECTED } = store.texts;
6162
const maxSize = store.maxSize || null;
6263
let maxReached = false;
6364

@@ -71,8 +72,12 @@ export class FileDropper extends Component<FileDropperProps, {}> {
7172
const otherRejected: File[] = [];
7273
fileRejections.forEach(reject => {
7374
if (maxSize !== null && reject && reject.file.size && reject.file.size > maxSize) {
75+
const text = FILERECTEDSIZE
76+
.replace(/%%FILENAME%%/g, reject.file.name)
77+
.replace(/%%MAXSIZE%%/g, fileSize(maxSize));
78+
7479
const message = new ValidationMessage(
75-
`File: '${reject.file.name}' is rejected, file size exceeds ${fileSize(maxSize)}`,
80+
text,
7681
"warning"
7782
);
7883
store.addValidationMessage(message);
@@ -83,7 +88,7 @@ export class FileDropper extends Component<FileDropperProps, {}> {
8388

8489
if (otherRejected.length > 0) {
8590
mx.ui.info(
86-
["The following files are rejected:", "", ...otherRejected.map(file => file.name)].join("\n"),
91+
[FILESREJECTED, "", ...otherRejected.map(file => file.name)].join("\n"),
8792
true
8893
);
8994
}

src/store/fileDropperFile.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ export class FileDropperFile implements IFileDropperFile {
5656
this.error = "File not saved, check logs";
5757
this.status = "error";
5858
}
59-
} catch (error) {
59+
// @ts-ignore
60+
} catch (error: any) {
6061
this.error = error;
6162
this.status = error;
6263
}
@@ -80,7 +81,8 @@ export class FileDropperFile implements IFileDropperFile {
8081
}
8182
this.hash = md5(base64);
8283
}
83-
} catch (error) {
84+
// @ts-ignore
85+
} catch (error: any) {
8486
this.status = "error";
8587
this.error = error.message || "unknown error";
8688
this.data = this.base64 = null;

src/util/texts.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ import { FileDropperContainerProps } from "../../typings/FileDropperProps";
33
export interface FileDropperTexts {
44
DROPZONE: string;
55
DROPZONEMAXIMUM: string;
6+
FILESREJECTED: string;
7+
FILESREJECTEDBYSERVER: string;
8+
FILERECTEDSIZE: string;
69
}
710

811
const defaultTexts: FileDropperTexts = {
912
DROPZONE: "Click me to add a file!",
10-
DROPZONEMAXIMUM: "Maximum amount for files reached, please consider removing files"
13+
DROPZONEMAXIMUM: "Maximum amount for files reached, please consider removing files",
14+
FILESREJECTED: "The following files are rejected:",
15+
FILESREJECTEDBYSERVER: "File: '%%FILENAME%%' rejected: %%ERROR%%",
16+
FILERECTEDSIZE: "File: '%%FILENAME%%' is rejected, file size exceeds %%MAXIZE%%"
1117
};
1218

1319
export const getTexts = (props: FileDropperContainerProps): FileDropperTexts => {
@@ -21,5 +27,17 @@ export const getTexts = (props: FileDropperContainerProps): FileDropperTexts =>
2127
texts.DROPZONEMAXIMUM = props.textDropZoneMaximum;
2228
}
2329

30+
if (props.textFilesRejected) {
31+
texts.FILESREJECTED = props.textFilesRejected;
32+
}
33+
34+
if (props.textFilesRejectedByServer) {
35+
texts.FILESREJECTEDBYSERVER = props.textFilesRejectedByServer;
36+
}
37+
38+
if (props.textFileRejectedSize) {
39+
texts.FILERECTEDSIZE = props.textFileRejectedSize;
40+
}
41+
2442
return texts;
2543
};

typings/FileDropperProps.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,7 @@ export interface FileDropperContainerProps extends CommonProps {
7575
textDropZone: string;
7676
textDropZoneMaximum: string;
7777
textDeleteFileConfirm: string;
78+
textFilesRejectedByServer: string;
79+
textFilesRejected: string;
80+
textFileRejectedSize: string;
7881
}

0 commit comments

Comments
 (0)