Skip to content

Commit

Permalink
extract function
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Mar 28, 2024
1 parent c3615eb commit fa5a72b
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,37 +457,29 @@ export class Translator {
const used = new Set();

for (
let source = text, i = text.length;
let i = text.length;
i > 0;
i = this._getNextSubstringLength(options.searchResolution, i, source)
i = this._getNextSubstringLength(options.searchResolution, i, text)
) {
const rawSource = text.substring(0, i);

for (const preprocessorVariant of preprocessorVariants) {
source = rawSource;
let source = rawSource;

const textReplacements = /** @type {import('translation').FindTermsTextReplacement[] | null} */ (preprocessorVariant.get('textReplacements'));
if (textReplacements !== null) {
source = this._applyTextReplacements(source, textReplacements);
}

for (const preprocessor of textPreprocessors.values()) {
const {id, textProcessor} = preprocessor;
const setting = preprocessorVariant.get(id);
source = textProcessor.process(source, setting);
}
source = this._applyTextProcessors(textPreprocessors, preprocessorVariant, source);

if (used.has(source)) { continue; }
used.add(source);
for (const deinflection of this._multiLanguageTransformer.transform(language, source)) {
const {trace, conditions} = deinflection;
for (const postprocessorVariant of postprocessorVariants) {
let {text: transformedText} = deinflection;
for (const postprocessor of textPostprocessors.values()) {
const {id, textProcessor} = postprocessor;
const setting = postprocessorVariant.get(id);
transformedText = textProcessor.process(transformedText, setting);
}
transformedText = this._applyTextProcessors(textPostprocessors, postprocessorVariant, transformedText);

/** @type {import('dictionary').InflectionRuleChainCandidate} */
const inflectionRuleChainCandidate = {
Expand All @@ -502,6 +494,21 @@ export class Translator {
return deinflections;
}

/**
* @param {import('language').TextProcessorWithId<unknown>[]} textPostprocessors
* @param {Map<string, unknown>}postprocessorVariant
* @param {string} transformedText
* @returns {string}
*/
_applyTextProcessors(textPostprocessors, postprocessorVariant, transformedText) {
for (const postprocessor of textPostprocessors.values()) {
const {id, textProcessor} = postprocessor;
const setting = postprocessorVariant.get(id);
transformedText = textProcessor.process(transformedText, setting);
}
return transformedText;
}

/**
* @param {string} searchResolution
* @param {number} currentLength
Expand Down

0 comments on commit fa5a72b

Please sign in to comment.