Crash Course Into Multilingual Websites With next.JS and Headless Content

Rate this content
Bookmark

On this crash course, we'll create a new project in the headless CMS, fill it with data, and use the content to build a multilingual Next.js app. We'll spend a lot of time in code to:

- Generate strongly typed models and structure for the content

- Set up the site for multiple languages

- Use respective language codes in content fetching and incorporate language fallbacks

- Resolve multilingual content from rich text fields


You will learn:

- How to work with content from headless CMS (Kontent.ai)

- How content model can be leveraged to generate TS types and what benefits it brings to your project

- How to add multilingual capabilities to Next.js website

- How to use language fallbacks

- What are the typical gotchas when working with multilingual content

This workshop has been presented at React Advanced Conference 2022, check out the latest edition of this React Conference.

FAQ

Next.js is utilized for building multilingual websites by handling different language contents efficiently. It supports dynamic routing, which is essential for creating localized versions of web pages, allowing users to switch seamlessly between languages while maintaining the context of the webpage.

A headless CMS, like Content.ai, offers flexibility and scalability in website development by separating the content management from the site's presentation layer. This decoupling allows developers to use any front-end technology and design custom content models that meet their specific needs, making it easier to integrate multilingual support and other dynamic features.

To import content into a Next.js project using Content.ai, first register for a free developer plan or use a shared project ID, then clone the provided GitHub repository. Use the content template manager tool to import the content.zip package by providing your project ID and management API key. This will populate your project with the necessary content types and data.

In a headless CMS like Content.ai, multilingual content management involves creating language-specific versions of content items. Users can configure different languages in the CMS settings and then create or translate content into these languages, ensuring that each language variant is properly linked and accessible through the CMS's API.

In Next.js, 'getStaticProps' is used for fetching data at build time and passing this data as props to the page, optimizing the page for performance by rendering static content. 'getStaticPaths' is used to define dynamic routes and pre-render pages based on data fetched. Both methods are crucial for building efficient, scalable applications with Next.js.

To add new languages to a Next.js project, first configure the languages in the Content.ai CMS and then update the Next.js configuration to include these languages in the 'i18n' block of 'next.config.js'. This setup enables dynamic routing based on locale and allows for the creation of language-specific URLs within the application.

Using TypeScript with Next.js provides benefits such as static type checking, which can catch errors at compile time, improved code quality and maintainability, and enhanced autocompletion and code navigation in supported editors. This combination is powerful for developing large-scale applications with greater efficiency and fewer bugs.

Language switching on a multilingual Next.js site can be handled by using the Next.js router to detect the current locale and display language options accordingly. Links or buttons can be configured to switch locales, dynamically updating the URL and re-rendering the page in the selected language without losing the page context.

Ondrej Polesny
Ondrej Polesny
126 min
14 Oct, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's workshop focused on building multilingual websites with Next.js and headless content. The workshop covered topics such as importing content into CMS projects, generating strongly-typed script models, implementing rich text resolution, adding language support, and implementing a language switch. The speaker provided step-by-step instructions and code examples throughout the workshop. Participants were encouraged to ask questions and were given access to the workshop materials and code repository.

1. Introduction to Multilingual Websites with Next.js

Short description:

Today, I want to talk to you about multilingual websites with Next.js and headless content. Our website at Content.ai is built with Next.js, and I want to share the knowledge I gained from building that site. The workshop is recorded, and you can ask questions if you need clarification. I will provide the code and the final solution for you to explore.

So let's take a look at the presentation. I hope you can see that. So, today, I want to talk to you about multilingual websites with Next.js and headless content. What does mean? First of all, let me introduce myself. I'm Ondrej. I'm a developer evangelist at Content.ai. We're a headless CMS or modular content platform, as we call it these days. And why I picked a topic like that is because our website at Content.ai is built with Next.js. We've been working on it for the past year. Before that, it used to be a Gatsby site for years. We've gained a lot of knowledge during that time. I wanted to take the opportunity and give some of the knowledge I gained from building that site to you, so it can save some time. Because we're always short on time. So that's what I do. Today, apart from showing you how Next.js works, I want to tell you how it handles the multilingual part of the website.

