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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*!
  2. * angular-translate - v2.11.0 - 2016-03-20
  3. *
  4. * Copyright (c) 2016 The angular-translate team, Pascal Precht; Licensed MIT
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. (function (root, factory) {
  26. if (typeof define === 'function' && define.amd) {
  27. // AMD. Register as an anonymous module unless amdModuleId is set
  28. define([], function () {
  29. return (factory());
  30. });
  31. } else if (typeof exports === 'object') {
  32. // Node. Does not work with strict CommonJS, but
  33. // only CommonJS-like environments that support module.exports,
  34. // like Node.
  35. module.exports = factory();
  36. } else {
  37. factory();
  38. }
  39. }(this, function () {
  40. $translateStaticFilesLoader.$inject = ['$q', '$http'];
  41. angular.module('pascalprecht.translate')
  42. /**
  43. * @ngdoc object
  44. * @name pascalprecht.translate.$translateStaticFilesLoader
  45. * @requires $q
  46. * @requires $http
  47. *
  48. * @description
  49. * Creates a loading function for a typical static file url pattern:
  50. * "lang-en_US.json", "lang-de_DE.json", etc. Using this builder,
  51. * the response of these urls must be an object of key-value pairs.
  52. *
  53. * @param {object} options Options object, which gets prefix, suffix and key.
  54. */
  55. .factory('$translateStaticFilesLoader', $translateStaticFilesLoader);
  56. function $translateStaticFilesLoader($q, $http) {
  57. 'use strict';
  58. return function (options) {
  59. if (!options || (!angular.isArray(options.files) && (!angular.isString(options.prefix) || !angular.isString(options.suffix)))) {
  60. throw new Error('Couldn\'t load static files, no files and prefix or suffix specified!');
  61. }
  62. if (!options.files) {
  63. options.files = [{
  64. prefix: options.prefix,
  65. suffix: options.suffix
  66. }];
  67. }
  68. var load = function (file) {
  69. if (!file || (!angular.isString(file.prefix) || !angular.isString(file.suffix))) {
  70. throw new Error('Couldn\'t load static file, no prefix or suffix specified!');
  71. }
  72. return $http(angular.extend({
  73. url: [
  74. file.prefix,
  75. options.key,
  76. file.suffix
  77. ].join(''),
  78. method: 'GET',
  79. params: ''
  80. }, options.$http))
  81. .then(function(result) {
  82. return result.data;
  83. }, function () {
  84. return $q.reject(options.key);
  85. });
  86. };
  87. var promises = [],
  88. length = options.files.length;
  89. for (var i = 0; i < length; i++) {
  90. promises.push(load({
  91. prefix: options.files[i].prefix,
  92. key: options.key,
  93. suffix: options.files[i].suffix
  94. }));
  95. }
  96. return $q.all(promises)
  97. .then(function (data) {
  98. var length = data.length,
  99. mergedData = {};
  100. for (var i = 0; i < length; i++) {
  101. for (var key in data[i]) {
  102. mergedData[key] = data[i][key];
  103. }
  104. }
  105. return mergedData;
  106. });
  107. };
  108. }
  109. $translateStaticFilesLoader.displayName = '$translateStaticFilesLoader';
  110. return 'pascalprecht.translate';
  111. }));