Tecnologia/Template-02/src/hooks/useI18n.ts

27 lines
720 B
TypeScript
Raw Normal View History

2026-05-13 22:24:26 +00:00
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,
};
}