Loading Namespaces Automatically with i18next

I am currently utilizing I18Next as a JavaScript-based Translation Solution, and here is the specific functionality that I require:

  1. By default, a namespace called "Core" is loaded. While it contains most of the keys I need for translation, there are some keys missing.
  2. There isn't a fixed list of namespaces available, so I cannot simply specify the namespaces I want to load using i18n.init.
  3. When the page loads, certain "Modules" may be added to the Application and these modules also need to undergo translation. These modules should indicate their i18n namespace name somewhere, then i18n should make the keys within that namespace accessible.

Essentially, I am seeking a way for i18next to automatically load namespaces when they are called. It can be assured that any namespaces accessed through t("[SomeNamespace]Key.Key2"); are valid and do exist. The challenge lies in the fact that i18next does not have an automatic "autoload" feature and I haven't discovered a method to manually load a resource file after i18n.init has been executed.

Below is the current code snippet I am working with:

    $.i18n.init(
        {
            lng: "en",
            fallbackLng: "en",
            useCookie: false,
            resGetPath: "System/i18n/__ns__/__lng__.json",
            ns: "Core"
        },
        function(t) {
            System.I18N = t;

            alert(System.I18N("LoginUI:Messages.Name"));
        }
    );

Upon execution, it only displays LoginUI:Messages.Name instead of the desired translation found in System/i18n/LoginUI/en.json:

{
    "Messages": {
        "Name": "Logon Interface"
    }
}

(In this scenario, Core/en.json is irrelevant. At present, my primary focus is on autoloading "LoginUI/en.json," or alternatively, being able to manually trigger its loading process.)

Answer ā„–2

After delving deep into the source code, I have devised a solution that somewhat functions, but it definitely needs enhancements for future use.

Inside the definition of i18n.addjQueryFunct(), include the following to access resStore (the translation storage variable):

$.i18n._getResStore = _getResStore;
$.i18n._writeResStore = _writeResStore;

function _getResStore() {
    return resStore;
}

function _writeResStore(r) {
    resStore = r;
}

If you need to load an additional namespace, simply follow these steps:

// define options, run $.i18n.init, etc...
// let's say ns = "namespace_foobar_new"
options.ns.namespaces.push(ns);
$.i18n.sync._fetchOne(lang, ns, $.extend({}, $.i18n.options, options),
    function(err, data) {
    store = {};
        store[lang] = {}; store[lang][ns] = data;

            newResStore = $.extend(true, {}, $.i18n._getResStore(), store);
            $.i18n._writeResStore(newResStore);
    });

Whew!

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

Hello, I have been working on implementing Mern Stack authentication for the past two weeks but I am still struggling to resolve a persistent error

An error message is displaying on the server stating: Server running on port 5000 Error: MongooseError: The uri parameter for openUri() must be a string, it is currently set as "undefined". Ensure that the first parameter in mongoose.connect() or mongoose ...

Pause and Persist

Can someone help me clarify a code issue I'm facing? I have a piece of code that checks an array for overlapping values based on different criteria. This code specifically deals with rows and columns in a Google Sheet using GAS. Here's what I cur ...

How can an Angular directive effectively serve as a front-facing interface for interacting with other elements?

