forked from bartholomej/ngx-translate-extract
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add key-as-initial-default-value flag (#49)
* feat!(keep): Add check if key is pre-existing * feat(keep): Add new post processor for key as initial default value * fix(keep): Revert change made to key-as-default post-processor
- Loading branch information
1 parent
1bd6d8b
commit 0771f62
Showing
5 changed files
with
62 additions
and
7 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
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
10 changes: 10 additions & 0 deletions
10
src/post-processors/key-as-initial-default-value.post-processor.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,10 @@ | ||
import {TranslationCollection, TranslationInterface} from '../utils/translation.collection.js'; | ||
import { PostProcessorInterface } from './post-processor.interface.js'; | ||
|
||
export class KeyAsInitialDefaultValuePostProcessor implements PostProcessorInterface { | ||
public name: string = 'KeyAsInitialDefaultValue'; | ||
|
||
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { | ||
return draft.map((key: string, val: TranslationInterface): TranslationInterface => val.value === '' && !existing.has(key) ? {value: key, sourceFiles: (val?.sourceFiles || [])} : val); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/post-processors/key-as-initial-default-value.post-processor.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,33 @@ | ||
import { describe, beforeEach, expect, it } from 'vitest'; | ||
|
||
import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface.js'; | ||
import { KeyAsInitialDefaultValuePostProcessor } from '../../src/post-processors/key-as-initial-default-value.post-processor.js'; | ||
import { TranslationCollection } from '../../src/utils/translation.collection.js'; | ||
|
||
describe('KeyAsInitialDefaultValuePostProcessor', () => { | ||
let processor: PostProcessorInterface; | ||
|
||
beforeEach(() => { | ||
processor = new KeyAsInitialDefaultValuePostProcessor(); | ||
}); | ||
|
||
it('should use key as default value', () => { | ||
const collection = new TranslationCollection({ | ||
'I have no value': { value: '', sourceFiles: [] }, | ||
'I have no value but I exist': { value: '', sourceFiles: [] }, | ||
'I am already translated': { value: 'Jeg er allerede oversat', sourceFiles: [] }, | ||
'Use this key as value as well': { value: '', sourceFiles: ['path/to/file.ts'] } | ||
}); | ||
const extracted = new TranslationCollection(); | ||
const existing = new TranslationCollection({ | ||
'I have no value but I exist': { value: '', sourceFiles: [] } | ||
}); | ||
|
||
expect(processor.process(collection, extracted, existing).values).to.deep.equal({ | ||
'I have no value': { value: 'I have no value', sourceFiles: [] }, | ||
'I have no value but I exist': { value: '', sourceFiles: [] }, | ||
'I am already translated': { value: 'Jeg er allerede oversat', sourceFiles: [] }, | ||
'Use this key as value as well': { value: 'Use this key as value as well', sourceFiles: ['path/to/file.ts'] } | ||
}); | ||
}); | ||
}); |