27 lines
720 B
TypeScript
27 lines
720 B
TypeScript
|
|
import { useLocation, useNavigate, useParams } from 'react-router-dom';
|
||
|
|
import { content } from '../content';
|
||
|
|
import { Language } from '../types';
|
||
|
|
|
||
|
|
export function useI18n() {
|
||
|
|
const { lang } = useParams<{ lang?: string }>();
|
||
|
|
const location = useLocation();
|
||
|
|
const navigate = useNavigate();
|
||
|
|
|
||
|
|
const currentLang: Language = (lang as Language) || 'pt';
|
||
|
|
const t = content.ui[currentLang];
|
||
|
|
const posts = content.posts[currentLang];
|
||
|
|
|
||
|
|
const changeLanguage = (newLang: Language) => {
|
||
|
|
const pathParts = location.pathname.split('/');
|
||
|
|
pathParts[1] = newLang; // Replace the language segment
|
||
|
|
navigate(pathParts.join('/'));
|
||
|
|
};
|
||
|
|
|
||
|
|
return {
|
||
|
|
lang: currentLang,
|
||
|
|
t,
|
||
|
|
posts,
|
||
|
|
changeLanguage,
|
||
|
|
};
|
||
|
|
}
|