diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-01-03 12:12:55 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-03 12:12:55 -0500 |
commit | 8e304b83c685dde17a00d402877a21303b7c11f2 (patch) | |
tree | 0b2123575502c3e3cb5127582b03e9c196c9891d /ext/bg/js/backend.js | |
parent | eda8534e195d653ee0dea36f70caed0d8d49acf1 (diff) |
Translator regex replacements (#1199)
* Add support for regex replacements during the translation process
* Allow assignment of textReplacements
* Rename
* Set up test data
* Write expected data
* Set up options
* Prevent infinite loop if regex matches empty string
* Implement setting controller
* Add support for testing pattern replacements
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r-- | ext/bg/js/backend.js | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index c7131728..690f6a3c 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -1655,9 +1655,11 @@ class Backend { convertAlphabeticCharacters, convertHiraganaToKatakana, convertKatakanaToHiragana, - collapseEmphaticSequences + collapseEmphaticSequences, + textReplacements: textReplacementsOptions } } = options; + const textReplacements = this._getTranslatorTextReplacements(textReplacementsOptions); return { wildcard, mainDictionary, @@ -1668,6 +1670,7 @@ class Backend { convertHiraganaToKatakana, convertKatakanaToHiragana, collapseEmphaticSequences, + textReplacements, enabledDictionaryMap }; } @@ -1686,6 +1689,29 @@ class Backend { return enabledDictionaryMap; } + _getTranslatorTextReplacements(textReplacementsOptions) { + const textReplacements = []; + for (const group of textReplacementsOptions.groups) { + const textReplacementsEntries = []; + for (let {pattern, ignoreCase, replacement} of group) { + try { + pattern = new RegExp(pattern, ignoreCase ? 'gi' : 'g'); + } catch (e) { + // Invalid pattern + continue; + } + textReplacementsEntries.push({pattern, replacement}); + } + if (textReplacementsEntries.length > 0) { + textReplacements.push(textReplacementsEntries); + } + } + if (textReplacements.length === 0 || textReplacementsOptions.searchOriginal) { + textReplacements.unshift(null); + } + return textReplacements; + } + async _openWelcomeGuidePage() { await this._createTab(chrome.runtime.getURL('/bg/welcome.html')); } |