Implementation of the I18next library

I am currently integrating react-i18next into my application to implement a switch button for French and English languages. Unfortunately, I am facing some issues as the translation is not working properly. I suspect it's related to the JSON file recognition, but I'm not entirely certain.

When I use {t("submit")}, it simply renders the string "submit" instead of fetching the corresponding translation from the file. It should display "Login".

Below is the relevant code snippet:

index.jsx:

// Code here

i18n.js :

// Code here

LoginScreen.js:

// Code here

en.js: (english json file)

{
    "submit": "Login"
}

I have double-checked the path to the translation file and reviewed for any possible typos, but the issue persists. Your help would be greatly appreciated.

Thank you in advance.

Answer №1

While I may not be an expert in reactjs, I am able to demonstrate my implementation in react-native.

I have organized the resources within the src folder as follows:

src/ lang >

   en > common.json
   fr > common.json

The common file contains values structured like this:

{
  "appStrings": {
     "ABOUT": "About",
    }
 }

To import these sources into the index.js file, I use the following code:

import resources from 'src/lang'; // ensure correct path is used
import {I18nextProvider} from 'react-i18next';
import i18next from 'i18next';


// Enabling multilingual support
i18next.init({
  compatibilityJSON: 'v3',
  interpolation: {
    escapeValue: false,
  },
  lng: 'en',
  resources: resources,
});

Next, I wrap my app with the i18Next provider:

<I18nextProvider i18n={i18next}>
    <App />
  </I18nextProvider>

Within components, I utilize it like so:

const {t} = useTranslation();
<Text>{t('t.appStrings.ABOUT')}</Text>

You can modify the initialization according to your needs:

lng : 'en/fr' // based on your requirements

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

Trigger notifications exclusively for specific hyperlink texts that are selected

I'm facing an issue where I need to notify the user when a specific link text for a hyperlink is clicked. The code provided here showcases the problem I am currently encountering. At the moment, the alert triggers whenever any link text is clicked, ra ...

What is the equivalent of getElementById in .ts when working with tags in .js?

Looking to incorporate Electron, Preload, Renderer with ReactJS and TypeScript into my project. <index.html> <body> <div id="root" /> <script src='./renderer.js'/> </body> <index.ts> const root = Re ...

Coordinated Universal Time on the Website

I am currently developing a website that will be exclusively accessible through the intranet, but it targets users across Australia. Recently, I have been instructed to explore the idea of incorporating UTC time on the site. I am contemplating how I can i ...

ReactJS encountered an error: Module '@progress/kendo-scheduler-react-wrapper' could not be located

![kendo ui npm issue]https://i.stack.imgur.com/5cERD.jpg Struggling with a 'can't resolve' error when attempting to integrate this kendo-ui package. ...

In Node JS, the variable ID is unable to be accessed outside of the Mongoose

When working with a Mongoose query, I encountered an error where I am trying to assign two different values to the same variable based on the query result. However, I keep getting this error: events.js:187 throw er; // Unhandled 'error' ev ...

Unable to identify the element ID for the jQuery append operation

After attempting to dynamically append a textarea to a div using jQuery, I encountered an issue. Despite the code appearing to work fine, there seems to be a problem when trying to retrieve the width of the textarea using its id, as it returns null. This s ...

When it comes to MongoDB aggregation, the order of indexes is not taken into consideration

To retrieve the 100 latest documents from a MongoDB collection, where each document is a combination of multiple documents with a similar field (in this case timestamp), I have implemented a series of queries using Node.js: return q.ninvoke(collec ...

Execute test scenarios and upon successful completion, initiate deployment of a React JS application using Jenkins

I am currently facing an issue with running test cases and deploying a React-js app using Jenkins. After pushing my code to Git, I can successfully run the React-js app locally. However, when trying to execute the second command mocha (which is used to ru ...

What is the proper way to utilize the 'shallow: true' parameter when using 'router.replace' in the latest version of Next.js?

Is there a workaround for using shallow: true with router.replace? I am currently working on Next 13 and have not been able to find any solution that replicates the behavior of shallow. I'm looking for a way to achieve the same result as using shallo ...

Title: Troubleshooting Nest.js Google OAuth Redirection Problem with CORS Blocking

I am currently encountering an issue with my Nest.js application that utilizes Google OAuth for authentication. The backend of Nest.js is running on port 5000, while the frontend (Next.js) is running on port 3000. I have properly configured the Google Clou ...

Calling `$httpBackend.verifyNoOutstandingRequest()` will not result in any errors being raised

During my test setup, I create an HTTP request but do not execute $httpBackend.flush(). Here is the test scenario: describe("Unit: Testing Services", function() { beforeEach(angular.mock.module('EmsWeb.Services')); describe("Unit: DalSer ...

The error is popping up as I try to deploy my Next.js application on Vercel

warning " > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d0f181c1e095009120d5011121c1914131a501f1c0f3d4f534c534d">[email protected]</a>" has an incorrect peer dependency "react@^16 || ^17" ...

Having some trouble creating a ping command in discord.js that displays latency; encountered this error message

I encountered an issue while trying to create a ping command that displays latency. I am unsure why this error is showing up, here is the specific error message - TypeError: Cannot read properties of undefined (reading 'ping') at Object.execu ...

Tips for implementing horizontal scroll in Material UI grid

I'm currently working on creating a kanban board using a grid layout with seven columns. Each column in the kanban board is built using Material UI grid components. Although I've attempted to implement the following CSS code, it doesn't see ...

Troubleshoot Node.js server-side rendering in Visual Studio Code

Debugging an SSR React application on the server side has been a major struggle for me. Our team is working on developing a new app from scratch, and with the project being so large, debugging the code is crucial. Here's the webpack configuration for ...

What is the best way to retrieve the data from this date object?

How can I access the date and time in the code below? I've attempted using functions within the Text block without success. It's unclear to me what mistake I'm making or how to correctly access this object, or transform it into an object th ...

Issue regarding Jquery widget

I am working with a widget that looks like this $.widget("ui.myWidget", { //default options options: { myOptions: "test" }, _create: function () { this.self = $(this.element[0]); this.self.find("thead th").click(fun ...

TypeScript will show an error message if it attempts to return a value and instead throws an

Here is the function in question: public getObject(obj:IObjectsCommonJSON): ObjectsCommon { const id = obj.id; this.objectCollector.forEach( object => { if(object.getID() === id){ return object; } }); throw new Erro ...

`Finding the nodejs API route for displaying images from a database`

How can I successfully display an image from the database without getting a string of question marks instead? Click here to see the issue >>> When attempting to directly call the API using the provided link, the following result is returned: {&qu ...

Passport sessions do not retain persistence

Having some trouble implementing OAuth 2.0 login where the sessions don't persist after authentication is a common issue. Additionally, there seems to be a problem with the app getting stuck in the routes/bnetauth.js file during the redirect in the ca ...