aboutsummaryrefslogtreecommitdiff
path: root/ext/js/language
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/language')
-rw-r--r--ext/js/language/translator.js22
1 files changed, 20 insertions, 2 deletions
diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js
index 733955c2..36ed8b43 100644
--- a/ext/js/language/translator.js
+++ b/ext/js/language/translator.js
@@ -329,8 +329,12 @@ export class Translator {
text2 = jp.collapseEmphaticSequences(text2, collapseEmphaticFull, sourceMap);
}
- for (let i = text2.length; i > 0; --i) {
- const source = text2.substring(0, i);
+ for (
+ let source = text2, i = text2.length;
+ i > 0;
+ i = this._getNextSubstringLength(options.searchResolution, i, source)
+ ) {
+ source = text2.substring(0, i);
if (used.has(source)) { break; }
used.add(source);
const rawSource = sourceMap.source.substring(0, sourceMap.getSourceLength(i));
@@ -343,6 +347,20 @@ export class Translator {
}
/**
+ * @param {string} searchResolution
+ * @param {number} currentLength
+ * @param {string} source
+ * @returns {number}
+ */
+ _getNextSubstringLength(searchResolution, currentLength, source) {
+ if (searchResolution === 'word') {
+ return source.search(/[^\p{Letter}][\p{Letter}\p{Number}]*$/u);
+ } else {
+ return currentLength - 1;
+ }
+ }
+
+ /**
* @param {string} text
* @param {TextSourceMap} sourceMap
* @param {import('translation').FindTermsTextReplacement[]} replacements