Skip to content

Commit 886db58

Browse files
committed
refactor(lingui): Prefer using i18n instead of custom hook
1 parent 3fbea37 commit 886db58

9 files changed

Lines changed: 22 additions & 35 deletions

File tree

src/components/ui/calendar/calendar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type Dispatch, type SetStateAction, useState } from "react"
22

33
import { msg } from "@lingui/core/macro"
44

5-
import { useLanguage, useTrans } from "../../../locales"
5+
import { i18n, useLanguage } from "../../../locales"
66
import { hstack, vstack } from "../../../styles/stack"
77
import { cn } from "../../../utils/cn"
88
import { ChevronLeft, ChevronRight } from "../../icons"
@@ -119,8 +119,8 @@ const ViewSwitch = ({ month, min, max, setMonth }: ViewSwitchProps) => {
119119
const prevDisabled = month.valueOf() <= minMonth.valueOf()
120120
const nextDisabled = month.valueOf() >= maxMonth.valueOf()
121121

122-
const nextCaption = useTrans(magnitudeCaptions[magnitude].next)
123-
const prevCaption = useTrans(magnitudeCaptions[magnitude].prev)
122+
const nextCaption = i18n._(magnitudeCaptions[magnitude].next)
123+
const prevCaption = i18n._(magnitudeCaptions[magnitude].prev)
124124

125125
return (
126126
<div className="flex">

src/components/ui/date-input.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import { CalendarDaysIcon } from "../icons"
66
import { Button } from "./button"
77
import { Calendar, type CalendarProps } from "./calendar"
88
import { Popover } from "./popover"
9-
import { useLocale, useTrans } from "../../locales"
9+
import { i18n, useLocale } from "../../locales"
1010
import { type ClassNameProp } from "../../types/base-props"
1111
import { cn } from "../../utils/cn"
1212
import { ErrorBoundary } from "../utility/error-boundary"
1313

1414
const DateValue = ({ date }: { date: Temporal.PlainDate }) => {
15-
const trans = useTrans()
1615
const locale = useLocale()
1716

18-
if (date.equals(Temporal.Now.plainDateISO())) return trans._(msg`Today`)
17+
if (date.equals(Temporal.Now.plainDateISO())) return i18n._(msg`Today`)
1918
return date.toLocaleString(locale, {
2019
year: "numeric",
2120
month: "2-digit",

src/components/ui/dialog/dialog.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type PropsWithChildren, type ReactNode } from "react"
22

33
import { msg } from "@lingui/core/macro"
44

5-
import { useTrans } from "../../../locales"
5+
import { i18n } from "../../../locales"
66
import { hstack, vstack } from "../../../styles/stack"
77
import { surface } from "../../../styles/surface"
88
import { zIndex } from "../../../styles/z-index"
@@ -38,7 +38,6 @@ const DialogActions = ({
3838
confirm,
3939
cancel,
4040
}: Pick<DialogProps, "confirm" | "cancel">) => {
41-
const trans = useTrans()
4241
if (!confirm && !cancel) return null
4342

4443
return (
@@ -50,7 +49,7 @@ const DialogActions = ({
5049
disabled={confirm.disabled}
5150
onClick={confirm.onClick}
5251
>
53-
{confirm.caption ?? trans._(msg`Confirm`)}
52+
{confirm.caption ?? i18n._(msg`Confirm`)}
5453
</Button>
5554
</DialogPrimitive.Close>
5655
)}
@@ -61,7 +60,7 @@ const DialogActions = ({
6160
disabled={cancel.disabled}
6261
onClick={cancel.onClick}
6362
>
64-
{cancel.caption ?? trans._(msg`Cancel`)}
63+
{cancel.caption ?? i18n._(msg`Cancel`)}
6564
</Button>
6665
</DialogPrimitive.Close>
6766
)}
@@ -125,7 +124,7 @@ export const Dialog = ({
125124

126125
<DialogPrimitive.Close>
127126
<IconButton
128-
title={useTrans(msg`Close`)}
127+
title={i18n._(msg`Close`)}
129128
className="absolute top-1 right-1"
130129
hideTitle
131130
icon={XIcon}

src/components/ui/file-input/file-input.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useState } from "react"
33
import { msg } from "@lingui/core/macro"
44
import { cva, type VariantProps } from "class-variance-authority"
55

6-
import { useTrans } from "../../../locales"
6+
import { i18n } from "../../../locales"
77
import { alert as alertStyles } from "../../../styles/alert"
88
import { focusOutline } from "../../../styles/focus-outline"
99
import { interactive } from "../../../styles/interactive"
@@ -59,7 +59,6 @@ export const FileInput = ({
5959
...props
6060
}: Omit<FileInputProps, "onDragStart" | "onDragEnd">) => {
6161
const [dragging, setDragging] = useState<DraggingState>("idle")
62-
const trans = useTrans()
6362

6463
return (
6564
<ErrorBoundary>
@@ -79,7 +78,7 @@ export const FileInput = ({
7978
className={cn(fileInput({ dragging, alert: alert?.kind }), className)}
8079
>
8180
<AnimatedUploadIcon status={dragging} />
82-
{label ?? trans._(msg`Drop File`)}
81+
{label ?? i18n._(msg`Drop File`)}
8382
{alert && (
8483
<Icon icon={alertStyles[alert.kind].icon} color={alert.kind} />
8584
)}

src/components/ui/pagination.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { msg } from "@lingui/core/macro"
44

55
import { Button } from "./button"
66
import { Select } from "./select"
7-
import { useTrans } from "../../locales"
7+
import { i18n } from "../../locales"
88
import { hstack } from "../../styles/stack"
99
import { type ClassNameProp } from "../../types/base-props"
1010
import { clamp } from "../../utils/clamp"
@@ -23,7 +23,7 @@ const PageSizeSelect = ({
2323
}: PageSizeSelectProps) => (
2424
<Select.Root
2525
value={String(value)}
26-
placeholder={useTrans(msg`Size`)}
26+
placeholder={i18n._(msg`Size`)}
2727
onChange={value => onPageSizeChange?.(Number(value))}
2828
>
2929
{pageSizes.map(size => (
@@ -113,7 +113,6 @@ export const Pagination = ({
113113
onPageSizeChange,
114114
className,
115115
}: PaginationProps) => {
116-
const trans = useTrans()
117116
const numberOfPages = getNumberOfPages(items, pageSize)
118117

119118
return (
@@ -128,7 +127,7 @@ export const Pagination = ({
128127
{pageSizeOptions && (
129128
<>
130129
<div className="flex-1" />
131-
<span className="mr-2">{trans._(msg`Page size:`)}</span>
130+
<span className="mr-2">{i18n._(msg`Page size:`)}</span>
132131
<PageSizeSelect
133132
pageSizes={pageSizeOptions}
134133
value={pageSize}

src/components/ui/text-input.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { SearchIcon, XIcon } from "../icons"
1515
import { InputAlertIcon } from "./fragments/input-alert-icon"
1616
import { InputBorder } from "./fragments/input-border"
1717
import { IconButton } from "./icon-button"
18-
import { useTrans } from "../../locales"
18+
import { i18n } from "../../locales"
1919
import { type AlertKind } from "../../types/alert"
2020
import { type ClassNameProp, type DisableProp } from "../../types/base-props"
2121
import { mergeRefs } from "../../utils/merge-refs"
@@ -47,7 +47,6 @@ export const TextInput = ({
4747
className,
4848
...props
4949
}: TextInputProps) => {
50-
const trans = useTrans()
5150
const textRef = useRef<HTMLInputElement>(null)
5251
const isSearch = type === "search"
5352
const isMasked = type === "password" && !!props.value
@@ -78,7 +77,7 @@ export const TextInput = ({
7877
<span className="grid size-10 shrink-0 place-content-center [:not(:hover,:focus-within)>&]:opacity-0">
7978
<IconButton
8079
hideTitle
81-
title={trans._(msg`Clear text field`)}
80+
title={i18n._(msg`Clear text field`)}
8281
icon={XIcon}
8382
size="sm"
8483
onClick={event => {

src/components/ui/toaster/toast.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { keyframes } from "goober"
66
import { XIcon } from "../../icons"
77
import { Icon } from "../icon"
88
import { type ToastDataProps } from "./toaster-data"
9-
import { useTrans } from "../../../locales"
9+
import { i18n } from "../../../locales"
1010
import { alert } from "../../../styles/alert"
1111
import { hstack } from "../../../styles/stack"
1212
import { surface } from "../../../styles/surface"
@@ -123,7 +123,7 @@ export const Toast = ({
123123
</div>
124124
<IconButton
125125
icon={XIcon}
126-
title={useTrans(msg`Close message`)}
126+
title={i18n._(msg`Close message`)}
127127
hideTitle
128128
look="flat"
129129
onClick={() => void exit()}

src/components/utility/error-boundary.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { Component, type ReactNode } from "react"
22

33
import { msg } from "@lingui/core/macro"
44

5-
import { useTrans } from "../../locales"
5+
import { i18n } from "../../locales"
66

77
const DefaultFallback = () => (
88
<div
99
className="grid size-10 place-content-center"
10-
title={useTrans(msg`An unexpected error occurred here`)}
10+
title={i18n._(msg`An unexpected error occurred here`)}
1111
>
1212
💥
1313
</div>

src/locales/l10n-provider.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { type PropsWithChildren, useEffect, useMemo, useState } from "react"
22

3-
import { type I18n, type MessageDescriptor, setupI18n } from "@lingui/core"
3+
import { type I18n, setupI18n } from "@lingui/core"
44

55
import { messages as de } from "./_de.json"
66
import { messages as en } from "./_en.json"
77
import { createContext } from "../utils/create-context"
88

99
const defaultLanguage = "en"
1010
const catalog = { en, de }
11-
const i18n = setupI18n({
11+
export const i18n = setupI18n({
1212
locales: Object.keys(catalog),
1313
locale: defaultLanguage,
1414
messages: catalog,
@@ -51,11 +51,3 @@ const useL10n = () => Context.useRequiredValue()
5151

5252
export const useLocale = () => useL10n().locale
5353
export const useLanguage = () => useL10n().language
54-
55-
export function useTrans(): typeof i18n
56-
export function useTrans(msg: MessageDescriptor): string
57-
export function useTrans(msg?: MessageDescriptor) {
58-
const { i18n } = useL10n()
59-
if (msg) return i18n._(msg)
60-
return i18n
61-
}

0 commit comments

Comments
 (0)