Implementing Multi-Language Support in React Portfolios
Introduction
In the my-portfolio project, our goal was to make the hero section more accessible and engaging for a global audience. We recently implemented a translation framework to allow the hero section to display content in multiple languages dynamically.
The Implementation Approach
To handle translations in a React environment, we opted for a centralized translation provider pattern. By decoupling the static text from the UI components, we can easily inject language-specific strings without cluttering the component logic.
Here is how we structured the translation hook to supply the hero section text:
import { useTranslation } from 'react-i18next';
const HeroSection = () => {
const { t } = useTranslation();
return (
<section>
<h1>{t('hero.title')}</h1>
<p>{t('hero.description')}</p>
</section>
);
};
This pattern allows the HeroSection to remain agnostic of the specific language being rendered. By using a standard hook, the component simply requests the translated key, while the underlying provider handles the current language state and file loading.
Key Benefits
- Scalability: Adding a new language is now as simple as adding a new JSON file to our translation directory.
- Separation of Concerns: UI developers can focus on component structure, while copywriters can manage translations independently.
- Maintainability: Hardcoded strings are removed, reducing the risk of typos and making content updates seamless.
Actionable Takeaway
If you are managing a static portfolio, start by extracting all visible text into a centralized configuration object or JSON file. Once the strings are separated from the JSX, migrating to a full internationalization library like react-i18next becomes a straightforward task rather than a massive refactor.
Generated with Gitvlg.com