Skip to content

Commit

Permalink
chore: migrate to getTranslations in async pages and layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbrusegard committed Oct 27, 2024
1 parent 34ef7b8 commit 972141f
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 26 deletions.
5 changes: 2 additions & 3 deletions src/app/[locale]/(default)/news/(main)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SquarePenIcon } from 'lucide-react';
import { useTranslations } from 'next-intl';
import { setRequestLocale } from 'next-intl/server';
import { getTranslations, setRequestLocale } from 'next-intl/server';

import { Link } from '@/lib/locale/navigation';

Expand All @@ -18,7 +17,7 @@ export default async function NewsHeaderLayout({
const { locale } = await params;

setRequestLocale(locale);
const t = useTranslations('news');
const t = await getTranslations('news');
return (
<>
<div className='flex items-center justify-between'>
Expand Down
3 changes: 1 addition & 2 deletions src/app/[locale]/(default)/news/(main)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { articleMockData as articleData } from '@/mock-data/article';
import { useTranslations } from 'next-intl';
import { getTranslations, setRequestLocale } from 'next-intl/server';
import {
type SearchParams,
Expand Down Expand Up @@ -37,7 +36,7 @@ export default async function NewsPage({
}) {
const { locale } = await params;
setRequestLocale(locale);
const t = useTranslations('ui');
const t = await getTranslations('ui');
const searchParamsCache = createSearchParamsCache({
[t('page')]: parseAsInteger.withDefault(1),
});
Expand Down
5 changes: 2 additions & 3 deletions src/app/[locale]/(default)/news/[article]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {
articleMockData as articleData,
authorMockData as authorData,
} from '@/mock-data/article';
import { useTranslations } from 'next-intl';
import { setRequestLocale } from 'next-intl/server';
import { getTranslations, setRequestLocale } from 'next-intl/server';
import Image from 'next/image';
import { notFound } from 'next/navigation';
import readingTime from 'reading-time';
Expand Down Expand Up @@ -37,7 +36,7 @@ export default async function ArticlePage({
}) {
const { locale, article } = await params;
setRequestLocale(locale);
const t = useTranslations('news');
const t = await getTranslations('news');

const data = articleData.find((a) => a.id === Number(article));
if (!data) {
Expand Down
7 changes: 3 additions & 4 deletions src/app/[locale]/(default)/storage/(main)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { SearchBar } from '@/components/composites/SearchBar';
import { SortSelector } from '@/components/composites/SortSelector';
import { SelectorsSkeleton } from '@/components/storage/SelectorsSkeleton';
import { ShoppingCartLink } from '@/components/storage/ShoppingCartLink';
import { useTranslations } from 'next-intl';
import { setRequestLocale } from 'next-intl/server';
import { getTranslations, setRequestLocale } from 'next-intl/server';
import { Suspense } from 'react';

type StorageLayoutProps = {
Expand All @@ -19,8 +18,8 @@ export default async function StorageLayout({
const { locale } = await params;

setRequestLocale(locale);
const t = useTranslations('storage');
const tUi = useTranslations('ui');
const t = await getTranslations('storage');
const tUi = await getTranslations('ui');

// This does not make much sense with a backend, most likely the categories in the backend will have a name in both languages and an ID
const categories = [
Expand Down
3 changes: 1 addition & 2 deletions src/app/[locale]/(default)/storage/(main)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { items } from '@/mock-data/items';
import { useTranslations } from 'next-intl';
import { getTranslations, setRequestLocale } from 'next-intl/server';
import {
type SearchParams,
Expand Down Expand Up @@ -34,7 +33,7 @@ export default async function StoragePage({
const { locale } = await params;

setRequestLocale(locale);
const t = useTranslations('ui');
const t = await getTranslations('ui');

const itemsPerPage = 12;

Expand Down
5 changes: 2 additions & 3 deletions src/app/[locale]/(default)/storage/shopping-cart/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Button } from '@/components/ui/Button';
import { Link } from '@/lib/locale/navigation';
import { ArrowLeftIcon } from 'lucide-react';
import { useTranslations } from 'next-intl';
import { setRequestLocale } from 'next-intl/server';
import { getTranslations, setRequestLocale } from 'next-intl/server';

type ShoppingCartLayoutProps = {
children: React.ReactNode;
Expand All @@ -16,7 +15,7 @@ export default async function StorageLayout({
const { locale } = await params;

setRequestLocale(locale);
const t = useTranslations('storage.shoppingCart');
const t = await getTranslations('storage.shoppingCart');
return (
<>
<div className='relative'>
Expand Down
16 changes: 7 additions & 9 deletions src/app/[locale]/(default)/storage/shopping-cart/page.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { BorrowDialog } from '@/components/storage/BorrowDialog';
import { ShoppingCartClearDialog } from '@/components/storage/ShoppingCartClearDialog';
import { ShoppingCartTable } from '@/components/storage/ShoppingCartTable';
import { useTranslations } from 'next-intl';
import { setRequestLocale } from 'next-intl/server';
import { use } from 'react';
import { getTranslations, setRequestLocale } from 'next-intl/server';

export default function StorageShoppingCartPage(props: {
export default async function StorageShoppingCartPage({
params,
}: {
params: Promise<{ locale: string }>;
}) {
const params = use(props.params);

const { locale } = params;
const { locale } = await params;

setRequestLocale(locale);
const t = useTranslations('storage.shoppingCart');
const tLoanForm = useTranslations('storage.loanForm');
const t = await getTranslations('storage.shoppingCart');
const tLoanForm = await getTranslations('storage.loanForm');

const tableMessages = {
tableDescription: t('tableDescription'),
Expand Down

0 comments on commit 972141f

Please sign in to comment.