This question delves into the realm of Web Components, with the examples being written in Angular for its versatility in handling certain issues (such as replace even though it's deprecated) and familiarity to many developers. Update After consideri ...

Creating dynamic input fields in JavaScript by using class-based methods

I have a requirement to dynamically duplicate a couple of fields whenever the user clicks on the Add button and then capture the data from these fields. While searching online, I came across various tutorials like Tutorial which demonstrate how to create n ...

retrieving identifiers from a separate table for an array of values

As a newcomer to node and noSQL databases, I am facing challenges in grasping the concept of passing an array of IDs and retrieving the corresponding values from another table. I have 'users' and 'products' tables in my database. The st ...

Tips for accessing jQuery UI tab elements and adjusting visibility for specific panels

How can I retrieve the panel numbers of jQuery UI tabs and adjust their visibility using CSS? I am looking to identify the panel numbers of each tab and control the visibility of certain tabs. <div id="tabs"> <ul> <li><a href="#"> ...

Unable to receive a response after attempting to send an email through XMLHttpRequest

I have been struggling to send emails even after editing the template. I am currently working on creating a subscription box and here is the code I have so far: var x= document.getElementById("emailtxt").value; var uploadFormData = new FormData(); upload ...

Activate video playback when scrolling, but ensure it only occurs one time

I've encountered an issue with my script that is meant to play a video when it reaches a certain position on scroll. The problem is, if the video is paused and scrolling continues, it starts playing again. I attempted to use just one scroll function b ...

Obtain the response from the aspx page in JSON format instead of the standard HTML

Is it possible to make an ajax call to the server and fetch the HTML of a specific page, like foo.aspx? Here is an example of the HTML code for foo.aspx: <form> <div>foo</div> </form> If I were to call this page from a remote loca ...

Tips for properly setting and accessing data objects in React when utilizing a Firebase database

Iā€™m encountering issues with storing and retrieving data using the useState hook in React after fetching it from a Firebase database. The issue appears to be related to not all users having all values in the playerData data packet ā€“ for example, if a p ...

How to rotate an object in Threejs using the mouse without having to click and drag?

I'm searching for a solution to rotate around an object in Threejs without the need to hold down the mouse button. A good example can be found on this website: which utilizes Threejs. Despite searching through forums and documentation, I have been un ...

Retrieving a map using latitude and longitude coordinates saved in a database

I have a webpage with an embedded Google Map and a dropdown list of cities. The latitude and longitude values for each city are stored in a database. When a user selects a city from the dropdown list and clicks submit, I want the map to load with the corre ...

Despite using callbacks when passing props in React JS components, the rerendering still occurs

In my React project, I implemented callbacks to prevent excessive rerendering due to the large amount of data being displayed and numerous buttons that trigger these rerenderings when clicked by the user. The useCallback hooks are effective in preventing ...

Retrieve data from an SQL database and populate an HTML dropdown menu in a web page using PHP

I am a beginner in the world of JavaScript and facing some challenges. I am working with PHP 7 and attempting to retrieve data from an SQL database, specifically a table. My goal is to extract a single column from this table and display it in a dropdown me ...

Using Vue.js and JavaScript to access a single variable across multiple class methods

I am working on organizing my custom channel logic into its own class. During the created lifecycle method, I am executing each method in the class I created. However, I am facing a challenge in retaining the instance of the socket that was created in the ...

Audio waves visualization - silence is golden

I am attempting to create a volume meter, using the web audio API to create a pulsation effect with a sound file loaded in an <audio> element. The indicator effect is working well with this code; I am able to track volume changes from the playing aud ...

Implement the functionality of mouse panning on the left mouse button for a 2D graph generated using a modified version of D3.js fork for the 3

I am attempting to create a 2D graph using a 3D Force Directed Graph system (specifically with D3.js, available at this link). My goal is to initiate the graph in 2D mode with disabled camera rotation. Users should have the option to switch to 3D mode with ...

Error: The component is unable to access this.props.match because it is undefined

Currently, I am in the process of designing a dashboard that will allow users to modify records and perform other actions. I have implemented edit and delete buttons that direct users to the appropriate routes. The challenge arises when accessing the edi ...

What methods does a browser use to track and store the locations of previously visited pages in its browsing

In my quest to craft personalized back and forward buttons, I experimented with storing visited pages in a JavaScript array. However, I discovered that maintaining the locations of visited pages in an array proved challenging. I am curious to know how a b ...

Difficulty displaying callback function within an object using Jquery for each

When iterating through a for-each loop in jQuery, I want to execute a callback function that is inside an object. Let's assume we have the following object: var myVar = { firstObj: { name: 'something' }, myFunc: function(value) ...