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

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
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
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.
Full Stack GraphQL In The Cloud With Neo4j Aura, Next.js, & Vercel
GraphQL Galaxy 2021GraphQL Galaxy 2021
161 min
Full Stack GraphQL In The Cloud With Neo4j Aura, Next.js, & Vercel
WorkshopFree
William Lyon
William Lyon
In this workshop we will build and deploy a full stack GraphQL application using Next.js, Neo4j, and Vercel. Using a knowledge graph of news articles we will first build a GraphQL API using Next.js API routes and the Neo4j GraphQL Library. Next, we focus on the front-end, exploring how to use GraphQL for data fetching with a Next.js application. Lastly, we explore how to add personalization and content recommendation in our GraphQL API to serve relevant articles to our users, then deploy our application to the cloud using Vercel and Neo4j Aura.

Table of contents:
- Next.js overview and getting started with Next.js
- API Routes with Next.js & building a GraphQL API
- Using the Neo4j GraphQL Library
- Working with Apollo Client and GraphQL data fetching in Next.js
- Deploying with Vercel and Neo4j Aura
High-performance Next.js
React Summit 2022React Summit 2022
50 min
High-performance Next.js
Workshop
Michele Riva
Michele Riva
Next.js is a compelling framework that makes many tasks effortless by providing many out-of-the-box solutions. But as soon as our app needs to scale, it is essential to maintain high performance without compromising maintenance and server costs. In this workshop, we will see how to analyze Next.js performances, resources usage, how to scale it, and how to make the right decisions while writing the application architecture.

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
Concurrent React and Server Components are changing the way we think about routing, rendering, and fetching in web applications. Next.js recently shared part of its vision to help developers adopt these new React features and take advantage of the benefits they unlock.In this talk, we’ll explore the past, present and future of routing in front-end applications and discuss how new features in React and Next.js can help us architect more performant and feature-rich applications.
The New Next.js App Router
React Summit 2023React Summit 2023
27 min
The New Next.js App Router
Next.js 13.4 recently released the stable version of the "App Router" – a transformative shift for the core of the framework. In this talk, I'll share why we made this change, the key concepts to know, and why I'm excited about the future of React.
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
Server Components are the hot new thing, but so far much of the discourse around them has been abstract. Let's change that. This talk will focus on the practical side of things, providing a roadmap to navigate the migration journey. Starting from an app using the older Next.js pages router and React Query, we’ll break this journey down into a set of actionable, incremental steps, stopping only when we have something shippable that’s clearly superior to what we began with. We’ll also discuss next steps and strategies for gradually embracing more aspects of this transformative paradigm.
You Don’t Know How to SSR
DevOps.js Conf 2024DevOps.js Conf 2024
23 min
You Don’t Know How to SSR
A walk-through of the evolution of SSR in the last twelve years. We will cover how techniques changed, typical problems, tools you can use and various solutions, all from the point of view of my personal experience as a consumer and maintainer.
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
If you're using a headless CMS for storing content, you also work with URL slugs, the last parts of any URL. The problem is, content editors are able to freely change the slugs which can cause 404 errors, lost page ranks, broken links, and in the end confused visitors on your site. In this talk, I will present a solution for keeping a history of URL slugs in the CMS and explain how to implement a proper redirect mechanism (using TypeScript!) for dynamically generated pages on a Next.js website.

Add to the talk notes: https://github.com/ondrabus/kontent-boilerplate-next-js-ts-congress-2022 
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
Frontend applications are getting more and more complicated, often delivering far more than just a UI. With this growing complexity comes a growing need for knowledge from the developers creating it, as they have to deal with matters like state-management, authorization, routing and more. In this talk, I'll show you how to use GraphQL in your Next.js application to create a data layer for your application, that exists between your frontend and your backend. In this data layer, you can handle complex matters to help you to keep your frontend application clean and "stupid".