From c8676422351fe9fd615394492184b56b72794e66 Mon Sep 17 00:00:00 2001 From: folland87 Date: Tue, 26 Mar 2024 16:40:54 +0100 Subject: [PATCH] add translations for share component --- client/locales.md | 2 +- client/src/components/share/index.tsx | 267 +++++++++++--------- client/src/components/share/locales/en.json | 10 + client/src/components/share/locales/fr.json | 10 + 4 files changed, 164 insertions(+), 125 deletions(-) create mode 100644 client/src/components/share/locales/en.json diff --git a/client/locales.md b/client/locales.md index a287b058..1124ae9a 100644 --- a/client/locales.md +++ b/client/locales.md @@ -25,4 +25,4 @@ | /components/truncate | OK | FRED | FR,EN,ES,DE | | /components/websites | OK | FRED | FR,EN | | /components/wiki | OK | FRED | FR,EN | -| /components/share | TODO | FRED | FR,EN | +| /components/share | OK | FRED | FR,EN | diff --git a/client/src/components/share/index.tsx b/client/src/components/share/index.tsx index f102995e..ff0a8bc3 100644 --- a/client/src/components/share/index.tsx +++ b/client/src/components/share/index.tsx @@ -1,46 +1,21 @@ -import { Link } from "@dataesr/dsfr-plus"; +import { Link, useDSFRConfig } from "@dataesr/dsfr-plus"; import useConsent from "../../hooks/useConsent"; import useCopyToClipboard from "../../hooks/useCopyToClipboard"; +import { RawIntlProvider, createIntl } from "react-intl"; -const onClickFacebook = (e: { preventDefault: () => void }) => { - e.preventDefault(); - window.open( - "https://www.facebook.com/sharer.php", - "Partager sur Facebook", - "toolbar=no,location=yes,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=450" - ); -}; - -const onClickTwitter = (e: { preventDefault: () => void }) => { - e.preventDefault(); - const currentURL = window.location.href; - const text = "#dataEsr #scanR"; - const twitterShareURL = `https://twitter.com/intent/tweet?url=${encodeURIComponent( - currentURL - )}&text=${encodeURIComponent(text)}`; - - window.open( - twitterShareURL, - "Partager sur Twitter", - "toolbar=no,location=yes,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=420" - ); -}; +const modules = import.meta.glob('./locales/*.json', { eager: true, import: 'default' }) +const messages = Object.keys(modules).reduce((acc, key) => { + const locale = key.match(/\.\/locales\/(.+)\.json$/)?.[1]; + if (locale) { + return { ...acc, [locale]: modules[key] } + } + return acc; +}, {}); -const onClickLinkedin = (e) => { - e.preventDefault(); - const currentURL = encodeURIComponent("www.google.fr"); - const text = "#dataEsr #scanR"; - const linkedinShareURL = `https://www.linkedin.com/sharing/share-offsite/?url=${currentURL}&text=${encodeURIComponent( - text - )}`; - window.open( - linkedinShareURL, - "Partager sur LinkedIn", - "toolbar=no,location=yes,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=420" - ); -}; export default function Share() { + const { locale } = useDSFRConfig(); + const intl = createIntl({ locale, messages: messages[locale] }); const url = window.location.href; const { consent, dialogId } = useConsent(); const { copy } = useCopyToClipboard(); @@ -50,93 +25,137 @@ export default function Share() { url )}`; + const formating = { + authorize: (chunks: any) => ( + + {chunks} + + ), + }; + + const onClickFacebook = (e: { preventDefault: () => void }) => { + e.preventDefault(); + window.open( + "https://www.facebook.com/sharer.php", + intl.formatMessage({ id: "share.facebook" }), + "toolbar=no,location=yes,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=450" + ); + }; + + const onClickTwitter = (e: { preventDefault: () => void }) => { + e.preventDefault(); + const currentURL = window.location.href; + const text = "#dataEsr #scanR"; + const twitterShareURL = `https://twitter.com/intent/tweet?url=${encodeURIComponent( + currentURL + )}&text=${encodeURIComponent(text)}`; + + window.open( + twitterShareURL, + intl.formatMessage({ id: "share.twitter" }), + "toolbar=no,location=yes,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=420" + ); + }; + + const onClickLinkedin = (e) => { + e.preventDefault(); + const currentURL = encodeURIComponent("www.google.fr"); + const text = "#dataEsr #scanR"; + const linkedinShareURL = `https://www.linkedin.com/sharing/share-offsite/?url=${currentURL}&text=${encodeURIComponent( + text + )}`; + window.open( + linkedinShareURL, + intl.formatMessage({ id: "share.linkedin" }), + "toolbar=no,location=yes,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=420" + ); + }; + return ( -
- {(!twitter || !facebook || !linkedin) && ( -

- Veuillez{" "} - - autoriser le dépot de cookies - {" "} - pour partager sur Facebook, Twitter et LinkedIn. -

- )} - -
+ +
+ {(!twitter || !facebook || !linkedin) && ( +

+ {intl.formatMessage({ id: "share.cookies.personalize" }, formating)} +

+ )} + +
+
); } diff --git a/client/src/components/share/locales/en.json b/client/src/components/share/locales/en.json new file mode 100644 index 00000000..32d3ebb2 --- /dev/null +++ b/client/src/components/share/locales/en.json @@ -0,0 +1,10 @@ +{ + "share.facebook": "Share on Facebook", + "share.twitter": "Share on Twitter", + "share.linkedin": "Share on LinkedIn", + "share.email": "Share via email", + "share.copy": "Copy to clipboard", + "share.inactive": "- inactive", + "share.cookies": "Customize cookies", + "share.cookies.personalize": "Please authorize the use of cookies to share on Facebook, Twitter, and LinkedIn." +} \ No newline at end of file diff --git a/client/src/components/share/locales/fr.json b/client/src/components/share/locales/fr.json index e69de29b..49bcb74c 100644 --- a/client/src/components/share/locales/fr.json +++ b/client/src/components/share/locales/fr.json @@ -0,0 +1,10 @@ +{ + "share.facebook": "Partager sur Facebook", + "share.twitter": "Partager sur Twitter", + "share.linkedin": "Partager sur LinkedIn", + "share.email": "Partager par email", + "share.copy": "Copier dans le presse-papier", + "share.inactive": "- désactivé", + "share.cookies": "Personnaliser les cookies", + "share.cookies.personalize": "Veuillez autoriser le dépot de cookies pour partager sur Facebook, Twitter et LinkedIn." +} \ No newline at end of file