Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr/2 #3

Merged
merged 4 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ export interface IModuleTranslationOptions {
* @param translations the resolved translation files
*/
translateMerger?: (translations: Translation[]) => Translation;

/**
* Custom module path template for fetching translations.
* @default modulePathTemplate "{baseTranslateUrl}/{moduleName}/{language}{fileType}"
*/
modulePathTemplate?: string;

/**
* Custom path template for fetching translations.
* @default pathTemplate "{baseTranslateUrl}/{language}{fileType}"
*/
pathTemplate?: string;
}
```

Expand Down
12 changes: 12 additions & 0 deletions lib/src/models/module-translation-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ export interface IModuleTranslationOptions {
* @param translations the resolved translation files
*/
translateMerger?: (translations: Translation[]) => Translation;

/**
* Custom module path template for fetching translations.
* @default modulePathTemplate "{baseTranslateUrl}/{moduleName}/{language}{fileType}"
*/
modulePathTemplate?: string;

/**
* Custom path template for fetching translations.
* @default pathTemplate "{baseTranslateUrl}/{language}{fileType}"
*/
pathTemplate?: string;
}
20 changes: 17 additions & 3 deletions lib/src/module-translate-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ export class ModuleTranslateLoader implements TranslateLoader {
enableNamespacing: true,
nameSpaceUppercase: true,
deepMerge: true,
modulePathTemplate: '{baseTranslateUrl}/{moduleName}/{language}{fileType}',
pathTemplate: '{baseTranslateUrl}/{language}{fileType}',
...this.options
};

private pathTemplateRx: RegExp = /{([^}]+)}/gi;

/**
* The ModuleTranslateLoader for 'ngx-translate/core'
*
Expand All @@ -37,20 +41,30 @@ export class ModuleTranslateLoader implements TranslateLoader {
nameSpaceUppercase,
modules,
translateError,
translateMerger
translateMerger,
pathTemplate,
modulePathTemplate,
} = this._defaultOptions;

const moduleRequests = modules.map(
({ baseTranslateUrl, moduleName, fileType, nameSpace, translateMap }) => {
if (!moduleName) {
const path = `${baseTranslateUrl}/${language}${fileType}`;
const pathOptions = { baseTranslateUrl, language, fileType };
const path = pathTemplate.replace(
this.pathTemplateRx,
(_, m1: string) => pathOptions[m1] || ''
);
return this.http.get<Translation>(path).pipe(
map(translation => (translateMap ? translateMap(translation) : translation)),
this._catchError(path, translateError)
);
}

const modulePath = `${baseTranslateUrl}/${moduleName}/${language}${fileType}`;
const modulePathOptions = { baseTranslateUrl, moduleName, language, fileType };
const modulePath = modulePathTemplate.replace(
this.pathTemplateRx,
(_, m1: string) => modulePathOptions[m1] || ''
);
return this.http.get<Translation>(modulePath).pipe(
map(translation => {
if (translateMap) {
Expand Down
36 changes: 36 additions & 0 deletions lib/test/module-translate-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,40 @@ describe('ModuleTranslateLoader', () => {
});
});
});

it('should load from custom path templates', done => {
const options: IModuleTranslationOptions = {
...defaultOptions,
modulePathTemplate: '{baseTranslateUrl}/{language}/{moduleName}{fileType}',
pathTemplate: '{baseTranslateUrl}/{language}{fileType}',
};

const language = 'en';

const loader = new ModuleTranslateLoader(TestBed.get(HttpClient), options);

loader.getTranslation(language).subscribe(translation => {
const expected = {
key: 'value',
key1: 'value1',
parent: { child: { grandChild: 'value1' } },
FEATURE1: { key: 'value', key1: 'value1', parent: { child: { grandChild: 'value1' } } },
FEATURE2: { key: 'value', key1: 'value1', parent: { child: { grandChild: 'value1' } } }
};
expect(translation).toEqual(expected);
done();
});

options.modules.forEach(({ baseTranslateUrl, moduleName, fileType }) => {
const path =
moduleName == null
? `${baseTranslateUrl}/${language}${fileType}`
: `${baseTranslateUrl}/${language}/${moduleName}${fileType}`;

const mock = httpMock.expectOne(path);
expect(mock.request.method).toEqual('GET');

mock.flush(mockTranslation);
});
});
});
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "@larscom/ngx-translate-module-loader",
"name": "ngx-translate-module-loader-2",
"version": "1.2.1",
"description": "Fetch multiple translation files (http) with ngx-translate. Each translations file gets it's own namespace out of the box",
"main": "./dist/public_api.js",
"typings": "./dist/public_api.d.ts",
"scripts": {
"build": "rimraf dist && tsc",
"postinstall": "npm run build",
"lint": "tslint -p tslint.json -t stylish",
"test": "jest",
"test:update": "jest -u",
Expand All @@ -21,7 +22,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/larscom/ngx-translate-module-loader"
"url": "git+https://github.com/aidenwallis/ngx-translate-module-loader"
},
"keywords": [
"angular",
Expand Down