diff --git a/angular.json b/angular.json index 6b1199b..371742d 100644 --- a/angular.json +++ b/angular.json @@ -44,5 +44,8 @@ } } }, - "defaultProject": "@ngx-translate/http-loader" -} + "defaultProject": "@ngx-translate/http-loader", + "cli": { + "analytics": false + } +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c539ee5..98af2c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,23 @@ "requires": { "@angular-devkit/core": "10.0.0", "rxjs": "6.5.5" + }, + "dependencies": { + "rxjs": { + "version": "6.5.5", + "resolved": "http://npm-repo.devops.mhealth.tech/rxjs/-/rxjs-6.5.5.tgz", + "integrity": "sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==", + "dev": true, + "requires": { + "tslib": "^1.9.0" + } + }, + "tslib": { + "version": "1.14.1", + "resolved": "http://npm-repo.devops.mhealth.tech/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", + "dev": true + } } }, "@angular-devkit/build-angular": { @@ -189,11 +206,26 @@ "glob": "^7.1.3" } }, + "rxjs": { + "version": "6.5.5", + "resolved": "http://npm-repo.devops.mhealth.tech/rxjs/-/rxjs-6.5.5.tgz", + "integrity": "sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==", + "dev": true, + "requires": { + "tslib": "^1.9.0" + } + }, "semver": { "version": "7.3.2", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz", "integrity": "sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==", "dev": true + }, + "tslib": { + "version": "1.14.1", + "resolved": "http://npm-repo.devops.mhealth.tech/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", + "dev": true } } }, @@ -205,6 +237,23 @@ "requires": { "@angular-devkit/architect": "0.1000.0", "rxjs": "6.5.5" + }, + "dependencies": { + "rxjs": { + "version": "6.5.5", + "resolved": "http://npm-repo.devops.mhealth.tech/rxjs/-/rxjs-6.5.5.tgz", + "integrity": "sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==", + "dev": true, + "requires": { + "tslib": "^1.9.0" + } + }, + "tslib": { + "version": "1.14.1", + "resolved": "http://npm-repo.devops.mhealth.tech/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", + "dev": true + } } }, "@angular-devkit/build-optimizer": { @@ -228,6 +277,23 @@ "@angular-devkit/architect": "0.1000.0", "@angular-devkit/core": "10.0.0", "rxjs": "6.5.5" + }, + "dependencies": { + "rxjs": { + "version": "6.5.5", + "resolved": "http://npm-repo.devops.mhealth.tech/rxjs/-/rxjs-6.5.5.tgz", + "integrity": "sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==", + "dev": true, + "requires": { + "tslib": "^1.9.0" + } + }, + "tslib": { + "version": "1.14.1", + "resolved": "http://npm-repo.devops.mhealth.tech/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", + "dev": true + } } }, "@angular-devkit/core": { @@ -241,6 +307,23 @@ "magic-string": "0.25.7", "rxjs": "6.5.5", "source-map": "0.7.3" + }, + "dependencies": { + "rxjs": { + "version": "6.5.5", + "resolved": "http://npm-repo.devops.mhealth.tech/rxjs/-/rxjs-6.5.5.tgz", + "integrity": "sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==", + "dev": true, + "requires": { + "tslib": "^1.9.0" + } + }, + "tslib": { + "version": "1.14.1", + "resolved": "http://npm-repo.devops.mhealth.tech/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", + "dev": true + } } }, "@angular-devkit/schematics": { @@ -252,6 +335,23 @@ "@angular-devkit/core": "10.0.0", "ora": "4.0.4", "rxjs": "6.5.5" + }, + "dependencies": { + "rxjs": { + "version": "6.5.5", + "resolved": "http://npm-repo.devops.mhealth.tech/rxjs/-/rxjs-6.5.5.tgz", + "integrity": "sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==", + "dev": true, + "requires": { + "tslib": "^1.9.0" + } + }, + "tslib": { + "version": "1.14.1", + "resolved": "http://npm-repo.devops.mhealth.tech/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", + "dev": true + } } }, "@angular/cli": { @@ -3139,6 +3239,23 @@ "enhanced-resolve": "4.1.1", "rxjs": "6.5.5", "webpack-sources": "1.4.3" + }, + "dependencies": { + "rxjs": { + "version": "6.5.5", + "resolved": "http://npm-repo.devops.mhealth.tech/rxjs/-/rxjs-6.5.5.tgz", + "integrity": "sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==", + "dev": true, + "requires": { + "tslib": "^1.9.0" + } + }, + "tslib": { + "version": "1.14.1", + "resolved": "http://npm-repo.devops.mhealth.tech/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", + "dev": true + } } }, "@ngx-translate/core": { @@ -3294,11 +3411,26 @@ "semver-intersect": "1.4.0" }, "dependencies": { + "rxjs": { + "version": "6.5.5", + "resolved": "http://npm-repo.devops.mhealth.tech/rxjs/-/rxjs-6.5.5.tgz", + "integrity": "sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==", + "dev": true, + "requires": { + "tslib": "^1.9.0" + } + }, "semver": { "version": "7.3.2", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz", "integrity": "sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==", "dev": true + }, + "tslib": { + "version": "1.14.1", + "resolved": "http://npm-repo.devops.mhealth.tech/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", + "dev": true } } }, @@ -4356,6 +4488,16 @@ "integrity": "sha512-DYWGk01lDcxeS/K9IHPGWfT8PsJmbXRtRd2Sx72Tnb8pcYZQFF1oSDb8hJtS1vhp212q1Rzi5dUf9+nq0o9UIg==", "dev": true }, + "bindings": { + "version": "1.5.0", + "resolved": "http://npm-repo.devops.mhealth.tech/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "dev": true, + "optional": true, + "requires": { + "file-uri-to-path": "1.0.0" + } + }, "blob": { "version": "0.0.4", "resolved": "https://registry.npmjs.org/blob/-/blob-0.0.4.tgz", @@ -8536,6 +8678,13 @@ "schema-utils": "^2.6.5" } }, + "file-uri-to-path": { + "version": "1.0.0", + "resolved": "http://npm-repo.devops.mhealth.tech/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "dev": true, + "optional": true + }, "filename-regex": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/filename-regex/-/filename-regex-2.0.1.tgz", @@ -8996,8 +9145,8 @@ "dev": true, "optional": true, "requires": { - "delegates": "1.0.0", - "readable-stream": "2.3.6" + "delegates": "^1.0.0", + "readable-stream": "^2.0.6" } }, "balanced-match": { @@ -9012,7 +9161,7 @@ "dev": true, "optional": true, "requires": { - "balanced-match": "1.0.0", + "balanced-match": "^1.0.0", "concat-map": "0.0.1" } }, @@ -9079,7 +9228,7 @@ "dev": true, "optional": true, "requires": { - "minipass": "2.2.4" + "minipass": "^2.2.1" } }, "fs.realpath": { @@ -9094,14 +9243,14 @@ "dev": true, "optional": true, "requires": { - "aproba": "1.2.0", - "console-control-strings": "1.1.0", - "has-unicode": "2.0.1", - "object-assign": "4.1.1", - "signal-exit": "3.0.2", - "string-width": "1.0.2", - "strip-ansi": "3.0.1", - "wide-align": "1.1.2" + "aproba": "^1.0.3", + "console-control-strings": "^1.0.0", + "has-unicode": "^2.0.0", + "object-assign": "^4.1.0", + "signal-exit": "^3.0.0", + "string-width": "^1.0.1", + "strip-ansi": "^3.0.1", + "wide-align": "^1.1.0" } }, "glob": { @@ -9110,12 +9259,12 @@ "dev": true, "optional": true, "requires": { - "fs.realpath": "1.0.0", - "inflight": "1.0.6", - "inherits": "2.0.3", - "minimatch": "3.0.4", - "once": "1.4.0", - "path-is-absolute": "1.0.1" + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" } }, "has-unicode": { @@ -9130,7 +9279,7 @@ "dev": true, "optional": true, "requires": { - "safer-buffer": "2.1.2" + "safer-buffer": "^2.1.0" } }, "ignore-walk": { @@ -9139,7 +9288,7 @@ "dev": true, "optional": true, "requires": { - "minimatch": "3.0.4" + "minimatch": "^3.0.4" } }, "inflight": { @@ -9148,8 +9297,8 @@ "dev": true, "optional": true, "requires": { - "once": "1.4.0", - "wrappy": "1.0.2" + "once": "^1.3.0", + "wrappy": "1" } }, "inherits": { @@ -9170,7 +9319,7 @@ "dev": true, "optional": true, "requires": { - "number-is-nan": "1.0.1" + "number-is-nan": "^1.0.0" } }, "isarray": { @@ -9185,7 +9334,7 @@ "dev": true, "optional": true, "requires": { - "brace-expansion": "1.1.11" + "brace-expansion": "^1.1.7" } }, "minimist": { @@ -9200,8 +9349,8 @@ "dev": true, "optional": true, "requires": { - "safe-buffer": "5.1.1", - "yallist": "3.0.2" + "safe-buffer": "^5.1.1", + "yallist": "^3.0.0" } }, "minizlib": { @@ -9210,7 +9359,7 @@ "dev": true, "optional": true, "requires": { - "minipass": "2.2.4" + "minipass": "^2.2.1" } }, "mkdirp": { @@ -9234,9 +9383,9 @@ "dev": true, "optional": true, "requires": { - "debug": "2.6.9", - "iconv-lite": "0.4.21", - "sax": "1.2.4" + "debug": "^2.1.2", + "iconv-lite": "^0.4.4", + "sax": "^1.2.4" } }, "node-pre-gyp": { @@ -9245,16 +9394,16 @@ "dev": true, "optional": true, "requires": { - "detect-libc": "1.0.3", - "mkdirp": "0.5.1", - "needle": "2.2.0", - "nopt": "4.0.1", - "npm-packlist": "1.1.10", - "npmlog": "4.1.2", - "rc": "1.2.7", - "rimraf": "2.6.2", - "semver": "5.5.0", - "tar": "4.4.1" + "detect-libc": "^1.0.2", + "mkdirp": "^0.5.1", + "needle": "^2.2.0", + "nopt": "^4.0.1", + "npm-packlist": "^1.1.6", + "npmlog": "^4.0.2", + "rc": "^1.1.7", + "rimraf": "^2.6.1", + "semver": "^5.3.0", + "tar": "^4" } }, "nopt": { @@ -9263,8 +9412,8 @@ "dev": true, "optional": true, "requires": { - "abbrev": "1.1.1", - "osenv": "0.1.5" + "abbrev": "1", + "osenv": "^0.1.4" } }, "npm-bundled": { @@ -9279,8 +9428,8 @@ "dev": true, "optional": true, "requires": { - "ignore-walk": "3.0.1", - "npm-bundled": "1.0.3" + "ignore-walk": "^3.0.1", + "npm-bundled": "^1.0.1" } }, "npmlog": { @@ -9289,10 +9438,10 @@ "dev": true, "optional": true, "requires": { - "are-we-there-yet": "1.1.4", - "console-control-strings": "1.1.0", - "gauge": "2.7.4", - "set-blocking": "2.0.0" + "are-we-there-yet": "~1.1.2", + "console-control-strings": "~1.1.0", + "gauge": "~2.7.3", + "set-blocking": "~2.0.0" } }, "number-is-nan": { @@ -9313,7 +9462,7 @@ "dev": true, "optional": true, "requires": { - "wrappy": "1.0.2" + "wrappy": "1" } }, "os-homedir": { @@ -9334,8 +9483,8 @@ "dev": true, "optional": true, "requires": { - "os-homedir": "1.0.2", - "os-tmpdir": "1.0.2" + "os-homedir": "^1.0.0", + "os-tmpdir": "^1.0.0" } }, "path-is-absolute": { @@ -9356,10 +9505,10 @@ "dev": true, "optional": true, "requires": { - "deep-extend": "0.5.1", - "ini": "1.3.5", - "minimist": "1.2.0", - "strip-json-comments": "2.0.1" + "deep-extend": "^0.5.1", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" }, "dependencies": { "minimist": { @@ -9376,13 +9525,13 @@ "dev": true, "optional": true, "requires": { - "core-util-is": "1.0.2", - "inherits": "2.0.3", - "isarray": "1.0.0", - "process-nextick-args": "2.0.0", - "safe-buffer": "5.1.1", - "string_decoder": "1.1.1", - "util-deprecate": "1.0.2" + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" } }, "rimraf": { @@ -9391,7 +9540,7 @@ "dev": true, "optional": true, "requires": { - "glob": "7.1.2" + "glob": "^7.0.5" } }, "safe-buffer": { @@ -9436,9 +9585,9 @@ "dev": true, "optional": true, "requires": { - "code-point-at": "1.1.0", - "is-fullwidth-code-point": "1.0.0", - "strip-ansi": "3.0.1" + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "strip-ansi": "^3.0.0" } }, "string_decoder": { @@ -9447,7 +9596,7 @@ "dev": true, "optional": true, "requires": { - "safe-buffer": "5.1.1" + "safe-buffer": "~5.1.0" } }, "strip-ansi": { @@ -9456,7 +9605,7 @@ "dev": true, "optional": true, "requires": { - "ansi-regex": "2.1.1" + "ansi-regex": "^2.0.0" } }, "strip-json-comments": { @@ -9471,13 +9620,13 @@ "dev": true, "optional": true, "requires": { - "chownr": "1.0.1", - "fs-minipass": "1.2.5", - "minipass": "2.2.4", - "minizlib": "1.1.0", - "mkdirp": "0.5.1", - "safe-buffer": "5.1.1", - "yallist": "3.0.2" + "chownr": "^1.0.1", + "fs-minipass": "^1.2.5", + "minipass": "^2.2.4", + "minizlib": "^1.1.0", + "mkdirp": "^0.5.0", + "safe-buffer": "^5.1.1", + "yallist": "^3.0.2" } }, "util-deprecate": { @@ -9492,7 +9641,7 @@ "dev": true, "optional": true, "requires": { - "string-width": "1.0.2" + "string-width": "^1.0.2" } }, "wrappy": { @@ -16703,17 +16852,17 @@ } }, "rxjs": { - "version": "6.5.5", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.5.5.tgz", - "integrity": "sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==", + "version": "6.6.3", + "resolved": "http://npm-repo.devops.mhealth.tech/rxjs/-/rxjs-6.6.3.tgz", + "integrity": "sha512-trsQc+xYYXZ3urjOiJOuCOa5N3jAZ3eiSpQB5hIT8zGlL2QfnHLJ2r7GMkBGuIausdJN1OneaI6gQlsqNHHmZQ==", "requires": { "tslib": "^1.9.0" }, "dependencies": { "tslib": { - "version": "1.13.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz", - "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==" + "version": "1.14.1", + "resolved": "http://npm-repo.devops.mhealth.tech/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" } } }, @@ -19194,6 +19343,17 @@ "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz", "integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==", "dev": true, + "optional": true, + "requires": { + "bindings": "^1.5.0", + "nan": "^2.12.1" + } + }, + "nan": { + "version": "2.14.2", + "resolved": "http://npm-repo.devops.mhealth.tech/nan/-/nan-2.14.2.tgz", + "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==", + "dev": true, "optional": true }, "normalize-path": { @@ -19587,7 +19747,11 @@ "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz", "integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==", "dev": true, - "optional": true + "optional": true, + "requires": { + "bindings": "^1.5.0", + "nan": "^2.12.1" + } }, "is-absolute-url": { "version": "3.0.3", @@ -19601,6 +19765,13 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, + "nan": { + "version": "2.14.2", + "resolved": "http://npm-repo.devops.mhealth.tech/nan/-/nan-2.14.2.tgz", + "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==", + "dev": true, + "optional": true + }, "normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", diff --git a/package.json b/package.json index 025db4a..9ae19cc 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "@angular/platform-browser-dynamic": "^10.0.0", "@ngx-translate/core": ">=12.0.0", "core-js": "^3.6.5", - "rxjs": "^6.5.5", + "rxjs": "^6.6.3", "tslib": "^2.0.0", "zone.js": "~0.10.3" }, diff --git a/projects/ngx-translate/http-loader/src/lib/http-multi-loader.ts b/projects/ngx-translate/http-loader/src/lib/http-multi-loader.ts new file mode 100644 index 0000000..8854cab --- /dev/null +++ b/projects/ngx-translate/http-loader/src/lib/http-multi-loader.ts @@ -0,0 +1,28 @@ +import {HttpClient} from "@angular/common/http"; +import {TranslateLoader} from "@ngx-translate/core"; +import {Observable, of, forkJoin} from 'rxjs'; +import {catchError, map} from "rxjs/operators"; + +export interface TranslationResource { + prefix: string; + suffix: string; +} + +export class TranslateHttpMultiLoader implements TranslateLoader { + constructor(private http: HttpClient, private resources: TranslationResource[]) {} + + /** + * Gets the multiple translation files from the server + */ + public getTranslation(lang: string): Observable { + const requests = this.resources.map((resource) => { + const path = `${resource.prefix}/${lang}${resource.suffix}`; + return this.http.get(path).pipe( + catchError((res) => { + return of({}); + }) + ); + }); + return forkJoin(requests).pipe(map((response) => Object.assign({}, ...response))); + } +} diff --git a/projects/ngx-translate/http-loader/src/public_api.ts b/projects/ngx-translate/http-loader/src/public_api.ts index f236ac3..6e0b106 100644 --- a/projects/ngx-translate/http-loader/src/public_api.ts +++ b/projects/ngx-translate/http-loader/src/public_api.ts @@ -1 +1,2 @@ export * from "./lib/http-loader"; +export * from "./lib/http-multi-loader"; diff --git a/projects/ngx-translate/http-loader/tests/http-multi-loader.spec.ts b/projects/ngx-translate/http-loader/tests/http-multi-loader.spec.ts new file mode 100644 index 0000000..3b1c821 --- /dev/null +++ b/projects/ngx-translate/http-loader/tests/http-multi-loader.spec.ts @@ -0,0 +1,118 @@ +import {HttpClient} from "@angular/common/http"; +import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing"; +import {fakeAsync, TestBed} from "@angular/core/testing"; +import {TranslateLoader, TranslateModule, TranslateService} from "@ngx-translate/core"; +import {TranslateHttpMultiLoader, TranslationResource} from "../src/public_api"; + +// AoT requires an exported function for factories +export function translateLoaderFactory(http: HttpClient) { + return new TranslateHttpMultiLoader( http, [ + { prefix: '/assets/i18n/home', suffix: '.json' }, + { prefix: '/assets/i18n/product', suffix: '.json' } + ]); +} + +describe('TranslateLoader', () => { + let translate: TranslateService; + let http: HttpTestingController; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + HttpClientTestingModule, + TranslateModule.forRoot({ + loader: { + provide: TranslateLoader, + useFactory: translateLoaderFactory, + deps: [HttpClient] + } + }) + ], + providers: [TranslateService] + }); + translate = TestBed.get(TranslateService); + http = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + translate = undefined; + http = undefined; + }); + + it('should be able to provide TranslateHttpMultiLoader', () => { + expect(TranslateHttpMultiLoader).toBeDefined(); + expect(translate.currentLoader).toBeDefined(); + expect(translate.currentLoader instanceof TranslateHttpMultiLoader).toBeTruthy(); + }); + + it('should be able to get translations', () => { + translate.use('en'); + + // this will request the translation from the backend because we use a static files loader for TranslateService + translate.get('TEST').subscribe((res: string) => { + expect(res).toEqual('This is a home page test'); + }); + + // mock response after the xhr request, otherwise it will be undefined + http.expectOne('/assets/i18n/home/en.json').flush({ + "TEST": "This is a home page test", + "TEST2": "This is another test" + }); + + http.expectOne('/assets/i18n/product/en.json').flush({ + "TEST3": "This is a product page test", + "TEST4": "This is another test" + }); + + // this will request the translation from downloaded translations without making a request to the backend + translate.get('TEST3').subscribe((res: string) => { + expect(res).toEqual('This is a product page test'); + }); + }); + + it('should be able to reload a lang', () => { + translate.use('en'); + + // this will request the translation from the backend because we use a static files loader for TranslateService + translate.get('TEST').subscribe((res: string) => { + expect(res).toEqual('This is a test'); + + // reset the lang as if it was never initiated + translate.reloadLang('en').subscribe((res2: string) => { + expect(translate.instant('TEST')).toEqual('This is a test 2'); + }); + + http.expectOne('/assets/i18n/home/en.json').flush({"TEST": "This is a test 2"}); + }); + + // mock response after the xhr request, otherwise it will be undefined + http.expectOne('/assets/i18n/home/en.json').flush({"TEST": "This is a test"}); + }); + + it('should be able to reset a lang', fakeAsync(() => { + translate.use('en'); + spyOn(http, 'expectOne').and.callThrough(); + + // this will request the translation from the backend because we use a static files loader for TranslateService + translate.get('TEST').subscribe((res: string) => { + expect(res).toEqual('This is a test'); + expect(http.expectOne).toHaveBeenCalledTimes(1); + + // reset the lang as if it was never initiated + translate.resetLang('en'); + + expect(translate.instant('TEST')).toEqual('TEST'); + + // use set timeout because no request is really made and we need to trigger zone to resolve the observable + setTimeout(() => { + translate.get('TEST').subscribe((res2: string) => { + expect(res2).toEqual('TEST'); // because the loader is "pristine" as if it was never called + expect(http.expectOne).toHaveBeenCalledTimes(1); + }); + }, 10); + }); + + // mock response after the xhr request, otherwise it will be undefined + http.expectOne('/assets/i18n/home/en.json').flush({"TEST": "This is a test"}); + })); +});