-
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(config): Generator is open to configuration
- Loading branch information
Showing
10 changed files
with
337 additions
and
105 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
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,37 @@ | ||
import { Rule } from "../config/config-loader"; | ||
import { TranslationEntry } from "../translation/generate-template-data"; | ||
import { | ||
AddPlaceholderTransformer, | ||
RemoveKeyPartTransformer, | ||
applyAddPlaceholderTransformer, | ||
applyRemoveLastPartTransformer, | ||
} from "./transformer"; | ||
|
||
type KeyEndsWithCondition = { keyEndsWith: string[] }; | ||
|
||
export type KeyEndsWithRule = { | ||
condition: KeyEndsWithCondition; | ||
transformer: AddPlaceholderTransformer | RemoveKeyPartTransformer; | ||
}; | ||
|
||
export function isKeyRule(rule: Rule): rule is KeyEndsWithRule { | ||
return "keyEndsWith" in rule.condition; | ||
} | ||
|
||
export function applyKeyEndsWithRule( | ||
rule: KeyEndsWithRule, | ||
entry: TranslationEntry | ||
): TranslationEntry { | ||
const { key } = entry; | ||
let result = entry; | ||
const match = rule.condition.keyEndsWith.some((endingPart) => key.endsWith(`.${endingPart}`)); | ||
if (match) { | ||
if ("removeLastPart" in rule.transformer) { | ||
result = applyRemoveLastPartTransformer(rule.transformer, result); | ||
} | ||
if ("addPlaceholder" in rule.transformer) { | ||
result = applyAddPlaceholderTransformer(rule.transformer, result); | ||
} | ||
} | ||
return result; | ||
} |
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,53 @@ | ||
import { TranslationEntry } from "../translation/generate-template-data"; | ||
|
||
export type AddPlaceholderTransformer = { | ||
addPlaceholder: { | ||
name: string; | ||
type: string[]; | ||
}; | ||
}; | ||
export type RemoveKeyPartTransformer = { removeLastPart: true }; | ||
export type AddMatchedPlaceholderTransformer = { | ||
addMatchedPlaceholder: { | ||
type: string[]; | ||
}; | ||
}; | ||
|
||
export function applyAddPlaceholderTransformer( | ||
transformer: AddPlaceholderTransformer, | ||
entry: TranslationEntry | ||
): TranslationEntry { | ||
const previousType = | ||
entry.interpolations.get(transformer.addPlaceholder.name) ?? []; | ||
entry.interpolations.set( | ||
transformer.addPlaceholder.name, | ||
previousType.concat(transformer.addPlaceholder.type) | ||
); | ||
return entry; | ||
} | ||
|
||
export function applyAddMatchedPlaceholderTransformer( | ||
transformer: AddMatchedPlaceholderTransformer, | ||
placeholder: string, | ||
entry: TranslationEntry | ||
): TranslationEntry { | ||
const previousType = entry.interpolations.get(placeholder) ?? []; | ||
entry.interpolations.set( | ||
placeholder, | ||
previousType.concat(transformer.addMatchedPlaceholder.type) | ||
); | ||
return entry; | ||
} | ||
|
||
export function applyRemoveLastPartTransformer( | ||
transformer: RemoveKeyPartTransformer, | ||
entry: TranslationEntry | ||
): TranslationEntry { | ||
if (transformer.removeLastPart) { | ||
return { ...entry, key: removeLastPart(entry.key) }; | ||
} | ||
return entry; | ||
} | ||
|
||
const removeLastPart = (key: string, delimiter = ".") => | ||
key.split(delimiter).slice(0, -1).join(delimiter); |
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,54 @@ | ||
import { Rule } from "../config/config-loader"; | ||
import { TranslationEntry } from "../translation/generate-template-data"; | ||
import { | ||
AddMatchedPlaceholderTransformer, | ||
AddPlaceholderTransformer, | ||
applyAddMatchedPlaceholderTransformer, | ||
applyAddPlaceholderTransformer, | ||
} from "./transformer"; | ||
|
||
type TranslationMatchCondition = { | ||
placeholderPattern: { | ||
prefix: string; | ||
suffix: string; | ||
}; | ||
}; | ||
|
||
export type TranslationMatchRule = { | ||
condition: TranslationMatchCondition; | ||
transformer: AddMatchedPlaceholderTransformer | AddPlaceholderTransformer; | ||
}; | ||
|
||
export function isTranslationRule(rule: Rule): rule is TranslationMatchRule { | ||
return "placeholderPattern" in rule.condition; | ||
} | ||
|
||
export function applyTranslationMatchRule( | ||
rule: TranslationMatchRule, | ||
entry: TranslationEntry | ||
): TranslationEntry { | ||
let result = entry; | ||
const { prefix, suffix } = rule.condition.placeholderPattern; | ||
const regexp = new RegExp(`${prefix}(.*?)${suffix}`, "g"); | ||
|
||
const matches: string[] = []; | ||
let match; | ||
while ((match = regexp.exec(entry.translation)) !== null) { | ||
matches.push(match[1]); | ||
} | ||
|
||
if ("addMatchedPlaceholder" in rule.transformer) { | ||
const transformer = rule.transformer; | ||
matches.forEach((placeholder) => { | ||
result = applyAddMatchedPlaceholderTransformer( | ||
transformer, | ||
placeholder, | ||
result | ||
); | ||
}); | ||
} | ||
if ("addPlaceholder" in rule.transformer) { | ||
result = applyAddPlaceholderTransformer(rule.transformer, result); | ||
} | ||
return result; | ||
} |
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,10 +1,15 @@ | ||
export interface InterpolationTemplateData { | ||
name: string; | ||
type: "string" | "number"; | ||
type: InterpolationTypeTemplateData[]; | ||
last?: boolean; | ||
} | ||
|
||
export interface WordingEntryTemplateData { | ||
export interface InterpolationTypeTemplateData { | ||
value: string; | ||
last?: boolean; | ||
} | ||
|
||
export interface TranslationEntryTemplateData { | ||
key: string; | ||
interpolations: InterpolationTemplateData[]; | ||
} |
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
Oops, something went wrong.