From ae7ba54723fd28a758cd3c403f0cef293695a9ec Mon Sep 17 00:00:00 2001 From: Przemek Wiech Date: Sat, 9 Mar 2019 16:07:52 +0100 Subject: [PATCH] Translate dates and linkify texts in the details panel --- package.json | 1 + src/details.tsx | 33 ++++++++++++++++++++++++++------- src/react-linkify.d.ts | 3 +++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 src/react-linkify.d.ts diff --git a/package.json b/package.json index 655baf6..35d6c13 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "react": "latest", "react-dom": "latest", "react-intl": "^2.8.0", + "react-linkify": "^0.2.2", "react-router-dom": "^4.3.1", "semantic-ui-css": "^2.4.1", "semantic-ui-react": "^0.84.0", diff --git a/src/details.tsx b/src/details.tsx index d31ba2b..861a632 100644 --- a/src/details.tsx +++ b/src/details.tsx @@ -1,8 +1,11 @@ import * as React from 'react'; +import flatMap from 'array.prototype.flatmap'; +import Linkify from 'react-linkify'; +import {formatDate, getDate} from 'topola'; +import {FormattedMessage} from 'react-intl'; import {GedcomData} from './gedcom_util'; import {GedcomEntry} from 'parse-gedcom'; -import {FormattedMessage} from 'react-intl'; -import flatMap from 'array.prototype.flatmap'; +import {intlShape} from 'react-intl'; interface Props { gedcom: GedcomData; @@ -33,11 +36,20 @@ function translateTag(tag: string) { ); } -function eventDetails(entry: GedcomEntry, tag: string) { +function translateDate(gedcomDate: string, locale: string) { + const dateOrRange = getDate(gedcomDate); + const date = dateOrRange && dateOrRange.date; + if (!date) { + return gedcomDate; + } + return formatDate(date, locale); +} + +function eventDetails(entry: GedcomEntry, tag: string, locale: string) { const lines = []; const date = entry.tree.find((subentry) => subentry.tag === 'DATE'); if (date && date.data) { - lines.push(date.data); + lines.push(translateDate(date.data, locale)); } const place = entry.tree.find((subentry) => subentry.tag === 'PLAC'); if (place && place.data) { @@ -57,7 +69,7 @@ function eventDetails(entry: GedcomEntry, tag: string) { {lines.map((line) => ( <> - {line} + {line}
))} @@ -85,7 +97,7 @@ function dataDetails(entry: GedcomEntry, tag: string) { {lines.map((line) => ( <> - {line} + {line}
))} @@ -125,13 +137,20 @@ function getDetails( } export class Details extends React.Component { + /** Make intl appear in this.context. */ + static contextTypes = { + intl: intlShape, + }; + render() { const entries = this.props.gedcom.indis[this.props.indi].tree; return (
{getDetails(entries, NAME_TAGS, nameDetails)} - {getDetails(entries, EVENT_TAGS, eventDetails)} + {getDetails(entries, EVENT_TAGS, (entry, tag) => + eventDetails(entry, tag, this.context.intl.locale), + )} {getDetails(entries, DATA_TAGS, dataDetails)}
); diff --git a/src/react-linkify.d.ts b/src/react-linkify.d.ts new file mode 100644 index 0000000..fe564ef --- /dev/null +++ b/src/react-linkify.d.ts @@ -0,0 +1,3 @@ +declare module 'react-linkify' { + export default class Linkify extends React.Component {} +}