From d8f303178c1d304ef95af4ab0a71dcf4a6f8524a Mon Sep 17 00:00:00 2001 From: Candy Date: Mon, 25 Jul 2016 12:45:38 -0400 Subject: [PATCH] Add globalization --- .gitignore | 3 +++ index.js | 3 +++ intl/en/messages.json | 15 +++++++++++++++ lib/connector.js | 4 +++- lib/sql.js | 32 +++++++++++++++++++------------- package.json | 3 ++- 6 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 intl/en/messages.json diff --git a/.gitignore b/.gitignore index 2db30da..66b57f5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ node_modules checkstyle.xml loopback-connector-*.tgz +!intl/ +intl/* +!intl/en/ \ No newline at end of file diff --git a/index.js b/index.js index 7b038af..00bb7fd 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,9 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +var SG = require('strong-globalize'); +SG.SetRootDir(__dirname); + exports.Connector = require('./lib/connector'); // Set up SqlConnector as an alias to SQLConnector exports.SQLConnector = exports.SqlConnector = require('./lib/sql'); diff --git a/intl/en/messages.json b/intl/en/messages.json new file mode 100644 index 0000000..100274c --- /dev/null +++ b/intl/en/messages.json @@ -0,0 +1,15 @@ +{ + "8dc8fd7cc38a39896c03663cca38aad5": "execute() must be implemented by the connector", + "06c539e08213828e62e7f5339fb255ec": "{{getPlaceholderForValue()}} must be implemented by the connector", + "2b051e39b1efd1f2106b155495087b0f": "id value is required", + "30a4fa7fc115109b8cd096d895e9869e": "{{getPlaceholderForIdentifier()}} must be implemented by the connector", + "4cbf9bb7e7347b86c1dba4e54163c4ea": "{{getCountForAffectedRows()}} must be implemented by the connector", + "871b10e82a2dc1d0659571c5dee8fa95": "{{executeSQL()}} must be implemented by the connector", + "956ede286087599213bc112b5a76408b": "{{applyPagination()}} must be implemented by the connector", + "bca74e4fbb2859c22381fb531a0f896d": "{{getInsertedId()}} must be implemented by the connector", + "c491cd5577a54bf265d407ddaa20ffcb": "{{escapeValue()}} must be implemented by the connector", + "e54d944c2a2c85a23caa86027ae307cf": "Cannot migrate models not attached to this datasource: {0}", + "f3e021460b7a6f5991fca890b786a11b": "{{escapeName()}} must be implemented by the connector", + "fe1c27354af9f4c88bf9c1d246e41230": "{{toColumnValue()}} must be implemented by the connector", + "fe914555563f5f566adfc0c5db8445db": "{{fromColumnValue()}} must be implemented by the connector" +} diff --git a/lib/connector.js b/lib/connector.js index 39be3ca..57606a4 100644 --- a/lib/connector.js +++ b/lib/connector.js @@ -3,6 +3,8 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +var SG = require('strong-globalize'); +var g = SG(); var debug = require('debug')('loopback:connector'); module.exports = Connector; @@ -80,7 +82,7 @@ Connector.prototype.getMetadata = function() { * @param {Function} [callback] The callback function */ Connector.prototype.execute = function(command, params, options, callback) { - throw new Error('execute() must be implemented by the connector'); + throw new Error(g.f('execute() must be implemented by the connector')); }; /** diff --git a/lib/sql.js b/lib/sql.js index 2c82a47..1a02407 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -3,6 +3,9 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +var SG = require('strong-globalize'); +var g = SG(); + var util = require('util'); var async = require('async'); var assert = require('assert'); @@ -1229,7 +1232,7 @@ Connector.defineAliases(SQLConnector.prototype, 'all', ['findAll']); SQLConnector.prototype.find = function(model, id, options, cb) { if (id == null) { process.nextTick(function() { - var err = new Error('id value is required'); + var err = new Error(g.f('id value is required')); if (cb) { cb(err); } @@ -1329,8 +1332,8 @@ SQLConnector.prototype.automigrate = function(models, cb) { }); if (invalidModels.length) { return process.nextTick(function() { - cb(new Error('Cannot migrate models not attached to this datasource: ' + - invalidModels.join(' '))); + cb(new Error(g.f('Cannot migrate models not attached to this datasource: %s', + invalidModels.join(' ')))); }); } @@ -1398,7 +1401,7 @@ SQLConnector.prototype.escapeObject = function(obj) { * */ SQLConnector.prototype.toColumnValue = function(propertyDef, value) { - throw new Error('toColumnValue() must be implemented by the connector'); + throw new Error(g.f('{{toColumnValue()}} must be implemented by the connector')); }; /** @@ -1408,7 +1411,7 @@ SQLConnector.prototype.toColumnValue = function(propertyDef, value) { * @returns {*} Model property value */ SQLConnector.prototype.fromColumnValue = function(propertyDef, value) { - throw new Error('fromColumnValue() must be implemented by the connector'); + throw new Error(g.f('{{fromColumnValue()}} must be implemented by the connector')); }; /** @@ -1417,7 +1420,7 @@ SQLConnector.prototype.fromColumnValue = function(propertyDef, value) { * @returns {String} An escaped name for SQL */ SQLConnector.prototype.escapeName = function(name) { - throw new Error('escapeName() must be implemented by the connector'); + throw new Error(g.f('{{escapeName()}} must be implemented by the connector')); }; /** @@ -1426,7 +1429,7 @@ SQLConnector.prototype.escapeName = function(name) { * @returns {*} An escaped value for SQL */ SQLConnector.prototype.escapeValue = function(value) { - throw new Error('escapeValue() must be implemented by the connector'); + throw new Error(g.f('{{escapeValue()}} must be implemented by the connector')); }; /** @@ -1435,7 +1438,8 @@ SQLConnector.prototype.escapeValue = function(value) { * @returns {String} The place holder */ SQLConnector.prototype.getPlaceholderForIdentifier = function(key) { - throw new Error('getPlaceholderForIdentifier() must be implemented by the connector'); + throw new Error(g.f('{{getPlaceholderForIdentifier()}} must be implemented by ' + + 'the connector')); }; /** @@ -1444,7 +1448,8 @@ SQLConnector.prototype.getPlaceholderForIdentifier = function(key) { * @returns {String} The place holder */ SQLConnector.prototype.getPlaceholderForValue = function(key) { - throw new Error('getPlaceholderForValue() must be implemented by the connector'); + throw new Error(g.f('{{getPlaceholderForValue()}} must be implemented by ' + + 'the connector')); }; /** @@ -1454,7 +1459,7 @@ SQLConnector.prototype.getPlaceholderForValue = function(key) { * @param {Object} filter The filter object from the query */ SQLConnector.prototype.applyPagination = function(model, stmt, filter) { - throw new Error('applyPagination() must be implemented by the connector'); + throw new Error(g.f('{{applyPagination()}} must be implemented by the connector')); }; /** @@ -1465,7 +1470,8 @@ SQLConnector.prototype.applyPagination = function(model, stmt, filter) { * @returns {Number} Number of rows affected */ SQLConnector.prototype.getCountForAffectedRows = function(model, info) { - throw new Error('getCountForAffectedRows() must be implemented by the connector'); + throw new Error(g.f('{{getCountForAffectedRows()}} must be implemented by ' + + 'the connector')); }; /** @@ -1475,7 +1481,7 @@ SQLConnector.prototype.getCountForAffectedRows = function(model, info) { * @returns {*} The inserted id value */ SQLConnector.prototype.getInsertedId = function(model, info) { - throw new Error('getInsertedId() must be implemented by the connector'); + throw new Error(g.f('{{getInsertedId()}} must be implemented by the connector')); }; /** @@ -1486,5 +1492,5 @@ SQLConnector.prototype.getInsertedId = function(model, info) { * @param {Function} [callback] The callback function */ SQLConnector.prototype.executeSQL = function(sql, params, options, callback) { - throw new Error('executeSQL() must be implemented by the connector'); + throw new Error(g.f('{{executeSQL()}} must be implemented by the connector')); }; diff --git a/package.json b/package.json index f4ae973..1e985ce 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "license": "MIT", "dependencies": { "async": "^1.0.0", - "debug": "^2.2.0" + "debug": "^2.2.0", + "strong-globalize": "^2.5.8" }, "devDependencies": { "chai": "~2.3.0",