pstream is dead; long live pstream
taciturnaxolotl.github.io/pstream-ng/
1import { useCallback, useMemo } from "react";
2import { useTranslation } from "react-i18next";
3
4// 35% chance of getting a joke title (Cooper done changed this code!)
5const shouldGiveJokeTitle = () => Math.random() < 0.35;
6
7export function useRandomTranslation() {
8 const { t } = useTranslation();
9 const shouldJoke = useMemo(() => shouldGiveJokeTitle(), []);
10 const seed = useMemo(() => Math.random(), []);
11
12 const getRandomTranslation = useCallback(
13 (key: string): string => {
14 const defaultTitle = t(`${key}.default`) ?? "";
15 if (!shouldJoke) return defaultTitle;
16
17 const keys = t(`${key}.extra`, {
18 returnObjects: true,
19 defaultValue: defaultTitle,
20 });
21 if (Array.isArray(keys)) {
22 if (keys.length === 0) return defaultTitle;
23 return keys[Math.floor(seed * keys.length)];
24 }
25
26 return typeof keys === "string" ? keys : defaultTitle;
27 },
28 [t, seed, shouldJoke],
29 );
30
31 return { t: getRandomTranslation };
32}