Hello everyone! I'm new to SolidJS (but worked with React/Next before) and I wanted to implement i18next in a very basic app that I have that shows weather data.
The main thing is **I don't want to use any third-party i18n libraries for SolidJS (like solid-i18next etc) but want to use just the plain old i18next **(https://www.i18next.com/).
Normal translations are implemented but now I want to add a LanguageSwitcher component that can toggle between the en and the es locale. Here's my current code:
// LanguageSwitcher.jsx
import i18next from "i18next";
const LanguageSwitcher = () => {
function handleLanguageChange(event) {
i18next.changeLanguage(event.target.value);
}
return (
<div>
<label htmlFor="language-switcher">
<select id="language-switcher" onChange={handleLanguageChange}>
<option value="en">{i18next.t("english_label")}</option>
<option value="es">{i18next.t("spanish_label")}</option>
</select>
</label>
</div>
);
};
export default LanguageSwitcher;
And here's my i18next config in App.js:
// App.jsx
createEffect(() => {
i18next
.use(Backend)
.init({
lng: "en",
debug: true,
interpolation: {
escapeValue: false,
},
fallbackLng: false,
ns: "translations",
backend: {
loadPath: "./../locales/{{lng}}/{{ns}}.json",
},
})
.then(() => setIsReady(true))
.catch((err) => console.error(err));
});
When I choose a different locale like es, i18next does work and logs this to console:
i18next: languageChanged es
But the issue is that I don't see it reflected on the UI. I know this issue is related to SolidJS state changes but I wasn’t able to get how to make it work. Please let me know 😄