Now, a little bit of a housekeeping as well. The workshop is recorded. So you can always go back and see what I did. The workshop is built in a way that you can do things with me. Because when I show you something, tell you not to do anything when I'm an attendee I always do something. Do anything you want. You can just do it with me. If there are any questions, now the workshop is not hard. It's rather an intro level. So if there are any questions, feel free to drop me a line in chat or drop me a line on Discord. I can always go back and re-explain some topics. It's pretty much the basics, but if you haven't seen Next.js yet, you're maybe working on another platform or another framework, it's probably not going to be that easy for you. So just don't be ashamed to ask with all the new beginners. It's perfectly fine, I'm really happy to go back and re-explain some stuff again.

The code. I will give you the code in form of a GitHub repository, and I will also share the final code with you. So you can take the final solution and go back and see how things were implemented. So no worries there. We're here to have fun and to learn something.

2. Agenda and Important Links

Short description:

Today's agenda includes importing content into CMS projects, generating strongly-typed script models, implementing a heuristic solution on the homepage, creating listing index and detail pages, adding new languages, adjusting data fetching queries, and adding context to the language switch. Important links include the GitHub repository, which contains all workshop data, and the Content.ai registration link. Registering allows access to the application and importing content into the CMS.

So today's agenda, this is the website that we'll be working on today. You see, it's a very simple tail-end template, so I'm always trying to go the easiest route here, and it lists conferences. These are the conferences that we as Content.ai are part of this fall. So we're starting with React Advanced. There are some other conferences you'll see on the website. This is just so we have some kind of content and something to display on the website. This is how it's going to look like, and I think we can start with the agenda and what's going to happen today.

So first of all, we're gonna import content into your CMS projects. Now there are two ways you can go through this workshop. First of them is you register for a free developer plan at content.ai and you can import the content into your own project, or I'll share my project ID with you and you can use mine. Doesn't matter which one, which way you want to go. Both are possible. I'll take you through both options if, regardless of what you choose. Next when we have the content in the project, we'll generate strongly-type script models. So we're going to take the content that is, or the content types that are in the CMS, we're going to generate models into our codebase so we don't have to write them manually and still work with strongly-typed models in TypeScript. Then we're going to implement a heuristic solution on the homepage. First of all, we need to have a site so that we can make it multi-lingual. This is going to be one step towards making the website a bit more dynamic to have the heuristic solution. We're going to implement a listing index and listing detail pages. This one will be for the conference list and for a detail of the conference. We're going to look at get static props, get static paths methods so that you're comfortable using those. Then we're going to add two new languages in both code and the CMS. Now, multi-lingual websites, a lot of developers are scared from them, but it's not only the implementation. A large portion of the multi-lingual websites have been on the server where you manage the content. So, we're going to do both. Then we're going to adjust the data fetching queries that get the data into our project to respect the use local and we're going to add context to our language switch so when you switch between languages, the context of the webpage that you're on stays the same. And that's it, I think. That's the plan for the next two hours. Now, it looks a bit more complicated than it is. I'm sure it's going to be fine. Now, let's start with some important links.

The most important link is the GitHub repository. Let me take the link and I'm going to put it in the chat so you don't have to you know, write it from my screen, but I have to first figure out how to get rid of the zoom panel. This worked, right? So let me just put it there. React advanced workshop 2022. So I put it both in the chat and on Discord. So this is where all the data for the workshop are. It's not just about the code of the website, but there is also a file called links.txt where you'll find all the other links that appear in the presentation. Everything is there. There is also a file called workshop.pdf where this is the presentation that I'm showing you converting to PDF. So if you want to get a page back or page forward, it's perfectly fine to do that in that PDF and there is a content.zip package that will need to import the content into the CMS. So can you someone please confirm that you can access the repository? I made it public, but hopefully GitHub is not. Yeah, you can. Okay, perfect. Alright, so that's the first important link. Another important link is the content of AI registration. I'm gonna put this link in the chat as well. I think just this one is fine. If you're not logged in, it will take you to the registration. Anyway, so let me just put it there. Obviously when I go there, I'm gonna get right into the application because I'm already logged in. Oh, this is not the right project. We'll swap that in a second. But let me open a private window. I think this should take us to the registration. Right? So you just click on join here. And provide your email, password, and create your account. No credit card is needed, just your email address and maybe some additional info. And you should be able to go through that quickly and get right into the app. I think the project was called conference blog. Perfect. So what you see when you register, again, if you don't want to register, it's fine. I'll share my project ID with you. You can work in VS code. We can work with that project I.D. If you want to register, I'll give you a few minutes to do that. When you register and see, you know, get into the app, you're probably going to see an empty screen. I already have some content here, but if you see an empty screen, that's fine. I'm going to show you how to import content into that project in a moment. And of course, while you're registering, you can also clone the repository.

