MCPcopy
hub / github.com/umami-software/umami / useLocale

Function useLocale

src/components/hooks/useLocale.ts:16–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14const selector = (state: { locale: string }) => state.locale;
15
16export function useLocale() {
17 const locale = useApp(selector);
18 const forceUpdate = useForceUpdate();
19 const dir = getTextDirection(locale);
20 const dateLocale = getDateLocale(locale);
21
22 async function loadMessages(locale: string) {
23 const { data } = await httpGet(`${process.env.basePath || ''}/intl/messages/${locale}.json`);
24
25 messages[locale] = data;
26 }
27
28 async function saveLocale(value: string) {
29 if (!messages[value]) {
30 await loadMessages(value);
31 }
32
33 setItem(LOCALE_CONFIG, value);
34
35 document.getElementById('__next')?.setAttribute('dir', getTextDirection(value));
36
37 if (locale !== value) {
38 setLocale(value);
39 } else {
40 forceUpdate();
41 }
42 }
43
44 useEffect(() => {
45 if (!messages[locale]) {
46 saveLocale(locale);
47 }
48 }, [locale]);
49
50 useEffect(() => {
51 const url = new URL(window?.location?.href);
52 const locale = url.searchParams.get('locale');
53
54 if (locale) {
55 saveLocale(locale);
56 }
57 }, []);
58
59 return { locale, saveLocale, messages, dir, dateLocale };
60}

Callers 15

WorldMapFunction · 0.90
PageviewsChartFunction · 0.90
MetricLabelFunction · 0.90
EventsChartFunction · 0.90
WeeklyTrafficFunction · 0.90
PropertyChartFunction · 0.90
PropertyNumericChartFunction · 0.90
PropertyDateChartFunction · 0.90
DateDisplayFunction · 0.90
LinkButtonFunction · 0.90
DateDistanceFunction · 0.90
BarChartComponentFunction · 0.90

Calls 5

useForceUpdateFunction · 0.90
getTextDirectionFunction · 0.90
getDateLocaleFunction · 0.90
saveLocaleFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected