angular-translate-loader-static-files.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*!
  2. * angular-translate - v2.15.1 - 2017-04-03
  3. *
  4. * Copyright (c) 2017 The angular-translate team, Pascal Precht; Licensed MIT
  5. */
  6. (function (root, factory) {
  7. if (typeof define === 'function' && define.amd) {
  8. // AMD. Register as an anonymous module unless amdModuleId is set
  9. define([], function () {
  10. return (factory());
  11. });
  12. } else if (typeof module === 'object' && module.exports) {
  13. // Node. Does not work with strict CommonJS, but
  14. // only CommonJS-like environments that support module.exports,
  15. // like Node.
  16. module.exports = factory();
  17. } else {
  18. factory();
  19. }
  20. }(this, function () {
  21. $translateStaticFilesLoader.$inject = ['$q', '$http'];
  22. angular.module('pascalprecht.translate')
  23. /**
  24. * @ngdoc object
  25. * @name pascalprecht.translate.$translateStaticFilesLoader
  26. * @requires $q
  27. * @requires $http
  28. *
  29. * @description
  30. * Creates a loading function for a typical static file url pattern:
  31. * "lang-en_US.json", "lang-de_DE.json", etc. Using this builder,
  32. * the response of these urls must be an object of key-value pairs.
  33. *
  34. * @param {object} options Options object, which gets prefix, suffix, key, and fileMap
  35. */
  36. .factory('$translateStaticFilesLoader', $translateStaticFilesLoader);
  37. function $translateStaticFilesLoader($q, $http) {
  38. 'use strict';
  39. return function (options) {
  40. if (!options || (!angular.isArray(options.files) && (!angular.isString(options.prefix) || !angular.isString(options.suffix)))) {
  41. throw new Error('Couldn\'t load static files, no files and prefix or suffix specified!');
  42. }
  43. if (!options.files) {
  44. options.files = [{
  45. prefix: options.prefix,
  46. suffix: options.suffix
  47. }];
  48. }
  49. var load = function (file) {
  50. if (!file || (!angular.isString(file.prefix) || !angular.isString(file.suffix))) {
  51. throw new Error('Couldn\'t load static file, no prefix or suffix specified!');
  52. }
  53. var fileUrl = [
  54. file.prefix,
  55. options.key,
  56. file.suffix
  57. ].join('');
  58. if (angular.isObject(options.fileMap) && options.fileMap[fileUrl]) {
  59. fileUrl = options.fileMap[fileUrl];
  60. }
  61. return $http(angular.extend({
  62. url: fileUrl,
  63. method: 'GET'
  64. }, options.$http))
  65. .then(function(result) {
  66. return result.data;
  67. }, function () {
  68. return $q.reject(options.key);
  69. });
  70. };
  71. var promises = [],
  72. length = options.files.length;
  73. for (var i = 0; i < length; i++) {
  74. promises.push(load({
  75. prefix: options.files[i].prefix,
  76. key: options.key,
  77. suffix: options.files[i].suffix
  78. }));
  79. }
  80. return $q.all(promises)
  81. .then(function (data) {
  82. var length = data.length,
  83. mergedData = {};
  84. for (var i = 0; i < length; i++) {
  85. for (var key in data[i]) {
  86. mergedData[key] = data[i][key];
  87. }
  88. }
  89. return mergedData;
  90. });
  91. };
  92. }
  93. $translateStaticFilesLoader.displayName = '$translateStaticFilesLoader';
  94. return 'pascalprecht.translate';
  95. }));