aboutsummaryrefslogtreecommitdiff
path: root/ext/js/display/display-generator.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2024-02-25 11:20:44 -0500
committerGitHub <noreply@github.com>2024-02-25 16:20:44 +0000
commit2e9ea19207a7410f929bb908759d48cb2340f29c (patch)
treea6bde1297d693bb8d50e4c93a963aa3179e5a2ce /ext/js/display/display-generator.js
parent73169f06dff767020718a5715eba97d3575ba7e1 (diff)
"isJapanese" check move (#730)
* Move isStringPartiallyJapanese out of ClipboardMonitor * Create isStringPartiallyJapanese function * Add textMayBeTranslatable * Rename API function * Rename internal function * Add helper * Update translatable check * Pass language to TextScanner * Pass language explicitly * Use textMayBeTranslatable * No redundant translatable check * Update eslint * Remove double newline * Collapse * Rename
Diffstat (limited to 'ext/js/display/display-generator.js')
-rw-r--r--ext/js/display/display-generator.js32
1 files changed, 20 insertions, 12 deletions
diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js
index 22912e9f..0b3236e9 100644
--- a/ext/js/display/display-generator.js
+++ b/ext/js/display/display-generator.js
@@ -20,7 +20,8 @@ import {ExtensionError} from '../core/extension-error.js';
import {isObject} from '../core/utilities.js';
import {getDisambiguations, getGroupedPronunciations, getTermFrequency, groupKanjiFrequencies, groupTermFrequencies, groupTermTags, isNonNounVerbOrAdjective} from '../dictionary/dictionary-data-util.js';
import {HtmlTemplateCollection} from '../dom/html-template-collection.js';
-import {distributeFurigana, getKanaMorae, getPitchCategory, isCodePointKanji, isStringPartiallyJapanese} from '../language/ja/japanese.js';
+import {distributeFurigana, getKanaMorae, getPitchCategory, isCodePointKanji} from '../language/ja/japanese.js';
+import {getLanguageFromText} from '../language/text-utilities.js';
import {createPronunciationDownstepPosition, createPronunciationGraph, createPronunciationText} from './sandbox/pronunciation-generator.js';
import {StructuredContentGenerator} from './sandbox/structured-content-generator.js';
@@ -991,12 +992,7 @@ export class DisplayGenerator {
* @param {string} [language]
*/
_setTextContent(node, value, language) {
- if (typeof language === 'string') {
- node.lang = language;
- } else if (isStringPartiallyJapanese(value)) {
- node.lang = 'ja';
- }
-
+ this._setElementLanguage(node, language, value);
node.textContent = value;
}
@@ -1008,11 +1004,7 @@ export class DisplayGenerator {
_setMultilineTextContent(node, value, language) {
// This can't just call _setTextContent because the lack of <br> elements will
// cause the text to not copy correctly.
- if (typeof language === 'string') {
- node.lang = language;
- } else if (isStringPartiallyJapanese(value)) {
- node.lang = 'ja';
- }
+ this._setElementLanguage(node, language, value);
let start = 0;
while (true) {
@@ -1029,6 +1021,22 @@ export class DisplayGenerator {
}
/**
+ * @param {HTMLElement} element
+ * @param {string|undefined} language
+ * @param {string} content
+ */
+ _setElementLanguage(element, language, content) {
+ if (typeof language === 'string') {
+ element.lang = language;
+ } else {
+ const language2 = getLanguageFromText(content);
+ if (language2 !== null) {
+ element.lang = language2;
+ }
+ }
+ }
+
+ /**
* @param {string} reading
* @param {import('dictionary').TermPronunciation[]} termPronunciations
* @param {string[]} wordClasses