|
| 1 | +/*! |
| 2 | + * angular-translate - v2.14.0 - 2017-02-11 |
| 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 exports === 'object') { |
| 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 | + |
| 22 | +$translateStaticFilesLoader.$inject = ['$q', '$http']; |
| 23 | +angular.module('pascalprecht.translate') |
| 24 | +/** |
| 25 | + * @ngdoc object |
| 26 | + * @name pascalprecht.translate.$translateStaticFilesLoader |
| 27 | + * @requires $q |
| 28 | + * @requires $http |
| 29 | + * |
| 30 | + * @description |
| 31 | + * Creates a loading function for a typical static file url pattern: |
| 32 | + * "lang-en_US.json", "lang-de_DE.json", etc. Using this builder, |
| 33 | + * the response of these urls must be an object of key-value pairs. |
| 34 | + * |
| 35 | + * @param {object} options Options object, which gets prefix, suffix, key, and fileMap |
| 36 | + */ |
| 37 | +.factory('$translateStaticFilesLoader', $translateStaticFilesLoader); |
| 38 | + |
| 39 | +function $translateStaticFilesLoader($q, $http) { |
| 40 | + |
| 41 | + 'use strict'; |
| 42 | + |
| 43 | + return function (options) { |
| 44 | + |
| 45 | + if (!options || (!angular.isArray(options.files) && (!angular.isString(options.prefix) || !angular.isString(options.suffix)))) { |
| 46 | + throw new Error('Couldn\'t load static files, no files and prefix or suffix specified!'); |
| 47 | + } |
| 48 | + |
| 49 | + if (!options.files) { |
| 50 | + options.files = [{ |
| 51 | + prefix: options.prefix, |
| 52 | + suffix: options.suffix |
| 53 | + }]; |
| 54 | + } |
| 55 | + |
| 56 | + var load = function (file) { |
| 57 | + if (!file || (!angular.isString(file.prefix) || !angular.isString(file.suffix))) { |
| 58 | + throw new Error('Couldn\'t load static file, no prefix or suffix specified!'); |
| 59 | + } |
| 60 | + |
| 61 | + var fileUrl = [ |
| 62 | + file.prefix, |
| 63 | + options.key, |
| 64 | + file.suffix |
| 65 | + ].join(''); |
| 66 | + |
| 67 | + if (angular.isObject(options.fileMap) && options.fileMap[fileUrl]) { |
| 68 | + fileUrl = options.fileMap[fileUrl]; |
| 69 | + } |
| 70 | + |
| 71 | + return $http(angular.extend({ |
| 72 | + url: fileUrl, |
| 73 | + method: 'GET' |
| 74 | + }, options.$http)) |
| 75 | + .then(function(result) { |
| 76 | + return result.data; |
| 77 | + }, function () { |
| 78 | + return $q.reject(options.key); |
| 79 | + }); |
| 80 | + }; |
| 81 | + |
| 82 | + var promises = [], |
| 83 | + length = options.files.length; |
| 84 | + |
| 85 | + for (var i = 0; i < length; i++) { |
| 86 | + promises.push(load({ |
| 87 | + prefix: options.files[i].prefix, |
| 88 | + key: options.key, |
| 89 | + suffix: options.files[i].suffix |
| 90 | + })); |
| 91 | + } |
| 92 | + |
| 93 | + return $q.all(promises) |
| 94 | + .then(function (data) { |
| 95 | + var length = data.length, |
| 96 | + mergedData = {}; |
| 97 | + |
| 98 | + for (var i = 0; i < length; i++) { |
| 99 | + for (var key in data[i]) { |
| 100 | + mergedData[key] = data[i][key]; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return mergedData; |
| 105 | + }); |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +$translateStaticFilesLoader.displayName = '$translateStaticFilesLoader'; |
| 110 | +return 'pascalprecht.translate'; |
| 111 | + |
| 112 | +})); |
0 commit comments