February 19, 2023

Adding lang attribute to html tag in Next.js

The lang attribute in HTML specifies the language of the content of a web page. It is important to have the lang attribute set correctly on a web page for the following reasons:

Accessibility: The lang attribute helps screen readers and other assistive technologies identify the language of the content and read it correctly for users with different languages and reading preferences.

SEO (Search Engine Optimization): Search engines use the lang attribute to identify the language of the content of a web page. This helps them to provide more accurate search results for users searching in specific languages.

Translation: The lang attribute can also help translation tools and services to identify the language of the content and translate it correctly, which can be useful for websites with international audiences.

Localization: The lang attribute is an important part of internationalization and localization efforts. By setting the lang attribute correctly, web developers can ensure that their content is displayed correctly for users in different regions and with different language preferences.

To add the lang attribute to the HTML tag in Next.js, you can add the attribute to the Document component. Here's how you can do it:

Create a custom Document component if you don't already have one. To do this, create a new file called _document.js in the pages directory (if it doesn't exist already) and add the following code:

import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
  render() {
    return (
      <Html lang="en">
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

or add i18n to your next.config.js file:

module.exports = {
  i18n: {
    locales: ['en'],
    defaultLocale: 'en',
  },
};

Top 6 Most Recommended Developer Books

The Pragmatic Programmer

by Dave Thomas, Andy Hunt

Check out this book on Amazon)}

The Pragmatic Programmer is one of those rare tech audiobooks you'll listen, re-listen, and listen to again over the years. Whether you're new to the field or an experienced practitioner, you'll come away with fresh insights each and every time. Dave Thomas and Andy Hunt wrote the first edition of this influential book in 1999 to help their clients create better software and rediscover the joy of coding. These lessons have helped a generation of programmers examine the very essence of software development, independent of any particular language, framework, or methodology, and the Pragmatic philosophy has spawned hundreds of books, screencasts, and audio books, as well as thousands of careers and success stories. Now, 20 years later, this new edition re-examines what it means to be a modern programmer. Topics range from personal responsibility and career development to architectural techniques for keeping your code flexible and easy to adapt and reuse.

Published: 2019

Genre: Programming

Cover of The Pragmatic Programmer

The Pragmatic Programmer

Cover of Clean Code: A Handbook of Agile Software Craftsmanship

Clean Code: A Handbook of Agile Software Craftsmanship

Cover of Working Effectively with Legacy Code

Working Effectively with Legacy Code

Cover of Introduction to Algorithms

Introduction to Algorithms

Cover of Eloquent JavaScript

Eloquent JavaScript

Cover of The Road to React

The Road to React