Skip to content

Commit 9e9d1e9

Browse files
author
Raphael Balet
committed
add - logic : prefix & suffix
1 parent 0c8c76d commit 9e9d1e9

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

projects/multi-http-loader/README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,28 @@ export function HttpLoaderFactory(_httpBackend: HttpBackend) {
7070
export class AppModule { }
7171
```
7272

73-
The `MultiTranslateHttpLoader` takes a list of strings.
73+
The `MultiTranslateHttpLoader` takes a list of `string[]` or `ITranslationResource[]`.
7474

75-
Those strings, for example `['/assets/i18n/core/', '/assets/i18n/vendors/']`,
75+
### String[]
76+
For example `['/assets/i18n/core/', '/assets/i18n/vendors/']`,
7677
will load your translations files for the lang "en" from : `/assets/i18n/core/en.json` and `/assets/i18n/vendors/en.json`
7778

79+
### Custom suffix
80+
**For now this loader only support the `json` format.**
81+
82+
Instead of an array of `string[]`,
83+
you may pass a list of parameters:
84+
- `prefix: string = '/assets/i18n/'`
85+
- `suffix: string = '.json'`
86+
87+
```typescript
88+
export function HttpLoaderFactory(_httpBackend: HttpBackend) {
89+
return new MultiTranslateHttpLoader(_httpBackend, [
90+
{prefix: './assets/i18n/core/', suffix: '.json'},
91+
{prefix: './assets/i18n/vendors/'}, // , "suffix: '.json'" being the default value
92+
]);
93+
}
94+
```
7895

7996
The loader will merge all translation files from the server using [deepmerge](https://github.com/KyleAMathews/deepmerge).
8097

projects/multi-http-loader/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-translate-multi-http-loader",
3-
"version": "9.0.0",
3+
"version": "9.0.1",
44
"license": "MIT",
55
"author": "Raphael Balet",
66
"maintainers": [

projects/multi-http-loader/src/lib/multi-http-loader.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,24 @@ import * as merge from 'deepmerge'
44
import { forkJoin, Observable, of } from 'rxjs'
55
import { catchError, map } from 'rxjs/operators'
66

7+
export interface ITranslationResource {
8+
prefix: string
9+
suffix?: string
10+
}
11+
712
export class MultiTranslateHttpLoader implements TranslateLoader {
8-
constructor(private _handler: HttpBackend, private _resourcesPrefix: string[]) {}
13+
constructor(
14+
private _handler: HttpBackend,
15+
private _resourcesPrefix: string[] | ITranslationResource[],
16+
) {}
917

1018
public getTranslation(lang: string): Observable<any> {
11-
const requests = this._resourcesPrefix.map((prefix) => {
12-
const path = `${prefix}${lang}.json`
19+
const requests = this._resourcesPrefix.map((resource) => {
20+
let path: string
21+
if (resource.prefix) path = `${resource.prefix}${lang}${resource.suffix || '.json'}`
22+
else {
23+
path = `${resource}${lang}.json`
24+
}
1325

1426
return new HttpClient(this._handler).get(path).pipe(
1527
catchError((res) => {

0 commit comments

Comments
 (0)