Selaa lähdekoodia

aws-s3: add default locale for MiniXHRUpload, fixes #2459 (#2477)

Renée Kooi 4 vuotta sitten
vanhempi
commit
bf48826dba

+ 1 - 0
packages/@uppy/aws-s3/src/MiniXHRUpload.js

@@ -45,6 +45,7 @@ module.exports = class MiniXHRUpload {
 
 
     this.requests = opts.__queue
     this.requests = opts.__queue
     this.uploaderEvents = Object.create(null)
     this.uploaderEvents = Object.create(null)
+    this.i18n = opts.i18n
   }
   }
 
 
   _getOptions (file) {
   _getOptions (file) {

+ 23 - 1
packages/@uppy/aws-s3/src/index.js

@@ -28,6 +28,7 @@
 // If global `URL` constructor is available, use it
 // If global `URL` constructor is available, use it
 const URL_ = typeof URL === 'function' ? URL : require('url-parse')
 const URL_ = typeof URL === 'function' ? URL : require('url-parse')
 const { Plugin } = require('@uppy/core')
 const { Plugin } = require('@uppy/core')
+const Translator = require('@uppy/utils/lib/Translator')
 const RateLimitedQueue = require('@uppy/utils/lib/RateLimitedQueue')
 const RateLimitedQueue = require('@uppy/utils/lib/RateLimitedQueue')
 const settle = require('@uppy/utils/lib/settle')
 const settle = require('@uppy/utils/lib/settle')
 const hasProperty = require('@uppy/utils/lib/hasProperty')
 const hasProperty = require('@uppy/utils/lib/hasProperty')
@@ -78,6 +79,12 @@ module.exports = class AwsS3 extends Plugin {
     this.id = this.opts.id || 'AwsS3'
     this.id = this.opts.id || 'AwsS3'
     this.title = 'AWS S3'
     this.title = 'AWS S3'
 
 
+    this.defaultLocale = {
+      strings: {
+        timedOut: 'Upload stalled for %{seconds} seconds, aborting.'
+      }
+    }
+
     const defaultOptions = {
     const defaultOptions = {
       timeout: 30 * 1000,
       timeout: 30 * 1000,
       limit: 0,
       limit: 0,
@@ -87,11 +94,24 @@ module.exports = class AwsS3 extends Plugin {
 
 
     this.opts = { ...defaultOptions, ...opts }
     this.opts = { ...defaultOptions, ...opts }
 
 
+    this.i18nInit()
+
     this.client = new RequestClient(uppy, opts)
     this.client = new RequestClient(uppy, opts)
     this.handleUpload = this.handleUpload.bind(this)
     this.handleUpload = this.handleUpload.bind(this)
     this.requests = new RateLimitedQueue(this.opts.limit)
     this.requests = new RateLimitedQueue(this.opts.limit)
   }
   }
 
 
+  setOptions (newOpts) {
+    super.setOptions(newOpts)
+    this.i18nInit()
+  }
+
+  i18nInit () {
+    this.translator = new Translator([this.defaultLocale, this.uppy.locale, this.opts.locale])
+    this.i18n = this.translator.translate.bind(this.translator)
+    this.setPluginState() // so that UI re-renders and we see the updated locale
+  }
+
   getUploadParameters (file) {
   getUploadParameters (file) {
     if (!this.opts.companionUrl) {
     if (!this.opts.companionUrl) {
       throw new Error('Expected a `companionUrl` option containing a Companion address.')
       throw new Error('Expected a `companionUrl` option containing a Companion address.')
@@ -262,10 +282,12 @@ module.exports = class AwsS3 extends Plugin {
       getResponseError: defaultGetResponseError
       getResponseError: defaultGetResponseError
     }
     }
 
 
+    // Only for MiniXHRUpload, remove once we can depend on XHRUpload directly again
+    xhrOptions.i18n = this.i18n
+
     // Revert to `this.uppy.use(XHRUpload)` once the big comment block at the top of
     // Revert to `this.uppy.use(XHRUpload)` once the big comment block at the top of
     // this file is solved
     // this file is solved
     this._uploader = new MiniXHRUpload(this.uppy, xhrOptions)
     this._uploader = new MiniXHRUpload(this.uppy, xhrOptions)
-    this._uploader.i18n = this.uppy.i18n
   }
   }
 
 
   uninstall () {
   uninstall () {