Skip to content

Commit

Permalink
Add i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
cesalberca committed Jul 16, 2024
1 parent 3a36314 commit ee9f3df
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 7 deletions.
2 changes: 1 addition & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Messages = typeof import('./src/core/i18n/en.json')
type Messages = typeof import('./src/core/i18n/translations/en.json')
declare interface IntlMessages extends Messages {}

declare module '*.svg' {
Expand Down
6 changes: 5 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import createNextIntlPlugin from 'next-intl/plugin'

const withNextIntl = createNextIntlPlugin('./src/core/i18n/i18n.ts')

/** @type {import('next').NextConfig} */
const nextConfig = {
async redirects() {
Expand All @@ -11,4 +15,4 @@ const nextConfig = {
},
}

export default nextConfig
export default withNextIntl(nextConfig)
6 changes: 6 additions & 0 deletions src/app/[locale]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useTranslations } from 'next-intl'

export default function Index() {
const t = useTranslations('common')
return <h1>{t('coverage')}</h1>
}
10 changes: 5 additions & 5 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const inter = Inter({ subsets: ['latin'] })
export const metadata: Metadata = {
metadataBase: new URL(baseUrl),
title: {
default: 'Next.js Portfolio Starter',
template: '%s | Next.js Portfolio Starter',
default: 'César Alberca',
template: '%s | Blog',
},
description: 'This is my portfolio.',
openGraph: {
title: 'My Portfolio',
description: 'This is my portfolio.',
title: 'My blog',
description: 'César Alberca blog.',
url: baseUrl,
siteName: 'My Portfolio',
siteName: 'cesalberca',
locale: 'en_US',
type: 'website',
},
Expand Down
21 changes: 21 additions & 0 deletions src/app/locale/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NextIntlClientProvider } from 'next-intl'
import { getMessages } from 'next-intl/server'
import type { ReactNode } from 'react'

export default async function LocaleLayout({
children,
params: { locale },
}: {
children: ReactNode
params: { locale: string }
}) {
const messages = await getMessages()

return (
<html lang={locale}>
<body>
<NextIntlClientProvider messages={messages}>{children}</NextIntlClientProvider>
</body>
</html>
)
}
14 changes: 14 additions & 0 deletions src/app/middleware/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import createMiddleware from 'next-intl/middleware'

export default createMiddleware({
// A list of all locales that are supported
locales: ['en', 'de'],

// Used when no locale matches
defaultLocale: 'en',
})

export const config = {
// Match only internationalized pathnames
matcher: ['/', '/(en|es)/:path*'],
}
14 changes: 14 additions & 0 deletions src/core/i18n/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { notFound } from 'next/navigation'
import { getRequestConfig } from 'next-intl/server'

// Can be imported from a shared config
const locales = ['en', 'es']

export default getRequestConfig(async ({ locale }) => {
// Validate that the incoming `locale` parameter is valid
if (!locales.includes(locale as any)) notFound()

return {
messages: (await import(`../translations/${locale}.json`)).default,
}
})
File renamed without changes.
File renamed without changes.

0 comments on commit ee9f3df

Please sign in to comment.