-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use logger pattern for translator (#145)
* feat: implement factory pattern for translator WIP * test: remove componentsjs from crypto service test * fix: eslint error * chore: remove old translator from dgt-components * test: fixed tests in dgt-components * test: update coverage * chore: remove old i18n exports from index.ts * chore: add demo for translator * chore: removed label, refactored getTranslatorFor * chore: remove console.log Co-authored-by: Arthur Joppart <[email protected]>
- Loading branch information
1 parent
03f52f6
commit 9f4520a
Showing
30 changed files
with
391 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"example-translation": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut lacinia arcu vel aliquam interdum. Aliquam bibendum vitae magna id molestie." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/dgt-components/lib/components/source/source-list.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { define } from '../../util/define'; | ||
import { SourceListComponent } from './source-list.component'; | ||
|
||
describe('SourceListComponent', () => { | ||
|
||
let component: SourceListComponent; | ||
const tag = 'source-list-component'; | ||
|
||
beforeEach(() => { | ||
|
||
define(tag, SourceListComponent); | ||
component = document.createElement(tag) as SourceListComponent; | ||
|
||
}); | ||
|
||
it('should instantiate', async () => { | ||
|
||
window.document.body.appendChild(component); | ||
await component.updateComplete; | ||
|
||
expect(component).toBeTruthy(); | ||
expect(component).toBeInstanceOf(SourceListComponent); | ||
|
||
}); | ||
|
||
}); |
26 changes: 26 additions & 0 deletions
26
packages/dgt-components/lib/components/source/source.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { define } from '../../util/define'; | ||
import { SourceComponent } from './source.component'; | ||
|
||
describe('SourceComponent', () => { | ||
|
||
let component: SourceComponent; | ||
const tag = 'source-component'; | ||
|
||
beforeEach(() => { | ||
|
||
define(tag, SourceComponent); | ||
component = document.createElement(tag) as SourceComponent; | ||
|
||
}); | ||
|
||
it('should instantiate', async () => { | ||
|
||
window.document.body.appendChild(component); | ||
await component.updateComplete; | ||
|
||
expect(component).toBeTruthy(); | ||
expect(component).toBeInstanceOf(SourceComponent); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 0 additions & 56 deletions
56
packages/dgt-components/lib/services/i18n/mock-translator.spec.ts
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
packages/dgt-components/lib/services/i18n/mock-translator.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,7 @@ | |
"ArgumentError", | ||
"HttpError", | ||
"HttpClient", | ||
"EventObject" | ||
"EventObject", | ||
"EventTarget", | ||
"CustomEvent" | ||
] |
47 changes: 47 additions & 0 deletions
47
packages/dgt-utils/lib/i18n/factories/cached-translator-factory.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint-disable @typescript-eslint/dot-notation */ | ||
import fetchMock from 'jest-fetch-mock'; | ||
import { CachedTranslatorFactory } from './cached-translator-factory'; | ||
|
||
describe('', () => { | ||
|
||
let factory: CachedTranslatorFactory; | ||
|
||
beforeEach(() => { | ||
|
||
factory = new CachedTranslatorFactory(); | ||
|
||
}); | ||
|
||
it('should create translator with correct language', async () => { | ||
|
||
const translator = await factory.createTranslator('en'); | ||
expect(translator.getLang()).toEqual('en'); | ||
|
||
}); | ||
|
||
it('should set translations when creating translator', async () => { | ||
|
||
expect(factory['translations']).toBeUndefined(); | ||
await factory.createTranslator('en'); | ||
expect(factory['translations']).toBeTruthy(); | ||
|
||
}); | ||
|
||
it('should only fetch translations once', async () => { | ||
|
||
fetchMock.resetMocks(); | ||
fetchMock.mockIf(/.*\.json$/, '{}'); | ||
|
||
expect(factory['translations']).toBeUndefined(); | ||
|
||
for (let i = 0; i < 10; i++) { | ||
|
||
await factory.createTranslator('en'); | ||
|
||
} | ||
|
||
expect(fetchMock.mock.calls.length).toEqual(1); | ||
|
||
}); | ||
|
||
}); |
23 changes: 23 additions & 0 deletions
23
packages/dgt-utils/lib/i18n/factories/cached-translator-factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { MemoryTranslator } from '../translators/memory-translator'; | ||
import { TranslatorFactory } from '../models/translator-factory'; | ||
|
||
/** | ||
* Creates {@link Translator } instances for the given language tag. | ||
*/ | ||
export class CachedTranslatorFactory implements TranslatorFactory { | ||
|
||
private translations: { [key: string]: string }; | ||
|
||
async createTranslator(language: string): Promise<MemoryTranslator> { | ||
|
||
if (!this.translations) { | ||
|
||
this.translations = await (await fetch(`${window.location.origin}/${language}.json`)).json(); | ||
|
||
} | ||
|
||
return new MemoryTranslator(language, this.translations); | ||
|
||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
packages/dgt-utils/lib/i18n/factories/memory-translator-factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { MemoryTranslator } from '../translators/memory-translator'; | ||
import { TranslatorFactory } from '../models/translator-factory'; | ||
|
||
/** | ||
* Creates {@link Translator } instances for the given language tag. | ||
*/ | ||
export class MemoryTranslatorFactory implements TranslatorFactory { | ||
|
||
createTranslator(language: string): MemoryTranslator { | ||
|
||
return new MemoryTranslator(language); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.