UNPKG

@salesforce/apex-node

Version:

Salesforce JS library for Apex

105 lines 3.92 kB
"use strict"; /* * Copyright (c) 2020, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Message = exports.Localization = void 0; const util = __importStar(require("util")); const MISSING_LABEL_MSG = '!!! MISSING LABEL !!!'; class Localization { delegate; constructor(delegate) { this.delegate = delegate; } localize(label, args) { return this.delegate.localize(label, args); } } exports.Localization = Localization; class Message { messages; constructor(messages) { this.messages = messages; } localize(label, args) { let possibleLabel = this.getLabel(label); if (typeof args === 'undefined' && possibleLabel) { return possibleLabel; } // Note: We are getting an array of arrays when we want to consume the ...args param // e.g. [['value1', 'value2']] if (typeof args !== 'undefined' && Array.isArray(args)) { args = args[0].constructor === Array ? args[0] : args; } else if (typeof args !== 'undefined' && !Array.isArray(args)) { args = [args]; } if (!possibleLabel) { console.warn(`Missing label for key: ${label}`); possibleLabel = `${MISSING_LABEL_MSG} ${label}`; if (Array.isArray(args) && args.length >= 1) { args.forEach((arg) => { possibleLabel += ` (${arg})`; }); } return possibleLabel; } if (Array.isArray(args) && args.length >= 1) { const expectedNumArgs = possibleLabel.split('%s').length - 1; if (args.length !== expectedNumArgs) { // just log it, we might want to hide some in some languges on purpose console.log(`Arguments do not match for label '${label}', got ${args.length} but want ${expectedNumArgs}`); } args.unshift(possibleLabel); // eslint-disable-next-line prefer-spread return util.format.apply(util, args); } return possibleLabel; } getLabel(label) { if (this.messages[label]) { return this.messages[label]; } else { return undefined; } } } exports.Message = Message; //# sourceMappingURL=localization.js.map