📈 Gestion de la traduction dans le composant Meta.
- Utilisation de react-i18next.useTranslation() - Ajout de props page - Si les valeurs appelées avec useTranslation n'existent pas elles sont remplacées avec les valeurs par défaut transmises en props (require: title, description)pull/1795/head
parent
bcefb87927
commit
82d5c46918
|
@ -1,7 +1,9 @@
|
|||
import { Helmet } from 'react-helmet'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useLocation } from 'react-router'
|
||||
|
||||
type PropType = {
|
||||
page: string,
|
||||
title: string
|
||||
description: string
|
||||
ogDescription?: string
|
||||
|
@ -10,6 +12,7 @@ type PropType = {
|
|||
}
|
||||
|
||||
export default function Meta({
|
||||
page,
|
||||
title,
|
||||
description,
|
||||
ogDescription,
|
||||
|
@ -17,20 +20,28 @@ export default function Meta({
|
|||
ogImage,
|
||||
}: PropType) {
|
||||
const { pathname } = useLocation()
|
||||
const { t } = useTranslation()
|
||||
const meta = {
|
||||
title: t(`${page}.titre`, title) || title,
|
||||
description: t(`${page}.description`, description) || description,
|
||||
ogDescription: ogDescription ? t(`${page}.ogDescription`, ogDescription) || ogDescription : description,
|
||||
ogTitle: ogTitle ? t(`${page}.ogTitle`, ogTitle) || ogTitle : title,
|
||||
ogImage: ogImage ? t(`${page}.ogImage`, ogImage) || ogImage : null,
|
||||
}
|
||||
return (
|
||||
<Helmet>
|
||||
<title>{title}</title>
|
||||
<meta name="description" content={description} />
|
||||
<title>{meta.title}</title>
|
||||
<meta name="description" content={meta.description} />
|
||||
<meta property="og:type" content="website" />
|
||||
<meta property="og:title" content={ogTitle ?? title} />
|
||||
<meta property="og:description" content={ogDescription ?? description} />
|
||||
{ogImage && (
|
||||
<meta property="og:title" content={meta.ogTitle ?? meta.title} />
|
||||
<meta property="og:description" content={meta.ogDescription ?? meta.description} />
|
||||
{meta.ogImage && (
|
||||
<meta
|
||||
property="og:image"
|
||||
content={
|
||||
ogImage.startsWith('http')
|
||||
? ogImage
|
||||
: window.location.host + pathname + '/' + ogImage
|
||||
meta.ogImage.startsWith('http')
|
||||
? meta.ogImage
|
||||
: window.location.host + pathname + '/' + meta.ogImage
|
||||
}
|
||||
/>
|
||||
)}
|
||||
|
|
Loading…
Reference in New Issue