Skip to content
Advertisement

Change localization directory base on local storage item

In a particular step in the React stepper component, I have to change the directory based on a steps language. Unfortunately in one of the steps, the text is hardcoded into the images which requires me to make this change. The app uses i18next for localization.

Currently, all images for this step are rendered with this code:

JavaScript

There is an item in window.localStorage.i18nextLng that returns the “es” key for Spanish.

I tried creating a conditional statement that would read this key and then return the directory based on language:

JavaScript

All of the other steps are rendering correctly with the current structure. However, I can’t get the images from the es folder to map.

JavaScript

How can I change my directory based on this key or is there a way to concatenate the folder location in the path of the original code that would allow flexibility for more than one language? Is there a better way this scenario can be handled with i18next? Thanks in advance for any help.

Advertisement

Answer

I am afraid Webpack by default (and in CRA) will not allow creating dynamic import path. Moreover, you should not mix CommonJS and ES6 (use import instead of require).

You will need to explicitly define these resources. You may use lazy evaluation to lower the size of whole bundle (so one bundle does not contain all languages data).

JavaScript

You can also create modules that contain components / assets for specific language and import those modules dynamically.

assets/en.js

JavaScript

assets/es.js

JavaScript

LocalComponentsProvider.js

JavaScript
Advertisement