Fix localization issue in DateTime.

The class was only returning a localized string for 'years' and 'months'; English was used for the rest.

- Fixed by handling the remaining cases.
- New strings were added (1) so that they will all be consistent language-wise until the translators handle them all (2) allows for cleaner code through variable re-use (%duration%).
This commit is contained in:
infiinte-persistence 2020-05-09 03:24:16 +08:00 committed by Sean Yesmunt
parent 2b0314d001
commit 2582627f25
2 changed files with 32 additions and 23 deletions

View file

@ -1202,5 +1202,17 @@
"File preview": "File preview",
"Go Home": "Go Home",
"Uploading (%progress%%) ": "Uploading (%progress%%) ",
"Confirming": "Confirming"
}
"Confirming": "Confirming",
"%duration% years ago": "%duration% years ago",
"%duration% year ago": "%duration% year ago",
"%duration% months ago": "%duration% months ago",
"%duration% month ago": "%duration% month ago",
"%duration% days ago": "%duration% days ago",
"%duration% day ago": "%duration% day ago",
"%duration% hours ago": "%duration% hours ago",
"%duration% hour ago": "%duration% hour ago",
"%duration% minutes ago": "%duration% minutes ago",
"%duration% minute ago": "%duration% minute ago",
"%duration% seconds ago": "%duration% seconds ago",
"%duration% second ago": "%duration% second ago"
}

View file

@ -23,32 +23,29 @@ class DateTime extends React.PureComponent<Props> {
return null;
}
// Moment is very liberal with it's rounding
// Wait to show "two years ago" until it's actually been two years (or higher)
const numberOfYearsSincePublish = Math.floor(moment().diff(date, 'years'));
const suffixList = ['years', 'months', 'days', 'hours', 'minutes', 'seconds', ''];
var duration = 0;
if (numberOfYearsSincePublish === 1) {
return <span>{__('%numberOfYearsSincePublish% year ago', { numberOfYearsSincePublish })}</span>;
} else if (numberOfYearsSincePublish > 1) {
return <span>{__('%numberOfYearsSincePublish% years ago', { numberOfYearsSincePublish })}</span>;
for (var i = 0; i < suffixList.length; ++i) {
// moment() is very liberal with it's rounding.
// Always round down dates for better youtube parity.
duration = Math.floor(moment().diff(date, suffixList[i]));
if (duration > 0) {
break;
}
}
const numberOfMonthsSincePublish = Math.floor(moment().diff(date, 'months'));
if (numberOfMonthsSincePublish === 1) {
return <span>{__('%numberOfMonthsSincePublish% month ago', { numberOfMonthsSincePublish })}</span>;
} else if (numberOfMonthsSincePublish > 1) {
return <span>{__('%numberOfMonthsSincePublish% months ago', { numberOfMonthsSincePublish })}</span>;
if (i === suffixList.length) {
// This should never happen since we are handling up to 'seconds' now,
// but display the English version just in case it does.
return moment(date).from(moment());
}
const numberOfDaysSincePublish = Math.floor(moment().diff(date, 'days'));
if (numberOfDaysSincePublish === 1) {
return <span>{__('%numberOfDaysSincePublish% day ago', { numberOfDaysSincePublish })}</span>;
} else if (numberOfDaysSincePublish > 1) {
return <span>{__('%numberOfDaysSincePublish% days ago', { numberOfDaysSincePublish })}</span>;
}
// "just now", "a few minutes ago"
return <span>{moment(date).from(moment())}</span>;
// Strip off the 's' for the singular suffix, construct the string ID,
// then load the localized version.
const suffix = duration === 1 ? suffixList[i].substr(0, suffixList[i].length - 1) : suffixList[i];
const strId = '%duration% ' + suffix + ' ago';
return <span>{__(strId, { duration })}</span>;
}
return (