refactor: divide language parts to separate folders
This commit is contained in:
parent
8f52988b6c
commit
6810f1c844
@ -1,16 +1,9 @@
|
||||
'use client';
|
||||
|
||||
import { Loader } from 'lucide-react';
|
||||
import {
|
||||
createContext,
|
||||
type ReactNode,
|
||||
useContext,
|
||||
useEffect,
|
||||
useState,
|
||||
} from 'react';
|
||||
import { createContext, type ReactNode, useEffect, useState } from 'react';
|
||||
|
||||
import enTranslations from './locales/en.json';
|
||||
import ruTranslations from './locales/ru.json';
|
||||
import enTranslations from '../locales/en.json';
|
||||
import ruTranslations from '../locales/ru.json';
|
||||
|
||||
// Define available languages
|
||||
export const languages = {
|
||||
@ -36,7 +29,7 @@ type LanguageContextType = {
|
||||
t: (key: string) => string;
|
||||
};
|
||||
|
||||
const LanguageContext = createContext<LanguageContextType | undefined>(
|
||||
export const LanguageContext = createContext<LanguageContextType | undefined>(
|
||||
undefined,
|
||||
);
|
||||
|
||||
@ -80,17 +73,3 @@ export function LanguageProvider({ children }: { children: ReactNode }) {
|
||||
</LanguageContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
// Create hook
|
||||
export function useLanguage() {
|
||||
const context = useContext(LanguageContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useLanguage must be used within a LanguageProvider');
|
||||
}
|
||||
|
||||
if (typeof context.t !== 'function') {
|
||||
throw new Error('Translation function (t) is not available');
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
18
src/shared/language/hooks/use-language.ts
Normal file
18
src/shared/language/hooks/use-language.ts
Normal file
@ -0,0 +1,18 @@
|
||||
'use client';
|
||||
|
||||
import { useContext } from 'react';
|
||||
|
||||
import { LanguageContext } from '../context/language-provider';
|
||||
|
||||
export function useLanguage() {
|
||||
const context = useContext(LanguageContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useLanguage must be used within a LanguageProvider');
|
||||
}
|
||||
|
||||
if (typeof context.t !== 'function') {
|
||||
throw new Error('Translation function (t) is not available');
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
6
src/shared/language/index.ts
Normal file
6
src/shared/language/index.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export {
|
||||
LanguageProvider,
|
||||
languages,
|
||||
type Language,
|
||||
} from './context/language-provider';
|
||||
export { useLanguage } from './hooks/use-language';
|
||||
@ -2,11 +2,11 @@
|
||||
|
||||
import { Provider } from 'react-redux';
|
||||
|
||||
import { LanguageProvider } from '../language';
|
||||
import { store } from '../store';
|
||||
import { ThemeProvider } from '../theme/theme-provider';
|
||||
import { AosProvider } from './aos-provider';
|
||||
import { Toaster } from './toaster';
|
||||
import { LanguageProvider } from '../language';
|
||||
|
||||
type ProvidersProps = {
|
||||
children: React.ReactNode;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user