DashboardStringService.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['underscore', '../../app/nls/StringResources', '../../lib/@waca/core-client/js/core-client/utils/Lexicon'], function (_, DashboardStringResources, Lexicon) {
  8. var _stringService = {
  9. /**
  10. * @param {object} options
  11. */
  12. init: function init() /*options*/{
  13. this.polys = [{
  14. priority: 100,
  15. resource: DashboardStringResources
  16. }];
  17. },
  18. /**
  19. * Register a new StringResource bundle.
  20. *
  21. * @param {object} stringResource The StringResource to register. Must implement a get(key, interpolationOptions)
  22. * function which returns the key if the resource was not found
  23. * @param {number} priority Priority (number) given to this new resource, lower priority is more important.
  24. * Default resource has priority 100.
  25. */
  26. register: function register(stringResource, priority) {
  27. var newPoly = {
  28. priority: priority,
  29. resource: stringResource
  30. };
  31. if (_.find(this.polys, function (poly) {
  32. return poly.priority === priority && poly.resource === stringResource;
  33. })) {
  34. // We allow duplicate priorities and duplicate resources. Just not both.
  35. return;
  36. }
  37. var newIndex = _.sortedIndex(this.polys, newPoly, 'priority');
  38. this.polys.splice(newIndex, 0, newPoly);
  39. },
  40. /**
  41. * Get the string resource for the given key and interpolation options. Checks the Storytelling resources first and if
  42. * not found it checks the Dashboard resources.
  43. *
  44. * @param {string} key The key of the string to return
  45. * @param {object} interpolationOptions Optional interpolation options (see poly.t documentation for details)
  46. * @returns {string} The string to display
  47. */
  48. get: function get(key, interpolationOptions) {
  49. var msg = key;
  50. for (var i = 0; i < this.polys.length; i++) {
  51. msg = this.polys[i].resource.get(key, interpolationOptions);
  52. if (key != msg && msg.indexOf(Lexicon.NOT_TRANSLATED) !== 0) {
  53. break;
  54. }
  55. }
  56. return msg;
  57. },
  58. destroy: function destroy() {
  59. this.polys = null;
  60. }
  61. };
  62. function StringService(options) {
  63. this.init(options);
  64. }
  65. Object.keys(_stringService).forEach(function (key) {
  66. StringService.prototype[key] = _stringService[key];
  67. });
  68. return StringService;
  69. });
  70. //# sourceMappingURL=DashboardStringService.js.map