From 5e1f98631090ab7aeb25c251d98fb9d70f9d5701 Mon Sep 17 00:00:00 2001 From: Daniel Kao Date: Sat, 30 Nov 2024 17:47:51 +0800 Subject: [PATCH] fix: translate minute in reading mode --- app/src/main/assets/MozReadability.js | 26 +++++++++++++++++++ app/src/main/assets/process_text_nodes.js | 8 +++--- .../info/plateaukao/einkbro/view/EBWebView.kt | 4 +-- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/src/main/assets/MozReadability.js b/app/src/main/assets/MozReadability.js index 388fd38b..2f1b1c04 100644 --- a/app/src/main/assets/MozReadability.js +++ b/app/src/main/assets/MozReadability.js @@ -2351,6 +2351,27 @@ function getReadingSpeedForLanguage(lang) { return readingSpeed.get(lang) || readingSpeed.get("en"); } +const minuteTranslations = { + en: 'minutes', + ar: 'دقائق', + de: 'Minuten', + es: 'minutos', + fi: 'minuuttia', + fr: 'minutes', + he: 'דקות', + it: 'minuti', + jw: 'menit', + nl: 'minuten', + pl: 'minut', + pt: 'minutos', + ru: 'минут', + sk: 'minút', + sv: 'minuter', + tr: 'dakika', + zh: '分鐘' +}; + + function getReadingTime(length, lang = "en") { const readingSpeed = this.getReadingSpeedForLanguage(lang); const charactersPerMinuteLow = readingSpeed.cpm - readingSpeed.variance; @@ -2361,8 +2382,11 @@ function getReadingTime(length, lang = "en") { // Construct a localized and "humanized" reading time in minutes. // If we have both a fast and slow reading time we'll show both e.g. // "2 - 4 minutes", otherwise we'll just show "4 minutes". + console.log('lang', lang); + console.log('minuteTranslations[lang]', minuteTranslations[lang]); try { var parts = new Intl.RelativeTimeFormat(lang).formatToParts(readingTimeMinsSlow, 'minute'); + console.log('parts', parts); if (parts.length == 3) { // No need to use part[0] which represents the literal "in". var readingTime = parts[1].value; // reading time in minutes @@ -2372,6 +2396,8 @@ function getReadingTime(length, lang = "en") { readingTimeString = `${readingTimeMinsFast} - ${readingTimeString}`; } return readingTimeString; + } else { + return `${readingTimeMinsSlow} ${minuteTranslations[lang]}`; } } catch(error) { diff --git a/app/src/main/assets/process_text_nodes.js b/app/src/main/assets/process_text_nodes.js index 08e46a27..69ba21fd 100644 --- a/app/src/main/assets/process_text_nodes.js +++ b/app/src/main/assets/process_text_nodes.js @@ -14,8 +14,8 @@ function convertToVerticalStyle(node) { // digits longer than 3 characters , e.g. 12345, keep 2024 similar year numbers if (match[0].length >= 3) { - console.log("matchedText.length >= 3"); - console.log(matchedText); + //console.log("matchedText.length >= 3"); + //console.log(matchedText); if (matchedText[1] != '.') { if (lastIndex < match.index) { fragment.appendChild(document.createTextNode(text.slice(lastIndex, match.index))); @@ -139,10 +139,10 @@ function convertListLabel(node) { olLists.forEach((ol) => { const listStyleType = window.getComputedStyle(ol).listStyleType; - console.log(listStyleType); + //console.log(listStyleType); const listItems = ol.querySelectorAll("li"); listItems.forEach((li, index) => { - console.log(li.textContent); + //console.log(li.textContent); const marker = convertLabel(listStyleType, index); li.setAttribute("data-marker", marker); }); diff --git a/app/src/main/java/info/plateaukao/einkbro/view/EBWebView.kt b/app/src/main/java/info/plateaukao/einkbro/view/EBWebView.kt index c2501a3d..72f6bab7 100755 --- a/app/src/main/java/info/plateaukao/einkbro/view/EBWebView.kt +++ b/app/src/main/java/info/plateaukao/einkbro/view/EBWebView.kt @@ -1104,7 +1104,7 @@ open class EBWebView( var article = new Readability(documentClone, $readabilityOptions).parse(); document.innerHTMLCache = document.body.innerHTML; - article.readingTime = getReadingTime(article.length, document.lang); + article.readingTime = getReadingTime(article.length, document.documentElement.lang.substring(0, 2)); document.body.outerHTML = createHtmlBody(article) @@ -1115,7 +1115,7 @@ open class EBWebView( javascript:(function() { var documentClone = document.cloneNode(true); var article = new Readability(documentClone, $readabilityOptions).parse(); - article.readingTime = getReadingTime(article.length, document.lang); + article.readingTime = getReadingTime(article.length, document.documentElement.lang.substring(0, 2)); var bodyOuterHTML = createHtmlBodyWithUrl(article, "%s") var headOuterHTML = document.head.outerHTML; return (''+ headOuterHTML + bodyOuterHTML +'');