Skip to content

Commit

Permalink
refactor: reduce coupling and magic strings
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyArt1 committed May 11, 2024
1 parent cfb01de commit 25a2272
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 42 deletions.
11 changes: 8 additions & 3 deletions src/app/[lang]/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use client';
import { createContext, useContext } from 'react';
import { createContext, use } from 'react';

import { getDictionary } from '@/dictionaries';
import { i18n } from '@/i18n-config';

type Props = {
children: React.ReactNode;
Expand All @@ -11,6 +12,7 @@ type Props = {
export function LanguageProvider({ children, intl }: Props) {
const contextValue = {
intl,
locales: i18n.locales,
};

return (
Expand All @@ -21,7 +23,7 @@ export function LanguageProvider({ children, intl }: Props) {
}

export function useLanguage() {
const context = useContext(LanguageContext);
const context = use(LanguageContext);

if (!context) {
throw new Error('useLanguage must be used within a LanguageProvider');
Expand All @@ -32,4 +34,7 @@ export function useLanguage() {

export const LanguageContext = createContext({} as Context);

export type Context = { intl: Props['intl'] };
export type Context = {
intl: Props['intl'];
locales: (typeof i18n)['locales'];
};
78 changes: 43 additions & 35 deletions src/components/layout/languageSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
import { useRouter } from 'next/navigation';
import Image from 'next/image';

import ESLanguage from '@public/assets/es-language.svg';
import ENLanguage from '@public/assets/en-language.svg';
import { useLanguage } from '@/app/[lang]/provider';
import { ButtonApp } from '../elements/button';
import esicon from '@public/assets/es-language.svg';
import enicon from '@public/assets/en-language.svg';
import esdata from '../../dictionaries/es.json';
import endata from '../../dictionaries/en.json';

export const LanguageSelector = () => {
const { intl } = useLanguage();
import { ButtonApp } from '@/components/elements/button';
import { Locale } from '@/i18n-config';

const handleLanguageChange = () => {
return window.location.href = `/${intl.language === 'en' ? 'es' : 'en'}/identification`;
};
const locales = {
es: {
icon: esicon.src,
name: esdata.languageName,
},
en: {
icon: enicon.src,
name: endata.languageName,
},
};

return (
<div
style={{ marginRight: '16px' }}
>
<ButtonApp
variant='outlined'
startIcon={
<Image
src={intl.language === 'en' ? ESLanguage.src : ENLanguage.src}
alt="icon language"
width="24"
height="24"
/>
}
onClick={() => handleLanguageChange()}
>
{intl.language === 'en'
?
'Español'
:
'English'
}
</ButtonApp>
</div>
);
}
export const LanguageSelector = ({ other }: { other: Locale }) => {
const router = useRouter();

const changeLanguage = () => {
router.push(`/${other}/identification`);
};

return (
<div style={{ marginRight: 16 }}>
<ButtonApp
variant="outlined"
startIcon={
<Image
src={locales[other].icon}
alt="language icon"
width="24"
height="24"
/>
}
onClick={changeLanguage}
>
{locales[other].name}
</ButtonApp>
</div>
);
};
8 changes: 5 additions & 3 deletions src/components/layout/navBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import Link from 'next/link';
import Logo from '@public/assets/logo.svg';
import styles from './styles.module.css';

import { LanguageSelector } from './languageSelector';
import { useLanguage } from '@/app/[lang]/provider';
import { ButtonApp } from '../elements/button';
import { LanguageSelector } from './languageSelector';

export default function Index() {
const { intl } = useLanguage();
const { intl, locales } = useLanguage();

return (
<>
Expand Down Expand Up @@ -42,7 +42,9 @@ export default function Index() {
color="primary"
style={{ display: 'none' }}
/>
<LanguageSelector />
<LanguageSelector
other={locales.find((l) => l !== intl.language)!}
/>
<ButtonApp
notFullWidth
onClick={() =>
Expand Down
1 change: 1 addition & 0 deletions src/dictionaries/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"languageName": "English",
"language": "en",
"common": {
"title": "Cuenta Única Ciudadana",
Expand Down
1 change: 1 addition & 0 deletions src/dictionaries/es.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"languageName": "Español",
"language": "es",
"common": {
"title": "Cuenta Única Ciudadana",
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/i18n.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ function getLocale(request: NextRequest): string | undefined {
let languages = new Negotiator({ headers }).languages(locales);

// return matchLocale(languages, locales, i18n.defaultLocale);
return 'es';
return i18n.defaultLocale;
}

0 comments on commit 25a2272

Please sign in to comment.