,

Setting up Next i18n with Clerk Authentication in Next.js 13.4 App Router – A Step-by-Step Tutorial

Posted by






Configure Next i18n with Clerk Authentication | Next.js 13.4 App Router Tutorial

Configure Next i18n with Clerk Authentication

Next.js is a popular React framework that offers a great developer experience with features like automatic code splitting, route pre-fetching, and built-in support for server-side rendering. The latest version, Next.js 13.4, introduces even more improvements, including enhanced internationalization support (i18n) and seamless integration with third-party authentication providers like Clerk.

Setting Up Next i18n

To enable internationalization in a Next.js 13.4 app, you can use the built-in i18n support. First, you need to install the `next-translate` package using npm or yarn:


npm install next-translate

Then, you can create a `i18n.js` file in the root of your project and configure the i18n module with your desired settings:


import i18n from 'next-translate'

export default i18n({
locales: ['en', 'fr', 'de'],
defaultLocale: 'en',
pages: {
'*': ['common'],
'/': ['home'],
'/about': ['about']
}
})

Finally, you can use the `useTranslation` hook provided by `next-translate` to access translations within your components and pages:


import { useTranslation } from 'next-translate'

function AboutPage() {
const { t } = useTranslation('about')

return (

{t('title')}

{t('description')}

)
}

Integrating Clerk Authentication

Clerk is a modern authentication and user management platform that offers a seamless way to add user authentication and authorization to your Next.js app. To get started, you need to sign up for a Clerk account and create a new project. Then, you can install the `@clerk/clerk-react` package and configure it with your Clerk project’s settings:


npm install @clerk/clerk-react

Next, you can use the `ClerkProvider` component to wrap your app and provide authentication context to your components:


import { ClerkProvider } from '@clerk/clerk-react'

function MyApp({ Component, pageProps }) {
return (



)
}

export default MyApp

Combining i18n and Clerk Authentication

To combine Next i18n with Clerk authentication, you can simply use the two libraries together in your Next.js app. Since both `next-translate` and `@clerk/clerk-react` provide hooks for accessing their respective functionalities, you can use them in your components and pages seamlessly:


import { useTranslation } from 'next-translate'
import { useUser } from '@clerk/clerk-react'

function ProfilePage() {
const { t } = useTranslation('profile')
const { user } = useUser()

return (

{t('title')}

{t('description')}

Hello, {user.fullName}!

)
}

With this setup, you can easily create a multi-lingual app with support for user authentication and authorization. You can also customize the user interface of the authentication flow using Clerk’s powerful customization options. Now, your Next.js 13.4 app is ready to go global with internationalization and secure user authentication!


0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
berkay yücel
7 months ago

Thanks for work but sign-in page does not working when locale is de

Sayan Mondal
7 months ago

hi Ani, contents are really good but your voice is not clear