DEV Community

Cover image for How to set the HTML lang attribute in Next.js?
Dhairya Shah
Dhairya Shah

Posted on • Originally published at codewithsnowbit.hashnode.dev

7 3

How to set the HTML lang attribute in Next.js?

What is HTML lang Attribute?

The HTML lang attribute is used to identify the language of the content on the web and, when you've got an international audience say for example Spanish and English, it helps search engines return language-specific results for Spanish or English screen readers to provide correct pronunciation.

the lang attribute in Next.js

Here's what lang attribute looks like in HTML,

<html lang="en">
  <!-- Head and Body -->
</html>

Enter fullscreen mode Exit fullscreen mode

You can't just simply set the lang attribute by changing the HTML tag in Next.js.

To set the lang attribute to the HTML tag in Next.js. We have to add the i18n object in the next.config.js.

Here's how it must be done,

module.exports = {
  i18n: {
    locales: ["en"],
    defaultLocale: "en",
  },
  reactStrictMode: true,
}
Enter fullscreen mode Exit fullscreen mode

Let's understand the properties of the i18ni n above snippet,

  • locale: It is the array of the locales values that you want to add support to the website. For example, you can set the value to es for the Spanish language.
locales: ["es"]
Enter fullscreen mode Exit fullscreen mode
  • defaultLocale: It defines the default locale to be used on different pages.

After updating next.config.js, restart the server. Your final source code will have the lang="en" attribute in html tag.

<html lang="en"></html>
Enter fullscreen mode Exit fullscreen mode

Check out the full list of Language codes

Conclusion

We hope you enjoyed our article on how to set the lang attribute. Thank you for reading!

We all have used dummy text at some point or another as developers - for example, "Lorem Ipsum". Recently, I've launched Let's Lorem Ipsum, an easy-to-use service that enables you to copy and paste useful dummy content into projects where required - like annoying form fields!

Let’s Lorem Ipsum - https://letsloremipsum.vercel.app/

Let’s connect

If you found my content helpful and would like to thank me for my time, feel free to Buy me a coffee - https://www.buymeacoffee.com/codewithsnowbit.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay