Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feature: translate date display

goenninga5e6f4b9a5fe
diff --git a/app/pkg/web/renderer.go b/app/pkg/web/renderer.go
index b354bc12f..54fd44d92 100644
--- a/app/pkg/web/renderer.go
+++ b/app/pkg/web/renderer.go
@@ -168,7 +168,8 @@ func (r *Renderer) Render(w io.Writer, statusCode int, templateName string, prop
private["assets"] = r.assets
private["logo"] = LogoURL(ctx)
- localeChunkName := fmt.Sprintf("locale-%s-client-json", env.Config.Locale)
+ locale := i18n.GetLocale(ctx)
+ localeChunkName := fmt.Sprintf("locale-%s-client-json", locale)
private["preloadAssets"] = []*clientAssets{
r.chunkedAssets[localeChunkName],
r.chunkedAssets[props.ChunkName],
@@ -201,7 +202,7 @@ func (r *Renderer) Render(w io.Writer, statusCode int, templateName string, prop
public["props"] = props.Data
public["settings"] = &Map{
"mode": env.Config.HostMode,
- "locale": i18n.GetLocale(ctx),
+ "locale": locale,
"environment": env.Config.Environment,
"googleAnalytics": env.Config.GoogleAnalytics,
"domain": env.MultiTenantDomain(),
diff --git a/public/components/common/Moment.tsx b/public/components/common/Moment.tsx
index 39b3b6307..754926c43 100644
--- a/public/components/common/Moment.tsx
+++ b/public/components/common/Moment.tsx
@@ -2,6 +2,7 @@ import React from "react"
import { formatDate, timeSince } from "@fider/services"
interface MomentText {
+ locale: string
date: Date | string
format?: "relative" | "full" | "short"
}
@@ -17,10 +18,14 @@ export const Moment = (props: MomentText) => {
const date = props.date instanceof Date ? props.date : new Date(props.date)
const diff = (now.getTime() - date.getTime()) / (60 * 60 * 24 * 1000)
const display =
- diff >= 365 && format === "relative" ? formatDate(props.date, "short") : format === "relative" ? timeSince(now, date) : formatDate(props.date, format)
+ diff >= 365 && format === "relative"
+ ? formatDate(props.locale, props.date, "short")
+ : format === "relative"
+ ? timeSince(props.locale, now, date)
+ : formatDate(props.locale, props.date, format)
return (
- <span className="date" data-tooltip={formatDate(props.date, "full")}>
+ <span className="date" data-tooltip={formatDate(props.locale, props.date, "full")}>
{display}
</span>
)
diff --git a/public/pages/DesignSystem/DesignSystem.page.tsx b/public/pages/DesignSystem/DesignSystem.page.tsx
index a3e1b31c3..abc2f777c 100644
--- a/public/pages/DesignSystem/DesignSystem.page.tsx
+++ b/public/pages/DesignSystem/DesignSystem.page.tsx
@@ -33,6 +33,7 @@ import { notify, Failure } from "@fider/services"
import { HStack, VStack } from "@fider/components/layout"
import IconLightBulb from "@fider/assets/images/heroicons-light-bulb.svg"
import IconSearch from "@fider/assets/images/heroicons-search.svg"
+import { useFider } from "@fider/hooks"
const jonSnow: User = {
id: 0,
@@ -68,6 +69,7 @@ const visibilityPublic = { label: "Public", value: "public" }
const visibilityPrivate = { label: "Private", value: "private" }
const DesignSystemPage = () => {
+ const fider = useFider()
const [error, setError] = useState<Failure | undefined>(undefined)
const notifyError = async () => {
@@ -88,6 +90,9 @@ const DesignSystemPage = () => {
return e.preventEnable()
}
+ const threeHoursAgo = new Date()
+ threeHoursAgo.setHours(threeHoursAgo.getHours() - 3)
+
const forceError = async () => {
setError({
errors: [
@@ -366,13 +371,13 @@ const DesignSystemPage = () => {
<VStack>
<div>
- Relative: <Moment date="2017-06-03T16:55:06.815042Z" format="relative" />
+ Relative: <Moment locale={fider.currentLocale} date={threeHoursAgo} format="relative" />
</div>
<div>
- Short: <Moment date="2017-06-03T16:55:06.815042Z" format="short" />
+ Short: <Moment locale={fider.currentLocale} date="2017-06-03T16:55:06.815042Z" format="short" />
</div>
<div>
- Full: <Moment date="2017-06-03T16:55:06.815042Z" format="full" />
+ Full: <Moment locale={fider.currentLocale} date="2017-06-03T16:55:06.815042Z" format="full" />
</div>
</VStack>
diff --git a/public/pages/MyNotifications/MyNotifications.page.tsx b/public/pages/MyNotifications/MyNotifications.page.tsx
index c17ce1cd5..d8ba46cdd 100644
--- a/public/pages/MyNotifications/MyNotifications.page.tsx
+++ b/public/pages/MyNotifications/MyNotifications.page.tsx
@@ -2,7 +2,7 @@ import React from "react"
import { Notification } from "@fider/models"
import { Markdown, Moment, PageTitle } from "@fider/components"
-import { actions } from "@fider/services"
+import { actions, Fider } from "@fider/services"
import { HStack, VStack } from "@fider/components/layout"
interface MyNotificationsPageProps {
@@ -40,7 +40,7 @@ export default class MyNotificationsPage extends React.Component<MyNotifications
<Markdown text={n.title} style="full" />
</a>
<span className="text-muted">
- <Moment date={n.createdAt} />
+ <Moment locale={Fider.currentLocale} date={n.createdAt} />
</span>
</div>
)
diff --git a/public/pages/ShowPost/ShowPost.page.tsx b/public/pages/ShowPost/ShowPost.page.tsx
index f90d30744..d682371e9 100644
--- a/public/pages/ShowPost/ShowPost.page.tsx
+++ b/public/pages/ShowPost/ShowPost.page.tsx
@@ -119,7 +119,7 @@ export default class ShowPostPage extends React.Component<ShowPostPageProps, Sho
)}
<span className="text-muted">
- Posted by <UserName user={this.props.post.user} /> &middot; <Moment date={this.props.post.createdAt} />
+ Posted by <UserName user={this.props.post.user} /> &middot; <Moment locale={Fider.currentLocale} date={this.props.post.createdAt} />
</span>
</div>
</HStack>
diff --git a/public/pages/ShowPost/components/ShowComment.tsx b/public/pages/ShowPost/components/ShowComment.tsx
index 53c8ccab5..7e3dece0d 100644
--- a/public/pages/ShowPost/components/ShowComment.tsx
+++ b/public/pages/ShowPost/components/ShowComment.tsx
@@ -89,7 +89,7 @@ export const ShowComment = (props: ShowCommentProps) => {
const comment = props.comment
const editedMetadata = !!comment.editedAt && !!comment.editedBy && (
- <span data-tooltip={`This comment has been edited by ${comment.editedBy.name} on ${formatDate(comment.editedAt)}`}>· edited</span>
+ <span data-tooltip={`This comment has been edited by ${comment.editedBy.name} on ${formatDate(fider.currentLocale, comment.editedAt)}`}>· edited</span>
)
return (
@@ -104,7 +104,7 @@ export const ShowComment = (props: ShowCommentProps) => {
<HStack>
<UserName user={comment.user} />{" "}
<div className="text-xs">
- · <Moment date={comment.createdAt} /> {editedMetadata}
+ · <Moment locale={fider.currentLocale} date={comment.createdAt} /> {editedMetadata}
</div>
</HStack>
{!isEditing && canEditComment() && (
diff --git a/public/pages/ShowPost/components/VotesModal.tsx b/public/pages/ShowPost/components/VotesModal.tsx
index bac6cd23a..6cf802035 100644
--- a/public/pages/ShowPost/components/VotesModal.tsx
+++ b/public/pages/ShowPost/components/VotesModal.tsx
@@ -1,7 +1,7 @@
import React from "react"
import { Post, Vote } from "@fider/models"
import { Modal, Button, Loader, Avatar, UserName, Moment, Input } from "@fider/components"
-import { actions } from "@fider/services"
+import { actions, Fider } from "@fider/services"
import IconSearch from "@fider/assets/images/heroicons-search.svg"
import IconX from "@fider/assets/images/heroicons-x.svg"
import { HStack, VStack } from "@fider/components/layout"
@@ -87,7 +87,7 @@ export class VotesModal extends React.Component<VotesModalProps, VotesModalState
</VStack>
</HStack>
<span className="text-muted">
- <Moment date={x.createdAt} />
+ <Moment locale={Fider.currentLocale} date={x.createdAt} />
</span>
</HStack>
))}
diff --git a/public/services/utils.spec.ts b/public/services/utils.spec.ts
index edf4058d6..d9dd80b4b 100644
--- a/public/services/utils.spec.ts
+++ b/public/services/utils.spec.ts
@@ -25,57 +25,54 @@ import { readFileSync } from "fs"
})
})
;[
- { input: new Date(2018, 4, 27, 10, 12, 59), expected: "May 27, 2018 · 10:12" },
- { input: new Date(2058, 12, 12, 23, 21, 53), expected: "January 12, 2059 · 23:21" },
- { input: "2018-04-11T18:13:33.128082", expected: "April 11, 2018 · 18:13" },
- { input: "2017-11-20T07:47:42.158142", expected: "November 20, 2017 · 07:47" },
+ { input: new Date(2018, 4, 27, 10, 12, 59), expectedEn: "May 27, 2018 at 10:12 AM", expectedPtBr: "27 de maio de 2018 10:12" },
+ { input: new Date(2058, 12, 12, 23, 21, 53), expectedEn: "January 12, 2059 at 11:21 PM", expectedPtBr: "12 de janeiro de 2059 23:21" },
+ { input: "2018-04-11T18:13:33.128082", expectedEn: "April 11, 2018 at 6:13 PM", expectedPtBr: "11 de abril de 2018 18:13" },
+ { input: "2017-11-20T07:47:42.158142", expectedEn: "November 20, 2017 at 7:47 AM", expectedPtBr: "20 de novembro de 2017 07:47" },
].forEach((x) => {
- test(`formatDate (full) of ${x.input} should be ${x.expected}`, () => {
- const result = formatDate(x.input, "full")
- expect(result).toEqual(x.expected)
+ test(`[English] formatDate (full) of ${x.input} should be ${x.expectedEn}`, () => {
+ const result = formatDate("en", x.input, "full")
+ expect(result).toEqual(x.expectedEn)
+ })
+ test(`[Brazilian Portuguese] formatDate (full) of ${x.input} should be ${x.expectedPtBr}`, () => {
+ const result = formatDate("pt-BR", x.input, "full")
+ expect(result).toEqual(x.expectedPtBr)
})
})
;[
- { input: new Date(2018, 4, 27, 10, 12, 59), expected: "May '18" },
- { input: new Date(2058, 12, 12, 23, 21, 53), expected: "Jan '59" },
- { input: "2018-04-11T18:13:33.128082", expected: "Apr '18" },
- { input: "2017-11-20T07:47:42.158142", expected: "Nov '17" },
+ { input: new Date(2018, 4, 27, 10, 12, 59), expectedEn: "May 18", expectedPtBr: "mai. de 18" },
+ { input: new Date(2058, 12, 12, 23, 21, 53), expectedEn: "Jan 59", expectedPtBr: "jan. de 59" },
+ { input: "2018-04-11T18:13:33.128082", expectedEn: "Apr 18", expectedPtBr: "abr. de 18" },
+ { input: "2017-11-20T07:47:42.158142", expectedEn: "Nov 17", expectedPtBr: "nov. de 17" },
].forEach((x) => {
- test(`formatDate (short) of ${x.input} should be ${x.expected}`, () => {
- const result = formatDate(x.input, "short")
- expect(result).toEqual(x.expected)
+ test(`[English] formatDate (short) of ${x.input} should be ${x.expectedEn}`, () => {
+ const result = formatDate("en", x.input, "short")
+ expect(result).toEqual(x.expectedEn)
+ })
+ test(`[Brazilian Portuguese] formatDate (short) of ${x.input} should be ${x.expectedPtBr}`, () => {
+ const result = formatDate("pt-BR", x.input, "short")
+ expect(result).toEqual(x.expectedPtBr)
})
})
;[
- { input: new Date(2018, 4, 27, 19, 51, 9), expected: "less than a minute ago" },
- { input: new Date(2018, 4, 27, 10, 12, 59), expected: "about 10 hours ago" },
- { input: new Date(2018, 4, 26, 10, 12, 59), expected: "a day ago" },
- { input: new Date(2018, 4, 22, 10, 12, 59), expected: "5 days ago" },
- { input: new Date(2018, 3, 22, 10, 12, 59), expected: "about a month ago" },
- { input: new Date(2018, 2, 22, 10, 12, 59), expected: "2 months ago" },
- { input: new Date(2017, 3, 22, 10, 12, 59), expected: "about a year ago" },
- { input: new Date(2013, 3, 22, 10, 12, 59), expected: "5 years ago" },
+ { input: new Date(2018, 4, 27, 19, 51, 9), expectedEn: "1 second ago", expectedPtBr: "há 1 segundo" },
+ { input: new Date(2018, 4, 27, 10, 12, 59), expectedEn: "10 hours ago", expectedPtBr: "há 10 horas" },
+ { input: new Date(2018, 4, 26, 10, 12, 59), expectedEn: "yesterday", expectedPtBr: "ontem" },
+ { input: new Date(2018, 4, 22, 10, 12, 59), expectedEn: "5 days ago", expectedPtBr: "há 5 dias" },
+ { input: new Date(2018, 3, 22, 10, 12, 59), expectedEn: "last month", expectedPtBr: "mês passado" },
+ { input: new Date(2018, 2, 22, 10, 12, 59), expectedEn: "2 months ago", expectedPtBr: "há 2 meses" },
+ { input: new Date(2017, 3, 22, 10, 12, 59), expectedEn: "last year", expectedPtBr: "ano passado" },
+ { input: new Date(2013, 3, 22, 10, 12, 59), expectedEn: "5 years ago", expectedPtBr: "há 5 anos" },
].forEach((x) => {
- test(`timeSince ${x.input} should be ${x.expected}`, () => {
+ test(`[English] timeSince ${x.input} should be ${x.expectedEn}`, () => {
const now = new Date(2018, 4, 27, 19, 51, 10)
- const result = timeSince(now, x.input)
- expect(result).toEqual(x.expected)
+ const result = timeSince("en", now, x.input)
+ expect(result).toEqual(x.expectedEn)
})
-})
-;[
- { input: new Date(2018, 4, 27, 19, 51, 9), expected: "less than a minute ago" },
- { input: new Date(2018, 4, 27, 10, 12, 59), expected: "about 10 hours ago" },
- { input: new Date(2018, 4, 26, 10, 12, 59), expected: "a day ago" },
- { input: new Date(2018, 4, 22, 10, 12, 59), expected: "5 days ago" },
- { input: new Date(2018, 3, 22, 10, 12, 59), expected: "about a month ago" },
- { input: new Date(2018, 2, 22, 10, 12, 59), expected: "2 months ago" },
- { input: new Date(2017, 3, 22, 10, 12, 59), expected: "about a year ago" },
- { input: new Date(2013, 3, 22, 10, 12, 59), expected: "5 years ago" },
-].forEach((x) => {
- test(`timeSince ${x.input} should be ${x.expected}`, () => {
+ test(`[Brazilian Portuguese] timeSince ${x.input} should be ${x.expectedPtBr}`, () => {
const now = new Date(2018, 4, 27, 19, 51, 10)
- const result = timeSince(now, x.input)
- expect(result).toEqual(x.expected)
+ const result = timeSince("pt-BR", now, x.input)
+ expect(result).toEqual(x.expectedPtBr)
})
})
diff --git a/public/services/utils.ts b/public/services/utils.ts
index 07f5d2174..062ef7aa5 100644
--- a/public/services/utils.ts
+++ b/public/services/utils.ts
@@ -17,79 +17,42 @@ export const classSet = (input?: any): string => {
return ""
}
-const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
-
-export const currencySymbol = (currencyCode: string): string => {
- currencyCode = currencyCode ? currencyCode.toLowerCase() : ""
- switch (currencyCode) {
- case "eur":
- return "€"
- case "usd":
- return "$"
- }
- throw new Error(`Invalid currency code '${currencyCode}'`)
-}
-
-const twoDigits = (value: number): string => {
- return value <= 9 ? `0${value}` : value.toString()
-}
-
type DateFormat = "full" | "short"
+const shortOpts: Intl.DateTimeFormatOptions = { month: "short", year: "2-digit" }
+const fullOpts: Intl.DateTimeFormatOptions = { day: "2-digit", month: "long", year: "numeric", hour: "numeric", minute: "numeric" }
-export const formatDate = (input: Date | string, format: DateFormat = "full"): string => {
+export const formatDate = (locale: string, input: Date | string, format: DateFormat = "full"): string => {
const date = input instanceof Date ? input : new Date(input)
- const monthIndex = date.getMonth()
- const year = date.getFullYear()
-
- if (format === "short") {
- return `${monthNames[monthIndex].substring(0, 3)} '${year.toString().substring(2)}`
+ try {
+ const opts = format === "short" ? shortOpts : fullOpts
+ return new Intl.DateTimeFormat(locale, opts).format(date)
+ } catch {
+ return date.toLocaleString(locale)
}
-
- const day = date.getDate()
- const hours = twoDigits(date.getHours())
- const minutes = twoDigits(date.getMinutes())
- return `${monthNames[monthIndex]} ${day}, ${year} · ${hours}:${minutes}`
}
-const templates: { [key: string]: string } = {
- seconds: "less than a minute",
- minute: "about a minute",
- minutes: "%d minutes",
- hour: "about an hour",
- hours: "about %d hours",
- day: "a day",
- days: "%d days",
- month: "about a month",
- months: "%d months",
- year: "about a year",
- years: "%d years",
-}
-
-const template = (t: string, n: number): string => {
- return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n)).toString())
-}
-
-export const timeSince = (now: Date, date: Date): string => {
- const seconds = (now.getTime() - date.getTime()) / 1000
- const minutes = seconds / 60
- const hours = minutes / 60
- const days = hours / 24
- const years = days / 365
-
- return (
- ((seconds < 45 && template("seconds", seconds)) ||
- (seconds < 90 && template("minute", 1)) ||
- (minutes < 45 && template("minutes", minutes)) ||
- (minutes < 90 && template("hour", 1)) ||
- (hours < 24 && template("hours", hours)) ||
- (hours < 42 && template("day", 1)) ||
- (days < 30 && template("days", days)) ||
- (days < 45 && template("month", 1)) ||
- (days < 365 && template("months", days / 30)) ||
- (years < 1.5 && template("year", 1)) ||
- template("years", years)) + " ago"
- )
+export const timeSince = (locale: string, now: Date, date: Date): string => {
+ try {
+ const seconds = (now.getTime() - date.getTime()) / 1000
+ const minutes = Math.round(seconds / 60)
+ const hours = Math.round(minutes / 60)
+ const days = Math.round(hours / 24)
+ const months = Math.round(days / 30)
+ const years = Math.round(days / 365)
+
+ const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" })
+ return (
+ (seconds < 60 && rtf.format(-1 * seconds, "seconds")) ||
+ (minutes < 60 && rtf.format(-1 * minutes, "minutes")) ||
+ (hours < 24 && rtf.format(-1 * hours, "hours")) ||
+ (days < 30 && rtf.format(-1 * days, "days")) ||
+ (days < 365 && rtf.format(-1 * months, "months")) ||
+ rtf.format(-1 * years, "years")
+ )
+ } catch {
+ return formatDate(locale, date, "short")
+ }
}
export const fileToBase64 = async (file: File): Promise<string> => {
diff --git a/tsconfig.json b/tsconfig.json
index 0134b70e0..6ed7e4ff5 100755
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -8,7 +8,7 @@
"target": "es6",
"strict": true,
"jsx": "react",
- "lib": ["dom", "es2015.promise", "es6"],
+ "lib": ["dom", "es2015.promise", "es6", "ES2020.Intl"],
"baseUrl": ".",
"noEmit": true,
"noUnusedLocals": true,