From fe86c44bf8253022215f89d75cef0ef5b8299126 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sun, 13 Aug 2017 13:33:59 -0700 Subject: [PATCH] Add language setting for time formatting. --- src/General/Settings.coffee | 5 ++++- src/General/Settings/Advanced.html | 1 + src/Miscellaneous/Time.coffee | 25 +++++++++++++++++++------ src/config/Config.coffee | 1 + 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/General/Settings.coffee b/src/General/Settings.coffee index 0d8c2efdc..4ebf2cba1 100644 --- a/src/General/Settings.coffee +++ b/src/General/Settings.coffee @@ -493,7 +493,7 @@ Settings = $.id('lastarchivecheck').textContent = 'never' items = {} - for name in ['archiveLists', 'archiveAutoUpdate', 'captchaLanguage', 'boardnav', 'time', 'backlink', 'fileInfo', 'QR.personas', 'favicon', 'usercss', 'customCooldown', 'jsWhitelist'] + for name in ['archiveLists', 'archiveAutoUpdate', 'captchaLanguage', 'boardnav', 'time', 'timeLocale', 'backlink', 'fileInfo', 'QR.personas', 'favicon', 'usercss', 'customCooldown', 'jsWhitelist'] items[name] = Conf[name] input = inputs[name] event = if name in ['archiveLists', 'archiveAutoUpdate', 'QR.personas', 'favicon', 'usercss'] then 'change' else 'input' @@ -646,6 +646,9 @@ Settings = time: -> @nextElementSibling.textContent = Time.format @value, new Date() + timeLocale: -> + Settings.time.call $('[name=time]', Settings.dialog) + backlink: -> @nextElementSibling.textContent = @value.replace /%(?:id|%)/g, (x) -> ({'%id': '123456789', '%%': '%'})[x] diff --git a/src/General/Settings/Advanced.html b/src/General/Settings/Advanced.html index ecc0dae47..53dd2f680 100644 --- a/src/General/Settings/Advanced.html +++ b/src/General/Settings/Advanced.html @@ -64,6 +64,7 @@
Minute: %M
Second: %S
Literal %: %%
+
Language tag:
diff --git a/src/Miscellaneous/Time.coffee b/src/Miscellaneous/Time.coffee index f1ff39693..d1343e058 100644 --- a/src/Miscellaneous/Time.coffee +++ b/src/Miscellaneous/Time.coffee @@ -41,13 +41,26 @@ Time = 'December' ] + localeFormat: (date, options, defaultValue) -> + if Conf['timeLocale'] + try + return Intl.DateTimeFormat(Conf['timeLocale'], options).format(date) + defaultValue + + localeFormatPart: (date, options, part, defaultValue) -> + if Conf['timeLocale'] + try + parts = Intl.DateTimeFormat(Conf['timeLocale'], options).formatToParts(date) + return parts.map((x) -> if x.type is part then x.value else '').join('') + defaultValue + zeroPad: (n) -> if n < 10 then "0#{n}" else n formatters: - a: -> Time.day[@getDay()][...3] - A: -> Time.day[@getDay()] - b: -> Time.month[@getMonth()][...3] - B: -> Time.month[@getMonth()] + a: -> Time.localeFormat @, {weekday: 'short'}, Time.day[@getDay()][...3] + A: -> Time.localeFormat @, {weekday: 'long'}, Time.day[@getDay()] + b: -> Time.localeFormat @, {month: 'short'}, Time.month[@getMonth()][...3] + B: -> Time.localeFormat @, {month: 'long'}, Time.month[@getMonth()] d: -> Time.zeroPad @getDate() e: -> @getDate() H: -> Time.zeroPad @getHours() @@ -56,8 +69,8 @@ Time = l: -> @getHours() % 12 or 12 m: -> Time.zeroPad @getMonth() + 1 M: -> Time.zeroPad @getMinutes() - p: -> if @getHours() < 12 then 'AM' else 'PM' - P: -> if @getHours() < 12 then 'am' else 'pm' + p: -> Time.localeFormatPart @, {hour: 'numeric', hour12: true}, 'dayperiod', (if @getHours() < 12 then 'AM' else 'PM') + P: -> Time.formatters.p.call(@).toLowerCase() S: -> Time.zeroPad @getSeconds() y: -> @getFullYear().toString()[2..] Y: -> @getFullYear() diff --git a/src/config/Config.coffee b/src/config/Config.coffee index 4716c1cc3..da7a350c5 100644 --- a/src/config/Config.coffee +++ b/src/config/Config.coffee @@ -857,6 +857,7 @@ Config = captchaLanguage: '' time: '%m/%d/%y(%a)%H:%M:%S' + timeLocale: '' backlink: '>>%id'