Having trouble with removing a language from the router in Next.js when using ni18n?

I've been working on a website using ni18n with Next.js, but I'm having trouble removing the language part from the URL even after trying to force remove it.

What I'm aiming for is a URL like this:

"http://localhost:3000"

However, whenever I type in the website address, it redirects to "localhost:3000/tr" instead.

Any thoughts on why this might be happening? I'm stumped.

//_app.tsx
function MyApp({ Component, pageProps }: AppPropsWithLayout) {
  // Use the layout defined at the page level, if available
  const getLayout = Component.getLayout ?? ((page) => page)
  
  if(typeof window !== 'undefined'){
    const locale = window.localStorage.getItem('locale') || 'en'
    useSyncLanguage(locale)
  }
  
  
  return getLayout(
      <ThemeProvider attribute='class'>
        <Component {...pageProps} />
      </ThemeProvider>
  )
}
// ni18n.config.ts
import type { Ni18nOptions } from 'ni18n'

export const ni18nConfig: Ni18nOptions = {
  supportedLngs: ['en', 'tr'],
  ns: ['common','navbar'],
}
//18next.d.ts
declare module 'react-i18next' {
  interface CustomTypeOptions {
    resources: {
        common: typeof common,
        navbar: typeof navbar
    }
  }
}
//next.config.js
module.exports = {
    i18n: {
      defaultLocale: 'en',
      locales: ['en', 'tr'],
    },
  }

Answer №1

After much pondering, I have finally unraveled the mystery behind this occurrence. The reason lies in the default setting of 'localeDetection' being true in 18next. This issue can be resolved by disabling this setting in the configuration file.

module.exports = {
  i18n: {
    ...
    localeDetection: false,
  },
}

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

Fixing MySQL Specified Key is Too Long Error When Using NextAuth With Prisma

I'm interested in using NextAuth with Prisma Adaptor for a MySQL (5.7.36) database, but I encounter the following error: Error: Specified key was too long; max key length is 1000 bytes 0: sql_migration_connector::apply_migration::migration_step ...

Testing React Hooks in TypeScript with Jest and @testing-library/react-hooks

I have a unique custom hook designed to manage the toggling of a product id using a boolean value and toggle function as returns. As I attempt to write a unit test for it following a non-typescripted example, I encountered type-mismatch errors that I' ...

Utilize [markdown links](https://www.markdownguide.org/basic-syntax/#

I have a lengthy text saved in a string and I am looking to swap out certain words in the text with a highlighted version or a markdown link that directs to a glossary page explaining those specific words. The words needing replacement are contained within ...

I've been encountering issues when trying to use useForm and react-hook-form in conjunction with NextJs

Struggling with using 'useForm' in my NextJs project. The error message "Cannot resolve 'react-hook-form'" keeps popping up. Any ideas on how to solve this issue? import React from "react"; import { useForm } from "react- ...

The replacer argument of the JSON.stringify method doesn't seem to work properly when dealing with nested objects

My dilemma is sending a simplified version of an object to the server. { "fullName": "Don Corleone", "actor": { "actorId": 2, "name": "Marlon", "surname": "Brando", "description": "Marlon Brando is widely considered the greatest movie actor of a ...

TS - decorator relies on another irrespective of their position within the class

Is it possible to consistently run function decorator @A before @B, regardless of their position within the class? class Example { @A() public method1(): void { ... } @B() public method2(): void { ... } @A() public method3(): void { ... } } In the sc ...

Avoid the occurrence of the parent's event on the child node

Attempting to make changes to an existing table created in react, the table is comprised of rows and cells structured as follows: <Table> <Row onClick={rowClickHandler}> <Cell onCLick={cellClickHandler} /> <Cell /> ...

The 'subscribe' property is not available on the type '() => Observable<any>'

File for providing service: import { Observable } from 'rxjs/Rx'; import { Http, Response} from '@angular/http'; import { Injectable } from '@angular/core'; import 'rxjs/add/operator/Map'; @Injectable() export clas ...

The `react-router-dom` in React consistently displays the `NotFound` page, but strangely, the active class for the home page does not get

Currently, I am utilizing "react-router-dom": "^6.4.1" in my application and encountering two main issues: Upon navigating to another page, the home page retains the active class, resulting in both the new page and the home page disp ...

Issue with Nextjs environmental variables not functioning in JavaScript pages

Currently, I am in the process of developing a Next.js website. Within my JavaScript file, I am fetching data from an API and assigning a token using the .env.local file. However, when attempting to access the .env variable that I've set up, it seems ...

What is the preferred method for logging out: using window.location.replace('/') or setting window.location.href to window.location.origin?

When it comes to a logout button, which is the better option: window.location.replace('/') or window.location.href=window.location.origin? Can you explain the difference between these two methods? It's my understanding that both of them remo ...

Strange behavior of the .hasOwnProperty method

When attempting to instantiate Typescript objects from JSON data received over HTTP, I began considering using the for..in loop along with .hasOwnProperty(): class User { private name: string; private age: number; constructor(data: JSON) { ...

Issues with Content-Security-Policy Implementation in Next JS and AMP Technology

While developing an AMP application with Next JS, everything seems to work perfectly in localhost. However, upon moving to the production environment, I encountered errors related to AMP not being allowed to load its workers. The initial error message is: ...

Currently, I'm harnessing the power of TypeScript and React to identify and capture a click event on a dynamically generated element within my document

Is there a way to detect a click on the <p> tag with the ID of "rightDisplayBtn"? I've tried using an onclick function and event listener, but neither seem to be working as expected. function addDetails() { hideModal(); addBook ...

Upon upgrading NextJS, the error "previewData is not a function" has been encountered

I encountered an issue with my NextJS and Sanity.io blog project after updating NextJS from version 13.2.3 to 13.4.2. Upon running the project, I received the following error message: Error: (0 , next_headers__WEBPACK_IMPORTED_MODULE_1__.previewData) is ...

Tips for securely passing props based on conditions to a functional component in React

I came across this situation: const enum Tag { Friday: 'Friday', Planning: 'Planning' } type Props = { tag: Tag, // tour: (location: string) => void, // time: (date: Date) => void, } const Child: React.FC<Props> = ...

"Take control of FileUpload in PrimeNG by manually invoking it

Is there a way to customize the file upload process using a separate button instead of the component's default Upload button? If so, how can I achieve this in my code? Here is an example of what I've attempted: <button pButton type="button" ...

Angular Error: Property 'map' is not found in type 'OperatorFunction'

Code: Using map and switchMap from 'rxjs/operators'. import { map, switchMap } from 'rxjs/operators'; Here is the canActivate code for route guard: canActivate(): Observable<boolean> { return this.auth.userObservable ...

Leverage the globalDependencies feature in Angular2 to utilize Typescript tsd files

I am attempting to incorporate typescript tsd's from DefinitelyTyped into an Angular2 project (RC.0), but encountering issues with loading global dependencies properly: typings install --save dt~hellojs --global --save npm install --save hellojs Her ...

"Implementing a monorepo with turborepo for seamless deployment on Vercel: A step-by-step

There has been recent news about Turborepo being acquired by Vercel, sparking my interest to dive into it. To start, I initiated a turbo repo project with the following command: pnpx create-turbo Afterwards, I attempted to deploy it on Vercel by referring ...