@@ -10,26 +10,13 @@ import Button from "../../Button/Button";
1010import NameErrorMessage from "../ErrorMessage/NameErrorMessage" ;
1111import store from "../../../app/store" ;
1212import ApiCallHandler from "../../../utils/apiCallHandler" ;
13+ import { useTranslation } from "react-i18next" ;
14+ import { allowedExtensionsString } from "../../../utils/allowedExtensionsString" ;
1315
1416const allowedExtensions = {
1517 python : [ "jpg" , "jpeg" , "png" , "gif" ] ,
1618} ;
1719
18- const allowedExtensionsString = ( projectType ) => {
19- const extensionsList = allowedExtensions [ projectType ] ;
20- if ( extensionsList . length === 1 ) {
21- return `'.${ extensionsList [ 0 ] } '` ;
22- } else {
23- return (
24- `'.` +
25- extensionsList . slice ( 0 , - 1 ) . join ( `', '.` ) +
26- `' or '.` +
27- extensionsList [ extensionsList . length - 1 ] +
28- `'`
29- ) ;
30- }
31- } ;
32-
3320const ImageUploadButton = ( { reactAppApiEndpoint } ) => {
3421 const [ modalIsOpen , setIsOpen ] = useState ( false ) ;
3522 const [ files , setFiles ] = useState ( [ ] ) ;
@@ -41,6 +28,7 @@ const ImageUploadButton = ({ reactAppApiEndpoint }) => {
4128 const projectImages = useSelector ( ( state ) => state . editor . project . image_list ) ;
4229 const imageNames = projectImages . map ( ( image ) => `${ image . filename } ` ) ;
4330 const user = useSelector ( ( state ) => state . auth . user ) ;
31+ const { t } = useTranslation ( ) ;
4432
4533 const closeModal = ( ) => {
4634 setFiles ( [ ] ) ;
@@ -62,19 +50,27 @@ const ImageUploadButton = ({ reactAppApiEndpoint }) => {
6250 imageNames . includes ( fileName ) ||
6351 files . filter ( ( file ) => file . name === fileName ) . length > 1
6452 ) {
65- dispatch ( setNameError ( "Image names must be unique." ) ) ;
53+ dispatch (
54+ setNameError ( t ( "imageUploadButton.errors.imageNameNotUnique" ) ) ,
55+ ) ;
6656 return false ;
6757 } else if ( isValidFileName ( fileName , files ) ) {
6858 return true ;
6959 } else if ( ! allowedExtensions [ projectType ] . includes ( extension ) ) {
7060 dispatch (
7161 setNameError (
72- `Image names must end in ${ allowedExtensionsString ( projectType ) } .` ,
62+ t ( "errors.invalidImageExtension" , {
63+ extensions : allowedExtensionsString (
64+ projectType ,
65+ t ,
66+ allowedExtensions ,
67+ ) ,
68+ } ) ,
7369 ) ,
7470 ) ;
7571 return false ;
7672 } else {
77- dispatch ( setNameError ( "Error " ) ) ;
73+ dispatch ( setNameError ( "imageUploadButton.error " ) ) ;
7874 return false ;
7975 }
8076 } ) ;
@@ -119,7 +115,7 @@ const ImageUploadButton = ({ reactAppApiEndpoint }) => {
119115 return (
120116 < >
121117 < Button
122- buttonText = "Upload Image"
118+ buttonText = { t ( "imageUploadButton.uploadImage" ) }
123119 onClickHandler = { showModal }
124120 className = "proj-image-upload-button"
125121 />
@@ -128,10 +124,10 @@ const ImageUploadButton = ({ reactAppApiEndpoint }) => {
128124 isOpen = { modalIsOpen }
129125 onRequestClose = { closeModal }
130126 style = { customStyles }
131- contentLabel = "Upload Image"
127+ contentLabel = { t ( "imageUploadButton.uploadImage" ) }
132128 appElement = { document . getElementById ( "root" ) || undefined }
133129 >
134- < h2 > Upload an image </ h2 >
130+ < h2 > { t ( "imageUploadButton.uploadAnImage" ) } </ h2 >
135131
136132 < NameErrorMessage />
137133 < Dropzone
@@ -143,9 +139,7 @@ const ImageUploadButton = ({ reactAppApiEndpoint }) => {
143139 < section >
144140 < div { ...getRootProps ( ) } className = "dropzone-area" >
145141 < input { ...getInputProps ( ) } />
146- < p className = "dropzone-info" >
147- Drag and drop images here, or click to select images from file
148- </ p >
142+ < p className = "dropzone-info" > { t ( "imageUploadButton.info" ) } </ p >
149143 { files . map ( ( file , i ) => (
150144 < p key = { i } > { file . name } </ p >
151145 ) ) }
@@ -154,8 +148,14 @@ const ImageUploadButton = ({ reactAppApiEndpoint }) => {
154148 ) }
155149 </ Dropzone >
156150 < div className = "modal-footer" >
157- < Button buttonText = "Cancel" onClickHandler = { closeModal } />
158- < Button buttonText = "Save" onClickHandler = { saveImages } />
151+ < Button
152+ buttonText = { t ( "imageUploadButton.cancel" ) }
153+ onClickHandler = { closeModal }
154+ />
155+ < Button
156+ buttonText = { t ( "imageUploadButton.save" ) }
157+ onClickHandler = { saveImages }
158+ />
159159 </ div >
160160 </ Modal >
161161 </ >
0 commit comments