I'm currently working on a multilanguage application using Next.js, and I'm facing an issue with dynamically importing the RTL (Right-to-Left) CSS file when switching to the Arabic language. My goal is to have the application switch between RTL and LTR (Left-to-Right) layouts based on the selected language.
I attempted to put the import statement inside a component and use lazy loading, but Next.js threw an error stating that I can only import CSS in the
_app
component. Global Css cannot be imported from files other than your customI also tried importing a variable from a CSS file and using it to conditionally apply styles, but the application seemed to ignore the condition entirely (the rtl styles all applied no matter what).Importing the styles from css file image:2 applying the styles conditionally
Additionally, I experimented with using a conditional rendering approach like
{lang === "ar" && <link rel="stylesheet" href="style.css" />}
, but Next.js was unable to locate the CSS file regardless of what I tried. applying the link tag conditionallyI tried importing it in a conditional statement : it works when i switch to arabic, but statys like that and i cant switch back to other languages (ignore the squiggly line importing styles in a condition
I even tried putting all the classes in a javascript variables and then using them in the style tag. But for some reason, some styles get applied correctly and others get messed up storing css classes in a js string
I'd greatly appreciate any insights or alternative solutions from the community to dynamically switch the CSS between RTL and LTR layouts based on the selected language in my Next.js application.