loader-static-files.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. angular.module('pascalprecht.translate')
  2. /**
  3. * @ngdoc object
  4. * @name pascalprecht.translate.$translateStaticFilesLoader
  5. * @requires $q
  6. * @requires $http
  7. *
  8. * @description
  9. * Creates a loading function for a typical static file url pattern:
  10. * "lang-en_US.json", "lang-de_DE.json", etc. Using this builder,
  11. * the response of these urls must be an object of key-value pairs.
  12. *
  13. * @param {object} options Options object, which gets prefix, suffix and key.
  14. */
  15. .factory('$translateStaticFilesLoader', ['$q', '$http', function ($q, $http) {
  16. return function (options) {
  17. if (!options || (!angular.isString(options.prefix) || !angular.isString(options.suffix))) {
  18. throw new Error('Couldn\'t load static files, no prefix or suffix specified!');
  19. }
  20. var deferred = $q.defer();
  21. $http({
  22. url: [
  23. options.prefix,
  24. options.key,
  25. options.suffix
  26. ].join(''),
  27. method: 'GET',
  28. params: ''
  29. }).success(function (data) {
  30. deferred.resolve(data);
  31. }).error(function (data) {
  32. deferred.reject(options.key);
  33. });
  34. return deferred.promise;
  35. };
  36. }]);