Skip to content

Commit

Permalink
Added dynamic imports to the translation.
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-mbn committed Jul 11, 2023
1 parent 3e17f1e commit fb734f2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/contexts/AppContext.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { createContext } from 'react';
import { DeviceEnum } from '../enums/Device';
import { LanguageEnum } from '../enums/Language';
import { EN_US } from '../languages/enUS';
import { TranslationType } from '../languages/enUS';

interface AppContextProps {
deviceInfos?: {
device: DeviceEnum;
bodyWidth?: number;
};
TRANSLATION: typeof EN_US;
TRANSLATION?: TranslationType;
language: LanguageEnum;
theme: 'dark' | 'light';
}

export const AppContext = createContext<AppContextProps>({
deviceInfos: { device: DeviceEnum.MOBILE },
TRANSLATION: EN_US,
TRANSLATION: undefined,
language: LanguageEnum.English,
theme: 'dark',
});
23 changes: 14 additions & 9 deletions src/hooks/useTranslation.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { LanguageEnum } from '../enums/Language';
import { DE_DE } from '../languages/deDE';
import { EN_US } from '../languages/enUS';
import { PT_BR } from '../languages/ptBR';
import EN_US, { TranslationType } from '../languages/enUS';

export default function useTranslation() {
const [language, setLanguage] = useState<LanguageEnum>(LanguageEnum.English);
const filePath =
'../languages/' +
{
[LanguageEnum.English]: 'enUS',
[LanguageEnum.German]: 'deDE',
[LanguageEnum.Portuguese]: 'ptBR',
}[language];

const TRANSLATION = {
[LanguageEnum.English]: EN_US,
[LanguageEnum.German]: DE_DE,
[LanguageEnum.Portuguese]: PT_BR,
}[language];
const [TRANSLATION, setTranslation] = useState<TranslationType>(EN_US);

useEffect(() => {
import(filePath).then(({ default: translation }) => setTranslation(translation));
}, [filePath]);

return { TRANSLATION, language, setLanguage };
}
5 changes: 4 additions & 1 deletion src/languages/deDE.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const DE_DE = {
const DE_DE = {
CETEM_DESCRIPTION_P1: `Entwicklung von einem Python-basierten Algorithmus für die Trennung der Metalle der Seltenen Erde.`,
CETEM_DESCRIPTION_P2: `Gewinn der Preis für die "Beste Studentischer Forscher Arbeit" der Institution in zwei konsekutiven Jahren.`,
CETEM: 'CETEM (Zentrum für Mineraltechnologien)',
Expand Down Expand Up @@ -35,3 +35,6 @@ export const DE_DE = {
VERSIONING: 'Versionierung',
WORK_EXPERIENCE: 'Berufserfahrung',
};

export default DE_DE;
console.log('de de was downloaded');
6 changes: 5 additions & 1 deletion src/languages/enUS.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const EN_US = {
const EN_US = {
CETEM_DESCRIPTION_P1: 'Developed a Python-based algorithm for rare-earth elements separation.',
CETEM_DESCRIPTION_P2: 'Won the Institution\'s "Best Undergraduate Research Award" for two years consecutively.',
CETEM: 'CETEM (Center for Mineral Technologies)',
Expand Down Expand Up @@ -35,3 +35,7 @@ export const EN_US = {
VERSIONING: 'Versioning',
WORK_EXPERIENCE: 'Work Experience',
};

export default EN_US;
export type TranslationType = typeof EN_US;
console.log('en us was downloaded');
5 changes: 4 additions & 1 deletion src/languages/ptBR.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const PT_BR = {
const PT_BR = {
CETEM_DESCRIPTION_P1: 'Desenvolvi um algoritmo em Python para a separação de elementos de terras-raras.',
CETEM_DESCRIPTION_P2: 'Ganhei o prêmio de "Destaque em Iniciação Científica/Tecnológica" dois anos consecutivos.',
CETEM: 'CETEM (Centro de Tecnologia Mineral)',
Expand Down Expand Up @@ -36,3 +36,6 @@ export const PT_BR = {
VERSIONING: 'Versionamento',
WORK_EXPERIENCE: 'Experiência',
};

export default PT_BR;
console.log('pt br was downloaded');

0 comments on commit fb734f2

Please sign in to comment.