-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
344 additions
and
328 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
110 changes: 55 additions & 55 deletions
110
libs/transloco-validator/src/lib/transloco-validator.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 |
---|---|---|
@@ -1,56 +1,56 @@ | ||
import fs from 'fs'; | ||
|
||
import findDuplicatedPropertyKeys from 'find-duplicated-property-keys'; | ||
|
||
export default function (interpolationForbiddenChars: string, translationFilePaths: string[]) { | ||
translationFilePaths.forEach((path) => { | ||
const translation = fs.readFileSync(path, 'utf-8'); | ||
|
||
// Verify that we can parse the JSON | ||
let parsedTranslation; | ||
try { | ||
parsedTranslation = JSON.parse(translation); | ||
} catch(error) { | ||
throw new SyntaxError( | ||
`${error.message} (${path})` | ||
); | ||
} | ||
|
||
// Verify that we don't have any duplicate keys | ||
const duplicatedKeys = findDuplicatedPropertyKeys(translation); | ||
if (duplicatedKeys.length) { | ||
throw new Error( | ||
`Found duplicate keys: ${duplicatedKeys.map(dupl => dupl.toString())} (${path})` | ||
); | ||
} | ||
|
||
const forbiddenKeys = findPropertyKeysContaining(parsedTranslation, interpolationForbiddenChars); | ||
if (forbiddenKeys.length) { | ||
throw new Error( | ||
`Found forbidden characters [${interpolationForbiddenChars}] in keys: ${forbiddenKeys} (${path})` | ||
); | ||
} | ||
}); | ||
} | ||
|
||
function findPropertyKeysContaining(object: unknown, chars: string, parent = '<instance>') { | ||
const found = []; | ||
if (Array.isArray(object)) { | ||
for(let i = 0; i < object.length; i++) { | ||
const value = object[i]; | ||
found.push(...findPropertyKeysContaining(value, chars, `${parent}[${i}]`)); | ||
} | ||
} else if (typeof object === 'object') { | ||
for(const key in object) { | ||
const value = object[key]; | ||
for (const char of chars) { | ||
if (key.includes(char)) { | ||
found.push(parent + '.' + key); | ||
break; | ||
} | ||
} | ||
found.push(...findPropertyKeysContaining(value, chars, `${parent}.${key}`)); | ||
} | ||
} | ||
return found; | ||
import fs from 'fs'; | ||
|
||
import findDuplicatedPropertyKeys from 'find-duplicated-property-keys'; | ||
|
||
export default function (interpolationForbiddenChars: string, translationFilePaths: string[]) { | ||
translationFilePaths.forEach((path) => { | ||
const translation = fs.readFileSync(path, 'utf-8'); | ||
|
||
// Verify that we can parse the JSON | ||
let parsedTranslation; | ||
try { | ||
parsedTranslation = JSON.parse(translation); | ||
} catch(error) { | ||
throw new SyntaxError( | ||
`${error.message} (${path})` | ||
); | ||
} | ||
|
||
// Verify that we don't have any duplicate keys | ||
const duplicatedKeys = findDuplicatedPropertyKeys(translation); | ||
if (duplicatedKeys.length) { | ||
throw new Error( | ||
`Found duplicate keys: ${duplicatedKeys.map(dupl => dupl.toString())} (${path})` | ||
); | ||
} | ||
|
||
const forbiddenKeys = findPropertyKeysContaining(parsedTranslation, interpolationForbiddenChars); | ||
if (forbiddenKeys.length) { | ||
throw new Error( | ||
`Found forbidden characters [${interpolationForbiddenChars}] in keys: ${forbiddenKeys} (${path})` | ||
); | ||
} | ||
}); | ||
} | ||
|
||
function findPropertyKeysContaining(object: unknown, chars: string, parent = '<instance>') { | ||
const found = []; | ||
if (Array.isArray(object)) { | ||
for(let i = 0; i < object.length; i++) { | ||
const value = object[i]; | ||
found.push(...findPropertyKeysContaining(value, chars, `${parent}[${i}]`)); | ||
} | ||
} else if (typeof object === 'object') { | ||
for(const key in object) { | ||
const value = object[key]; | ||
for (const char of chars) { | ||
if (key.includes(char)) { | ||
found.push(parent + '.' + key); | ||
break; | ||
} | ||
} | ||
found.push(...findPropertyKeysContaining(value, chars, `${parent}.${key}`)); | ||
} | ||
} | ||
return found; | ||
} |
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 |
---|---|---|
@@ -1,72 +1,72 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
import { AvailableLangs } from './types'; | ||
|
||
export interface TranslocoConfig { | ||
defaultLang: string; | ||
reRenderOnLangChange: boolean; | ||
prodMode: boolean; | ||
fallbackLang?: string | string[]; | ||
failedRetries: number; | ||
availableLangs: AvailableLangs; | ||
flatten: { | ||
aot: boolean; | ||
}; | ||
missingHandler: { | ||
logMissingKey: boolean; | ||
useFallbackTranslation: boolean; | ||
allowEmpty: boolean; | ||
}; | ||
interpolation: [start: string, end: string, forbiddenChars?: string]; | ||
} | ||
|
||
export const TRANSLOCO_CONFIG = new InjectionToken<TranslocoConfig>( | ||
'TRANSLOCO_CONFIG', | ||
{ | ||
providedIn: 'root', | ||
factory: () => defaultConfig, | ||
} | ||
); | ||
|
||
export const defaultConfig: TranslocoConfig = { | ||
defaultLang: 'en', | ||
reRenderOnLangChange: false, | ||
prodMode: false, | ||
failedRetries: 2, | ||
fallbackLang: [], | ||
availableLangs: [], | ||
missingHandler: { | ||
logMissingKey: true, | ||
useFallbackTranslation: false, | ||
allowEmpty: false, | ||
}, | ||
flatten: { | ||
aot: false, | ||
}, | ||
interpolation: ['{{', '}}', '{}'], | ||
}; | ||
|
||
type DeepPartial<T> = T extends Array<any> | ||
? T | ||
: T extends object | ||
? { [P in keyof T]?: DeepPartial<T[P]> } | ||
: T; | ||
|
||
export type PartialTranslocoConfig = DeepPartial<TranslocoConfig>; | ||
|
||
export function translocoConfig( | ||
config: PartialTranslocoConfig = {} | ||
): TranslocoConfig { | ||
return { | ||
...defaultConfig, | ||
...config, | ||
missingHandler: { | ||
...defaultConfig.missingHandler, | ||
...config.missingHandler, | ||
}, | ||
flatten: { | ||
...defaultConfig.flatten, | ||
...config.flatten, | ||
}, | ||
}; | ||
} | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
import { AvailableLangs } from './types'; | ||
|
||
export interface TranslocoConfig { | ||
defaultLang: string; | ||
reRenderOnLangChange: boolean; | ||
prodMode: boolean; | ||
fallbackLang?: string | string[]; | ||
failedRetries: number; | ||
availableLangs: AvailableLangs; | ||
flatten: { | ||
aot: boolean; | ||
}; | ||
missingHandler: { | ||
logMissingKey: boolean; | ||
useFallbackTranslation: boolean; | ||
allowEmpty: boolean; | ||
}; | ||
interpolation: [start: string, end: string, forbiddenChars?: string]; | ||
} | ||
|
||
export const TRANSLOCO_CONFIG = new InjectionToken<TranslocoConfig>( | ||
'TRANSLOCO_CONFIG', | ||
{ | ||
providedIn: 'root', | ||
factory: () => defaultConfig, | ||
} | ||
); | ||
|
||
export const defaultConfig: TranslocoConfig = { | ||
defaultLang: 'en', | ||
reRenderOnLangChange: false, | ||
prodMode: false, | ||
failedRetries: 2, | ||
fallbackLang: [], | ||
availableLangs: [], | ||
missingHandler: { | ||
logMissingKey: true, | ||
useFallbackTranslation: false, | ||
allowEmpty: false, | ||
}, | ||
flatten: { | ||
aot: false, | ||
}, | ||
interpolation: ['{{', '}}', '{}'], | ||
}; | ||
|
||
type DeepPartial<T> = T extends Array<any> | ||
? T | ||
: T extends object | ||
? { [P in keyof T]?: DeepPartial<T[P]> } | ||
: T; | ||
|
||
export type PartialTranslocoConfig = DeepPartial<TranslocoConfig>; | ||
|
||
export function translocoConfig( | ||
config: PartialTranslocoConfig = {} | ||
): TranslocoConfig { | ||
return { | ||
...defaultConfig, | ||
...config, | ||
missingHandler: { | ||
...defaultConfig.missingHandler, | ||
...config.missingHandler, | ||
}, | ||
flatten: { | ||
...defaultConfig.flatten, | ||
...config.flatten, | ||
}, | ||
}; | ||
} |
Oops, something went wrong.