Utilizing internationalization and next/image in next.config.js alongside TypeScript

Currently, I am in the process of developing a miniature application using TypeScript within NextJS now that support for TypeScript comes standard in Next.js. Additionally, my aim is to integrate two recently introduced features:

Let's take a closer look at i18n:

// next.config.js
module.exports = {
    i18n: {
      // These are all the locales you want to support in
      // your application 
      // Sub-path Routing 
      locales: ['en', 'cn'],
      // This is the default locale you want to be used when visiting
      // a non-locale prefixed path e.g. `/hello`
      defaultLocale: 'en',
    },
  }

Unfortunately, incorporating these features into my next.config.js has proven quite challenging. While it functions as intended for regular applications (JavaScript only), I encounter the following error with the TypeScript version:

(node:15704) UnhandledPromiseRejectionWarning: TypeError: Cannot set property '__nextLocale' of undefined

If any of you have insights or can provide an example of how to effectively utilize next.config.js and integrate these new features into a TypeScript-based NextJS application, it would be greatly appreciated.

Answer №1

I believe there is no locale referred to as 'en' or 'cn'

https://en.wikipedia.org/wiki/Language_localisation

I suggest the following solution:

const withImages = require('next-images')
const withPWA = require('next-pwa')

module.exports = withPWA(
withImages({
  images: {
    domains: [
      'images.ctfassets.net',
      'imgix.cosmicjs.com',
	  'cdn.cosmicjs.com',
   ],
   i18n: {
      // List of supported locales in your application
      locales: ['en-US', 'zh-CN'],
      // Default locale when visiting a page without specifying a locale
      defaultLocale: 'pt-BR',
      ...
   }
 },

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Submitting information to a server

I have a simple Angular 6 app with follow and unfollow buttons. When you click follow, the number increases. I want to save these follower numbers to a JSON server. Here is the link to the JSON server documentation: JSON Server Documentation To see a dem ...

Type ' ' cannot be assigned to type ''..ts(2322) ANOTHA ONE

Being a beginner in TypeScript and currently learning about enums, I encountered an error with the following example code that I cannot seem to understand. Here's the code snippet: enum Status { SUCCESS = 'success', FAILED = 'fa ...

Regex struggles to identify words containing foreign characters

Here is a method I have created to check if a user-input term matches any blacklisted terms: static checkAgainstBlacklist(blacklistTerms, term) { return blacklistTerms.some(word => (new RegExp(`\\b${word}\\b`, 'i&ap ...

What is the proper syntax for defining all CSS properties as a type in TypeScript?

Using StyleProps allows for specifying size and color. I would prefer if it covered all styles. This way, I can easily pass down styles directly to specific parts of the component. What should I include to encompass all CSS properties? How can I modify ...

Absolute imports in create-react-app do not function properly when using yarn v2 workspaces alongside typescript

I am currently utilizing yarn v2 workspaces, and within my workspaces, I have a frontend project built using create-react-app / react-scripts. My goal is to enable absolute imports in the frontend application so that I can simply do things like import Butt ...

How can you incorporate a line break into a template method?

Is there a way to create a line break in TypeScript? I tried searching for a solution here, but couldn't find one. I currently have a method that displays 10 numbers, and I would like to insert a line break between each number. I attempted using &bs ...

Using NextJs to Fetch Data from an External REST API Without Exposing the Request in the Client-Side Network Tab

In the process of building an application using Next.js for the frontend and PHP for the backend (REST API), my goal is to securely handle form submissions. Specifically, I need a way to extract form data from React.js and send it to the PHP-developed RE ...

Mastering the use of Action.Submit in adaptive cards to simulate user input

I am trying to implement MessageFactory.SuggestedActions within my "welcomeCard" adaptive card. Essentially, in my adaptive card (welcome card), I have several buttons for the user to click on, each with an Action.Submit type. { "type" ...

Tips for concealing XHR Requests within a react-based single page application

Is there a way to hide the endpoint visible in Chrome's devtools under the network tab when data is fetched in React? Can server-side rendering solve this issue? ...

Nextjs: Fallback for Loading or Suspense will not be displayed in a production environment

After updating to nextjs v13, I have successfully moved my personal page to the app directory. I experimented with adding loaders for RSCs by first using the fallback prop of the Suspense component and also including a loading component within each route ...

Experimenting with the static method within a singleton class using Typescript and Sinon

I have a separate layer in my application that uses a DAO class to retrieve data from the repository. I've implemented the DAO class as a Singleton and made its methods static. In another class, I've created service methods to manipulate the dat ...

Next JS - Value Mapping

{guideData?.night_out_list.length > 0 && guideData?.night_out_list? .map((guide_data, i) => ( // guide_data.main_category ))} The code snippet above represents a loop that retrieves a total of 19 results as shown below: { ...

typescript loop with a callback function executed at the conclusion

I am struggling with this code and it's driving me crazy. addUpSpecificDaysOfWeek(daysInMonth: any, callbackFunction: any){ var data = []; var that = this; daysMonth.forEach(function(day){ that.statsService.fetchData(that.userid, d ...

Can Angular 9 be used to compile a latex document?

Is it possible to utilize Angular 9 to compile and generate PDF files using latex? Specifically, I am curious about how to compile a document using Angular and Pdflatex. The idea is for the client to input their data in the form of a JSON data structure ...

How can type annotations be properly incorporated into this TypeScript code using an inline function class?

How can I provide type annotations to inform TypeScript that this code is correct? Indeed, I do require that inline function class. The code presented here is a simplified version of my actual problem. let x = 10; const obj = new (function() { if(--x) r ...

Encountering obstacles when trying to implement mongoose virtuals in a TypeScript environment

I have been exploring how to utilize virtuals in mongoose with typescript. Let's say I have an interface for defining a user. interface User { id: mongoose.ObjectId; name: string; likes: string; } Next, I define a schema for mongoose. // ...

Issue: React child components cannot be objects (received: object with keys)

Hey everyone, I could really use some help figuring out what I'm doing wrong. Here is the error message I'm receiving: Error: Objects are not valid as a React child (found: object with keys {id, title, bodyText, icon}). If you meant to render a ...

What is the function of async in Next.js when triggered by an onClick

Need help with calling an async function pushData() from a button onClick event async function pushData() { alert("wee"); console.log("pushing data"); try { await query(` //SQL CODE `); console.log("Done&quo ...

How to identify generic return type in TypeScript

My goal is to develop a core dialog class that can automatically resolve dialog types and return values based on the input provided. I have made progress in implementing this functionality, but I am facing challenges with handling the return values. Each ...

Retrieve all articles from a user using the TypeORM - findAll function

Is there a way to retrieve all articles of a specific user using the TypeORM package? In Sequelize, I have the following function: async findAllByUser(userUuid: string, findOptions: object): Promise<Article[]> { return await Article.findAll< ...