From 0ec3bff0563cfc11b45a70209e1d7f0048cb946e Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Thu, 21 Jul 2016 15:14:55 -0700 Subject: [PATCH] Add globalization --- .gitignore | 4 ++++ index.js | 2 ++ intl/en/messages.json | 13 +++++++++++++ lib/payload.js | 19 +++++++++++-------- lib/providers/apns.js | 7 ++++--- lib/providers/gcm.js | 5 ++++- lib/push-manager.js | 8 +++++--- package.json | 3 ++- 8 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 intl/en/messages.json diff --git a/.gitignore b/.gitignore index 16bec84..d2177a1 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ xunit.xml checkstyle.xml example/server-2.0/client/public/vendor doc + +!intl/ +intl/* +!intl/en/ diff --git a/index.js b/index.js index b8a5042..beaaaec 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,8 @@ var loopback = require('loopback'); var PushConnector = require('./lib/push-connector'); exports = module.exports = PushConnector; +var SG = require('strong-globalize'); +SG.SetRootDir(__dirname); /** * Export two model classes as properties diff --git a/intl/en/messages.json b/intl/en/messages.json new file mode 100644 index 0000000..349d436 --- /dev/null +++ b/intl/en/messages.json @@ -0,0 +1,13 @@ +{ + "0a11bfdd7655e825fbd1f998b2e24db9": "Empty payload", + "71a2b601d1e750aa05c7a28ed76b9973": "Invalid field (empty)", + "8bc87eb582af546d32505f2839234119": "Invalid payload", + "db1e3eb9bf0934d72eb8b0b110281f39": "Invalid field: {0}", + "e254b20a16461c7379202f6a5f8350cc": "Invalid variable type for ${{0}}", + "f5146dece5cd70db519daf8c7e6d8478": "The ${{0}} does not exist", + "f95c75ae3da1e88794fff4be7188f3dc": "Invalid value for `{0}`", + "e9023b2ab0a052c9ccbd257198c7429f": "Cannot send {{APNS}} notification: {0}", + "704e7bf9910b8532f7c4889366fdcbac": "{{GCM}} error code: {0}, deviceToken: {1}", + "033d31e7bd62a420f23e4b154945a2b8": "notification must be an object", + "1ec12f42dab8979b9fc90a95a023419e": "deviceTokens must be an array" +} diff --git a/lib/payload.js b/lib/payload.js index 2333e9b..2bbb976 100644 --- a/lib/payload.js +++ b/lib/payload.js @@ -6,6 +6,8 @@ // this file has no tests so avoid refactor and ignore jshint for now /*jshint ignore: start */ var serial,__hasProp = {}.hasOwnProperty; +var SG = require('strong-globalize'); +var g = SG(); serial = 0; @@ -14,7 +16,7 @@ Payload.prototype.locale_format = /^[a-z]{2}_[A-Z]{2}$/; function Payload(data) { var key, prefix, subkey, sum, type, value, _i, _len, _ref, _ref1; if (typeof data !== 'object') { - throw new Error('Invalid payload'); + throw new Error(g.t('Invalid payload')); } this.id = serial++; this.compiled = false; @@ -26,11 +28,12 @@ function Payload(data) { if (!__hasProp.call(data, key)) continue; value = data[key]; if (typeof key !== 'string' || key.length === 0) { - throw new Error("Invalid field (empty)"); + throw new Error(g.t("Invalid field (empty)")); } if (typeof value !== 'string') { - throw new Error("Invalid value for `" + key + "'"); + throw new Error(g.f("Invalid value for `%s`", key)); } + switch (key) { case 'title': this.title["default"] = value; @@ -45,7 +48,7 @@ function Payload(data) { if ((_ref = key.split('.', 2), prefix = _ref[0], subkey = _ref[1], _ref).length === 2) { this[prefix][subkey] = value; } else { - throw new Error("Invalid field: " + key); + throw new Error(g.f("Invalid field: %s", key)); } } } @@ -65,7 +68,7 @@ function Payload(data) { }).call(this)).length; } if (sum === 0) { - throw new Error('Empty payload'); + throw new Error(g.t('Empty payload')); } } @@ -118,15 +121,15 @@ Payload.prototype.variable = function (keyPath) { if ((_ref = this.event) != null ? _ref.name : undefined) { return (_ref1 = this.event) != null ? _ref1.name : undefined; } else { - throw new Error("The ${" + keyPath + "} does not exist"); + throw new Error(g.f("The ${%s} does not exist", keyPath)); } } _ref2 = keyPath.split('.', 2), prefix = _ref2[0], key = _ref2[1]; if (prefix !== 'var' && prefix !== 'data') { - throw new Error("Invalid variable type for ${" + keyPath + "}"); + throw new Error(g.f("Invalid variable type for ${%s}", keyPath)); } if (this[prefix][key] == null) { - throw new Error("The ${" + keyPath + "} does not exist"); + throw new Error(g.f("The ${%s} does not exist", keyPath)); } return this[prefix][key]; }; diff --git a/lib/providers/apns.js b/lib/providers/apns.js index 5e48d3a..4ef1a37 100644 --- a/lib/providers/apns.js +++ b/lib/providers/apns.js @@ -5,9 +5,10 @@ var inherits = require('util').inherits; var EventEmitter = require('events').EventEmitter; - var debug = require('debug')('loopback:component:push:provider:apns'); var apn = require('apn'); +var SG = require('strong-globalize'); +var g = SG(); function ApnsProvider(pushSettings) { pushSettings = pushSettings || {}; @@ -83,7 +84,7 @@ ApnsProvider.prototype._setupPushConnection = function(options) { connection.on('socketError', errorHandler); connection.on('transmissionError', function(code, notification, recipient) { - var err = new Error('Cannot send APNS notification: ' + code); + var err = new Error(g.f('Cannot send {{APNS}} notification: %s', code)); self.emit(err, notification, recipient); }); @@ -140,4 +141,4 @@ ApnsProvider.prototype.pushNotification = function(notification, deviceToken) { debug('Pushing notification to %j:', deviceToken, note); this._connection.pushNotification(note, deviceToken); -}; \ No newline at end of file +}; diff --git a/lib/providers/gcm.js b/lib/providers/gcm.js index 4b3dde7..b96cc4a 100644 --- a/lib/providers/gcm.js +++ b/lib/providers/gcm.js @@ -8,6 +8,8 @@ var extend = require('util')._extend; var EventEmitter = require('events').EventEmitter; var gcm = require('node-gcm'); var debug = require('debug')('loopback:component:push:provider:gcm'); +var SG = require('strong-globalize'); +var g = SG(); function GcmProvider(pushSettings) { var settings = pushSettings.gcm || {}; @@ -39,7 +41,8 @@ GcmProvider.prototype.pushNotification = function(notification, deviceToken) { debug('Device %j is no longer registered.', registrationIds[index]); devicesGoneRegistrationIds.push(registrationIds[index]); } else if (code) { - errors.push('GCM error code: ' + (code || 'Unknown') + ', deviceToken: ' + registrationIds[index]); + errors.push(g.f('{{GCM}} error code: %s, deviceToken: %s', + (code || 'Unknown'), registrationIds[index])); } }); diff --git a/lib/push-manager.js b/lib/push-manager.js index e85722e..5c9aa2c 100644 --- a/lib/push-manager.js +++ b/lib/push-manager.js @@ -12,6 +12,8 @@ var providers = require('./providers'); var loopback = require('loopback'); var NodeCache = require('node-cache'); var debug = require('debug')('loopback:component:push:push-manager'); +var SG = require('strong-globalize'); +var g = SG(); var Installation = require('../models').Installation; var Notification = require('../models').Notification; @@ -277,7 +279,7 @@ PushManager.prototype.notify = function(installation, notification, cb) { assert(cb, 'callback should be defined'); if(!(typeof notification === 'object' && notification)) { - return cb(new Error('notification must be an object')); + return cb(new Error(g.t('notification must be an object'))); } var appId = installation.appId; @@ -327,11 +329,11 @@ PushManager.prototype.notifyMany = function(appId, deviceType, deviceTokens, not assert(cb, 'callback should be defined'); if(!(typeof notification === 'object' && notification)) { - return cb(new Error('notification must be an object')); + return cb(new Error(g.t('notification must be an object'))); } if(!(Array.isArray(deviceTokens) && deviceTokens.length > 0)) { - return cb(new Error('deviceTokens must be an array')); + return cb(new Error(g.t('deviceTokens must be an array'))); } // Normalize the notification from a plain object diff --git a/package.json b/package.json index f99df5e..3dea31f 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "lodash": "^3.10.1", "mpns": "^2.1.0", "node-cache": "^3.2.1", - "node-gcm": "^0.14.0" + "node-gcm": "^0.14.0", + "strong-globalize": "^2.5.6" }, "devDependencies": { "chai": "^2.3.0",