Using multiple namespaces with i18next: files do not load properly

I am kind of new using i18next with React. I am currently working on building a personal website/portfolio with ReactJS/TS and want to translate content to English. I use JSON files to store my translations.

I’d like to keep things organised so I have several translation files in different folders:

  • My /public/locales/en and /public/locales/fr folders contain basic translations for my website (like buttons, page titles, text, etc.),
  • My /src/data/en and /src/data/fr folders contain data that I want to display dynamically (projects, skills and career milestones).

So far, “basic” translations work perfectly fine. With the help of this article and this one, I have created a LocaleSelector component that allows me to switch languages.

Things get trickier when it comes to dealing with repetitive data like those stored in my /data folder, because I cannot seem to get my translation files loaded and rendered properly.
I’ll illustrate my issue with the projects.json use case.

Before using i18next, it was pretty straightforward: I had 1 file that I imported into my Projectscomponent, used .map() function to iterate and automatically render a ProjectElement component for each object in my array.

Now, I get the following error: TypeError: projectsList.map is not a function.

My projects.json file is structured as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[
{
"id": 1,
"title": "My project 1",
"isComplete": true,
"tasks": [
"task1",
"task2"
]
}
// rest of code
]
</code>
<code>[ { "id": 1, "title": "My project 1", "isComplete": true, "tasks": [ "task1", "task2" ] } // rest of code ] </code>
[
    {
      "id": 1,
      "title": "My project 1",
      "isComplete": true,
      "tasks": [
        "task1",
        "task2"
      ]
    }
// rest of code
]

My i18n.ts file is configured as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import translationEN from '../public/locales/en/translation.json';
import milestonesEN from './data/en/milestones.json';
import projectsEN from './data/en/projects.json';
import skillsEN from './data/en/skills.json';
import translationFR from '../public/locales/fr/translation.json';
import milestonesFR from './data/fr/milestones.json';
import projectsFR from './data/fr/projects.json';
import skillsFR from './data/fr/skills.json';
const resources = {
en: {
translation: translationEN,
milestones: milestonesEN,
projects: projectsEN,
skills: skillsEN,
},
fr: {
translation: translationFR,
milestones: milestonesFR,
projects: projectsFR,
skills: skillsFR,
},
};
i18n
.use(LanguageDetector)
.use(initReactI18next)
.init({
resources,
fallbackLng: 'en',
interpolation: {
escapeValue: false,
},
debug: true,
});
export default i18n;
</code>
<code>import i18n from 'i18next'; import { initReactI18next } from 'react-i18next'; import LanguageDetector from 'i18next-browser-languagedetector'; import translationEN from '../public/locales/en/translation.json'; import milestonesEN from './data/en/milestones.json'; import projectsEN from './data/en/projects.json'; import skillsEN from './data/en/skills.json'; import translationFR from '../public/locales/fr/translation.json'; import milestonesFR from './data/fr/milestones.json'; import projectsFR from './data/fr/projects.json'; import skillsFR from './data/fr/skills.json'; const resources = { en: { translation: translationEN, milestones: milestonesEN, projects: projectsEN, skills: skillsEN, }, fr: { translation: translationFR, milestones: milestonesFR, projects: projectsFR, skills: skillsFR, }, }; i18n .use(LanguageDetector) .use(initReactI18next) .init({ resources, fallbackLng: 'en', interpolation: { escapeValue: false, }, debug: true, }); export default i18n; </code>
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

import translationEN from '../public/locales/en/translation.json';
import milestonesEN from './data/en/milestones.json';
import projectsEN from './data/en/projects.json';
import skillsEN from './data/en/skills.json';

import translationFR from '../public/locales/fr/translation.json';
import milestonesFR from './data/fr/milestones.json';
import projectsFR from './data/fr/projects.json';
import skillsFR from './data/fr/skills.json';

const resources = {
  en: {
    translation: translationEN,
    milestones: milestonesEN,
    projects: projectsEN,
    skills: skillsEN,
  },
  fr: {
    translation: translationFR,
    milestones: milestonesFR,
    projects: projectsFR,
    skills: skillsFR,
  },
};

i18n
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    resources,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false,
    },
    debug: true,
  });

export default i18n;

And this is my Projects component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import ProjectElement from './ProjectElement';
import { Switch } from '../ui/switch';
import { Label } from '../ui/label';
import type { ProjectInterface } from '@/@types/dataTypes';
function Projects() {
const { t } = useTranslation(['translation', 'projects']);
const projectsList: ProjectInterface[] = t('projects', {
returnObjects: true,
});
// console.log('projects list:', projectsList) >>> returns: 'projects';
return (
<main className="projects-container">
<div className="projects-title">
<h2 className="page-title">{t('projects.title')}</h2>
</div>
<div className="projects-content">
<div className="projects-list">
{projectsList.map((project) => {
return <ProjectElement key={project.id} project={project} />;
)}
</div>
</div>
</main>
);
}
export default Projects;
</code>
<code> import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import ProjectElement from './ProjectElement'; import { Switch } from '../ui/switch'; import { Label } from '../ui/label'; import type { ProjectInterface } from '@/@types/dataTypes'; function Projects() { const { t } = useTranslation(['translation', 'projects']); const projectsList: ProjectInterface[] = t('projects', { returnObjects: true, }); // console.log('projects list:', projectsList) >>> returns: 'projects'; return ( <main className="projects-container"> <div className="projects-title"> <h2 className="page-title">{t('projects.title')}</h2> </div> <div className="projects-content"> <div className="projects-list"> {projectsList.map((project) => { return <ProjectElement key={project.id} project={project} />; )} </div> </div> </main> ); } export default Projects; </code>

import { useState } from 'react';
import { useTranslation } from 'react-i18next';

import ProjectElement from './ProjectElement';
import { Switch } from '../ui/switch';
import { Label } from '../ui/label';

import type { ProjectInterface } from '@/@types/dataTypes';

function Projects() {
  const { t } = useTranslation(['translation', 'projects']);

  const projectsList: ProjectInterface[] = t('projects', {
    returnObjects: true,
  });

  // console.log('projects list:', projectsList) >>> returns: 'projects';

  return (
    <main className="projects-container">
      <div className="projects-title">
        <h2 className="page-title">{t('projects.title')}</h2>
      </div>
      <div className="projects-content">
        <div className="projects-list">
          {projectsList.map((project) => {
                return <ProjectElement key={project.id} project={project} />;
              )}
        </div>
      </div>
    </main>
  );
}

export default Projects;

I was expecting my console.log to return an array, but it simply returns a string (hence, the errorTypeError: projectsList.map is not a function). The thing is, I tried to console.log my projectsEN data in the i18n.ts file and it worked. I probably have made a mistake in configuring and loading my namespaces or i18n, but I really do not understand where.

I went through the official documentation and several other resources, they did not help.

Do you have any clue about this issue?
Many thanks in advance for your help!

New contributor

NollieChtn6 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật