From 901c933632f99d4d55fdca6e9b8896d65d14e026 Mon Sep 17 00:00:00 2001 From: MMP0 Date: Sun, 29 Oct 2017 00:01:56 +0900 Subject: [PATCH] Improve date formatting (#1693) * Update main.js * Update base.jet.html * Update CHANGELOG.md * Update en-us.all.json * Update ja-jp.all.json * Update main.js --- public/js/main.js | 15 ++++++++++++--- templates/layouts/partials/base.jet.html | 4 +++- translations/CHANGELOG.md | 3 +++ translations/en-us.all.json | 8 ++++++++ translations/ja-jp.all.json | 8 ++++++++ 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/public/js/main.js b/public/js/main.js index 9e504aa2..354628f4 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -40,6 +40,13 @@ function toggleLayer(elem) { } } +// String formatter +// "{0}{2}, {1}".format("foo", 123, "bar") == "foobar, 123" +String.prototype.format = function() { + var args = arguments + return this.replace(/\{(\w+)\}/g, function(m, k) { return args[k] }) +} + function parseAllDates() { // Date formatting var lang = document.getElementsByTagName("html")[0].getAttribute("lang") @@ -58,6 +65,7 @@ function parseAllDates() { var e = list[i] e.innerText = new Date(e.title).toLocaleString(lang, ymdOpt) e.title = new Date(e.title).toLocaleString(lang) + e.classList.remove("date-short") } var list = document.getElementsByClassName("date-full") @@ -65,12 +73,13 @@ function parseAllDates() { var e = list[i] var dateDifference = dateDiff(new Date(e.innerText), new Date()) - if(e.className != undefined && e.className.includes("scrape-date")) - e.title = ((dateDifference.d * 24) + dateDifference.h) + " hours " + dateDifference.m + " minutes ago" + if(e.classList.contains("scrape-date")) + e.title = hmFmt.format((dateDifference.d * 24) + dateDifference.h, dateDifference.m) else - e.title = dateDifference.d + " days " + dateDifference.h + " hours ago" + e.title = dhFmt.format(dateDifference.d, dateDifference.h) e.innerText = new Date(e.innerText).toLocaleString(lang) + e.classList.remove("date-full") } } function dateDiff( str1, str2 ) { diff --git a/templates/layouts/partials/base.jet.html b/templates/layouts/partials/base.jet.html index 8eb3596e..947165e7 100644 --- a/templates/layouts/partials/base.jet.html +++ b/templates/layouts/partials/base.jet.html @@ -100,7 +100,9 @@ websiteVersion = "{{ Config.Version }}", UserID = {{User.ID}}, domain = "{{getDomainName()}}", - darkTheme = "{{DarkTheme}}"; + darkTheme = "{{DarkTheme}}", + hmFmt = "{{ T("hours_minutes_ago") }}", + dhFmt = "{{ T("days_hours_ago") }}"; diff --git a/translations/CHANGELOG.md b/translations/CHANGELOG.md index 8e57d023..2e0feba7 100644 --- a/translations/CHANGELOG.md +++ b/translations/CHANGELOG.md @@ -60,3 +60,6 @@ * + generating_torrent ## 2017/10/09 * + no_api_token +## 2017/10/28 +* + hours_minutes_ago +* + days_hours_ago diff --git a/translations/en-us.all.json b/translations/en-us.all.json index f451a601..3f708b05 100644 --- a/translations/en-us.all.json +++ b/translations/en-us.all.json @@ -2174,5 +2174,13 @@ { "id": "search_from_specific_user", "translation": "Search from %s" + }, + { + "id": "hours_minutes_ago", + "translation": "{0} hours {1} minutes ago" + }, + { + "id": "days_hours_ago", + "translation": "{0} days {1} hours ago" } ] diff --git a/translations/ja-jp.all.json b/translations/ja-jp.all.json index f36fc420..1038ae58 100644 --- a/translations/ja-jp.all.json +++ b/translations/ja-jp.all.json @@ -2174,5 +2174,13 @@ { "id": "search_from_specific_user", "translation": "%s のアップロードを検索" + }, + { + "id": "hours_minutes_ago", + "translation": "{0} 時間 {1} 分前" + }, + { + "id": "days_hours_ago", + "translation": "{0} 日 {1} 時間前" } ]