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.