Watch more workshops on topic

Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
Create a Visually Editable Next.js Website Using React Bricks, With Blog and E-commerce
React Summit 2023React Summit 2023
139 min
Create a Visually Editable Next.js Website Using React Bricks, With Blog and E-commerce
Top Content
WorkshopFree
Matteo Frana
Matteo Frana
- React Bricks: why we built it, what it is and how it works- Create a free account- Create a new project with Next.js and Tailwind- Explore the directory structure- Anatomy of a Brick- Create a new Brick (Text-Image)- Add a title and description with RichText visual editing- Add an Image with visual editing- Add Sidebar controls to edit props (padding and image side)- Nesting Bricks using the Repeater component- Create an Image gallery brick- Publish on Netlify or Vercel- Page Types and Custom fields- Access Page meta values- Internationalization- How to reuse content across pages: Stories and Embeds- How to create an E-commerce with Products’ data from an external database and landing pages created visually in React Bricks- Advanced enterprise features: flexible permissions, locked structure, custom visual components
From Todo App to B2B SaaS with Next.js and Clerk
React Summit US 2023React Summit US 2023
153 min
From Todo App to B2B SaaS with Next.js and Clerk
WorkshopFree
Dev Agrawal
Dev Agrawal
If you’re like me, you probably have a million side-project ideas, some that could even make you money as a micro SaaS, or could turn out to be the next billion dollar startup. But how do you know which ones? How do you go from an idea into a functioning product that can be put into the hands of paying customers without quitting your job and sinking all of your time and investment into it? How can your solo side-projects compete with applications built by enormous teams and large enterprise companies?
Building rich SaaS products comes with technical challenges like infrastructure, scaling, availability, security, and complicated subsystems like auth and payments. This is why it’s often the already established tech giants who can reasonably build and operate products like that. However, a new generation of devtools are enabling us developers to easily build complete solutions that take advantage of the best cloud infrastructure available, and offer an experience that allows you to rapidly iterate on your ideas for a low cost of $0. They take all the technical challenges of building and operating software products away from you so that you only have to spend your time building the features that your users want, giving you a reasonable chance to compete against the market by staying incredibly agile and responsive to the needs of users.
In this 3 hour workshop you will start with a simple task management application built with React and Next.js and turn it into a scalable and fully functioning SaaS product by integrating a scalable database (PlanetScale), multi-tenant authentication (Clerk), and subscription based payments (Stripe). You will also learn how the principles of agile software development and domain driven design can help you build products quickly and cost-efficiently, and compete with existing solutions.
Building Reusable Server Components in NextJS
React Summit US 2023React Summit US 2023
88 min
Building Reusable Server Components in NextJS
Workshop
Will Bishop
Mettin Parzinski
2 authors
React continues to evolve their beta capability, React Server Components, and they're continuing to further develop them in partnership with frameworks like NextJS.In this workshop, attendees will learn what React Server Components are, how to effectively build and use them in NextJS, and focus on one of the major advantages of React/NextJS: reusability through components.We will also cover related beta technologies enabled by the `app` directory, such as nested layouts and server actions (alpha/experimental capability).Join us for this hands-on, 120 minute workshop!Technologies:
React, JavaScript/Typescript, NextJS, Miro
Building Blazing-Fast Websites with Next.js and Sanity.io
React Summit 2023React Summit 2023
71 min
Building Blazing-Fast Websites with Next.js and Sanity.io
WorkshopFree
Nancy Du
Nataliya Ioffe
2 authors
Join us for a hands-on workshop where we'll show you how to level up your React skills to build a high-performance headless website using Next.js, Sanity, and the JAMstack architecture. No prior knowledge of Next.js or Sanity is required, making this workshop ideal for anyone familiar with React who wants to learn more about building dynamic, responsive websites.
In this workshop, we'll explore how Next.js, a React-based framework, can be used to build a static website with server-side rendering and dynamic routing. You'll learn how to use Sanity as a headless CMS to manage your website’s content, create custom page templates with Next.js, use APIs to integrate with the CMS, and deploy your website to production with Vercel.
By the end of this workshop, you will have a solid understanding of how Next.js and Sanity.io can be used together to create a high-performance, scalable, and flexible website.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Routing in React 18 and Beyond
React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Top Content
Routing in React 18 brings a native app-like user experience and allows applications to transition between different environments. React Router and Next.js have different approaches to routing, with React Router using component-based routing and Next.js using file system-based routing. React server components provide the primitives to address the disadvantages of multipage applications while maintaining the same user experience. Improving navigation and routing in React involves including loading UI, pre-rendering parts of the screen, and using server components for more performant experiences. Next.js and Remix are moving towards a converging solution by combining component-based routing with file system routing.
A Practical Guide for Migrating to Server Components
React Advanced Conference 2023React Advanced Conference 2023
28 min
A Practical Guide for Migrating to Server Components
Top Content
Watch video: A Practical Guide for Migrating to Server Components
React query version five is live and we'll be discussing the migration process to server components using Next.js and React Query. The process involves planning, preparing, and setting up server components, migrating pages, adding layouts, and moving components to the server. We'll also explore the benefits of server components such as reducing JavaScript shipping, enabling powerful caching, and leveraging the features of the app router. Additionally, we'll cover topics like handling authentication, rendering in server components, and the impact on server load and costs.
The New Next.js App Router
React Summit 2023React Summit 2023
27 min
The New Next.js App Router
Watch video: The New Next.js App Router
Today's Talk is about the Next.js App Router, which has evolved over the years and is now a core feature of Next.js. The Talk covers topics such as adding components, fetching remote data, and exploring layouts. It also discusses submitting form data, simplifying code, and reusing components. The App Router allows for coexistence with the existing pages router and enables data fetching at the layout level using React Server Components.
You Don’t Know How to SSR
DevOps.js Conf 2024DevOps.js Conf 2024
23 min
You Don’t Know How to SSR
The Talk covers the speaker's personal journey into server-side rendering (SSR) and the evolution of web development frameworks. It explores the use of jQuery for animations in SSR, the challenges faced in integrating React with Umbraco, and the creation of a custom SSR framework. The Talk also discusses the benefits of Next.js and the use of serverless artifacts for deployment. Finally, it highlights the features of Astro, including its function per route capability.
How to properly handle URL slug changes in Next.js
TypeScript Congress 2022TypeScript Congress 2022
10 min
How to properly handle URL slug changes in Next.js
Top Content
This Talk explains how to handle URL slug changes in Next.js by using the getStaticPaths and getStaticProps methods. It covers implementing redirects and provides a solution to eliminate the need for editors to perform additional steps. The approach involves tracking URL slug changes and issuing proper redirects. The speaker encourages the audience to reach out with any questions or experiences with handling URL slugs.
Killing BFFs with GraphQL and Next.js
React Advanced Conference 2021React Advanced Conference 2021
21 min
Killing BFFs with GraphQL and Next.js
Top Content
This talk discusses the challenges and benefits of using Backend for Frontends (BFFs) and microservices in software development. It highlights how Next.js can simplify the creation of BFFs and unify the gateway for microservices. The talk also emphasizes the advantages of Next.js API routes in simplifying the development, deployment, and maintenance of APIs. It showcases the implementation of a BFF using Next.js and API routes, and the extension of API routes in an executable way. The speaker also mentions launching a course on using Next.js API routes for building a serverless GraphQL API.