Of course! This is a classic and very common issue when working with i18next and mapping data. You've diagnosed the problem perfectly.
The reason the image fails to load is that the keys you are using to look up the image are coming directly from your translated JSON files.
The Core Problem
- Your
serviceImages object in the React component has hardcoded, language-agnostic keys: tuition, monthlyPayment, cafeteria, etc.
- When you call
t('services', { returnObjects: true }), i18next fetches the services object from the JSON file for the current language.
- You then iterate over this object using
Object.entries(services). The key in [key, value] is the key from your JSON file.
Let's imagine your translation files look like this:
en.json
{
"services": {
"title": "Our Services",
"tuition": { "title": "Tuition", "description": "..." },
"summerSchool": { "title": "Summer School", "description": "..." }
}
}
es.json (for Spanish)
{
"services": {
"title": "Nuestros Servicios",
"matricula": { "title": "Matrícula", "description": "..." },
"escuelaDeVerano": { "title": "Escuela de Verano", "description": "..." }
}
}
When your language is English, the code tries to find serviceImages['tuition'], which exists. The image loads.
When you switch to Spanish, the code gets the key "matricula" from es.json and tries to find serviceImages['matricula']. This key does not exist in your serviceImages object, so it returns undefined, and the src attribute of the <img> tag becomes empty, resulting in a broken image.
The Solution: Keep Keys Consistent
The best practice is to treat the keys in your translation files as stable identifiers, not as translatable text. The keys should be the same across all language files. Only the values (like title, description) should change.
Step 1: Update Your Translation Files
Your JSON files should be structured so the keys are consistent.