Skip to content

Commit

Permalink
fix: translate minute in reading mode
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Nov 30, 2024
1 parent 36c6e37 commit 5e1f986
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
26 changes: 26 additions & 0 deletions app/src/main/assets/MozReadability.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -2372,6 +2396,8 @@ function getReadingTime(length, lang = "en") {
readingTimeString = `${readingTimeMinsFast} - ${readingTimeString}`;
}
return readingTimeString;
} else {
return `${readingTimeMinsSlow} ${minuteTranslations[lang]}`;
}
}
catch(error) {
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/assets/process_text_nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/info/plateaukao/einkbro/view/EBWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 ('<html>'+ headOuterHTML + bodyOuterHTML +'</html>');
Expand Down

0 comments on commit 5e1f986

Please sign in to comment.