2 changed files with 14 additions and 6 deletions
@ -1,15 +1,22 @@ |
|||||||
|
import { useCallback, useMemo } from "react"; |
||||||
import { useTranslation } from "react-i18next"; |
import { useTranslation } from "react-i18next"; |
||||||
|
|
||||||
export function useRandomTranslation() { |
export function useRandomTranslation() { |
||||||
const { t } = useTranslation(); |
const { t } = useTranslation(); |
||||||
|
const seed = useMemo(() => Math.random(), []); |
||||||
|
|
||||||
const getRandomTranslation = (key: string) => { |
const getRandomTranslation = useCallback( |
||||||
const res = t(key, { returnObjects: true }); |
(key: string) => { |
||||||
|
const res = t(key, { returnObjects: true }); |
||||||
|
|
||||||
if (Array.isArray(res)) return res[Math.floor(Math.random() * res.length)]; |
if (Array.isArray(res)) { |
||||||
|
return res[Math.floor(seed * res.length)]; |
||||||
|
} |
||||||
|
|
||||||
return res; |
return res; |
||||||
}; |
}, |
||||||
|
[t, seed] |
||||||
|
); |
||||||
|
|
||||||
return { t: getRandomTranslation }; |
return { t: getRandomTranslation }; |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue