From d1035d54457f4b46a407423c153f4765e6be3890 Mon Sep 17 00:00:00 2001 From: Richard Pringle Date: Tue, 26 Jul 2016 14:13:30 -0400 Subject: [PATCH] Add globalization to module. --- .gitignore | 4 ++++ index.js | 3 +++ intl/en/messages.json | 10 ++++++++++ lib/providers/filesystem/index.js | 15 +++++++++------ lib/storage-handler.js | 9 ++++++--- package.json | 5 +++-- 6 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 intl/en/messages.json diff --git a/.gitignore b/.gitignore index 40db2c9..1ae23b6 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,7 @@ npm-debug.log .idea node_modules providers-private.json + +!intl/ +intl/* +!intl/en/ diff --git a/index.js b/index.js index 2b5656a..64407e2 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,9 @@ // This file is licensed under the Artistic License 2.0. // License text available at https://opensource.org/licenses/Artistic-2.0 +var SG = require('strong-globalize'); +SG.SetRootDir(__dirname); + var StorageConnector = require('./lib/storage-connector'); StorageConnector.StorageService = require('./lib/storage-service'); diff --git a/intl/en/messages.json b/intl/en/messages.json new file mode 100644 index 0000000..41f4dca --- /dev/null +++ b/intl/en/messages.json @@ -0,0 +1,10 @@ +{ + "2eb418c4dc7f7a3e989bb71a8f5388d7": "{{FileSystemProvider}}: Path does not exist: {0}", + "6af59b6408b92f4c6b13a2c9b06379f2": "{{FileSystemProvider}}: Invalid name: {0}", + "95065f7f9499f75f49e3714aa4e2031d": "{{FileSystemProvider}}: Invalid name: ", + "c9fb0aba850059a14f4ed5e045e4ec3e": "Invalid name: {0}", + "f589fe721f4e6fa112d1f66081ed29ac": "{{FileSystemProvider}}: Invalid directory: {0}", + "45c1c136e750c62179d75a1c99151281": "{{maxFileSize}} exceeded, received {0} bytes of field data (max is {1})", + "78f6f36e8300e15cff778496fb1dd178": "{{contentType}} \"{0}\" is not allowed (Must be in [{1}])", + "b8a9e184534171cf66caf58d29ad76f5": "{{maxFieldsSize}} exceeded, received {0} bytes of field data" +} diff --git a/lib/providers/filesystem/index.js b/lib/providers/filesystem/index.js index 0c27129..2c4e98c 100644 --- a/lib/providers/filesystem/index.js +++ b/lib/providers/filesystem/index.js @@ -3,6 +3,9 @@ // This file is licensed under the Artistic License 2.0. // License text available at https://opensource.org/licenses/Artistic-2.0 +// Globalization +var g = require('strong-globalize')(); + /** * File system based on storage provider */ @@ -27,11 +30,11 @@ function FileSystemProvider(options) { this.root = options.root; var exists = fs.existsSync(this.root); if (!exists) { - throw new Error('FileSystemProvider: Path does not exist: ' + this.root); + throw new Error(g.f('{{FileSystemProvider}}: Path does not exist: %s', this.root)); } var stat = fs.statSync(this.root); if (!stat.isDirectory()) { - throw new Error('FileSystemProvider: Invalid directory: ' + this.root); + throw new Error(g.f('{{FileSystemProvider}}: Invalid directory: %s', this.root)); } } @@ -39,9 +42,9 @@ var namePattern = new RegExp('[^' + path.sep + '/]+'); function validateName(name, cb) { if (!name) { - cb && process.nextTick(cb.bind(null, new Error('Invalid name: ' + name))); + cb && process.nextTick(cb.bind(null, new Error(g.f('Invalid name: %s', name)))); if (!cb) { - console.error('FileSystemProvider: Invalid name: ', name); + console.error(g.f('{{FileSystemProvider}}: Invalid name: %s', name)); } return false; } @@ -50,9 +53,9 @@ function validateName(name, cb) { return true; } else { cb && process.nextTick(cb.bind(null, - new Error('FileSystemProvider: Invalid name: ' + name))); + new Error(g.f('{{FileSystemProvider}}: Invalid name: %s', name)))); if (!cb) { - console.error('FileSystemProvider: Invalid name: ', name); + console.error(g.f('{{FileSystemProvider}}: Invalid name: ', name)); } return false; } diff --git a/lib/storage-handler.js b/lib/storage-handler.js index dd0d2a5..52d1d25 100644 --- a/lib/storage-handler.js +++ b/lib/storage-handler.js @@ -3,6 +3,9 @@ // This file is licensed under the Artistic License 2.0. // License text available at https://opensource.org/licenses/Artistic-2.0 +// Globalization +var g = require('strong-globalize')(); + var IncomingForm = require('formidable'); var StringDecoder = require('string_decoder').StringDecoder; var path = require('path'); @@ -43,7 +46,7 @@ exports.upload = function(provider, req, res, options, cb) { part.on('data', function(buffer) { self._fieldsSize += buffer.length; if (self._fieldsSize > self.maxFieldsSize) { - self._error(new Error('maxFieldsSize exceeded, received ' + self._fieldsSize + ' bytes of field data')); + self._error(new Error(g.f('{{maxFieldsSize}} exceeded, received %s bytes of field data', self._fieldsSize))); return; } value += decoder.write(buffer); @@ -88,7 +91,7 @@ exports.upload = function(provider, req, res, options, cb) { } if (Array.isArray(allowedContentTypes) && allowedContentTypes.length !== 0) { if (allowedContentTypes.indexOf(file.type) === -1) { - self._error(new Error('contentType "' + file.type + '" is not allowed (Must be in [' + allowedContentTypes.join(', ') + '])')); + self._error(new Error(g.f('{{contentType}} "%s" is not allowed (Must be in [%s])', file.type, allowedContentTypes.join(', ')))); return; } } @@ -155,7 +158,7 @@ exports.upload = function(provider, req, res, options, cb) { // We are missing some way to tell the provider to cancel upload/multipart upload of the current file. // - s3-upload-stream doesn't provide a way to do this in it's public interface // - We could call provider.delete file but it would not delete multipart data - self._error(new Error('maxFileSize exceeded, received ' + fileSize + ' bytes of field data (max is ' + maxFileSize + ')')); + self._error(new Error(g.f('{{maxFileSize}} exceeded, received %s bytes of field data (max is %s)', fileSize, maxFileSize))); return; } }); diff --git a/package.json b/package.json index a6bb917..b36bb96 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,10 @@ "test": "./node_modules/.bin/mocha --timeout 30000 test/*test.js" }, "dependencies": { - "pkgcloud": "^1.1.0", "async": "^0.9.0", - "formidable": "^1.0.16" + "formidable": "^1.0.16", + "pkgcloud": "^1.1.0", + "strong-globalize": "^2.6.2" }, "devDependencies": { "express": "^4.11.0",