JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Member-only story

Easy Localization in React: Switch Between German and Spanish

Aleksei Aleinikov
JavaScript in Plain English
5 min read2 days ago

We need to make our Next.js project multilingual, supporting and .

Users: ‘Why is my language still the same?

The plan:

  1. Use for fetching page content from the backend, with the desired language specified in request headers.
  2. Store key-value translations (e.g., button texts, navigation labels) in Redux Toolkit.
  3. Provide a simple toggle to switch between (Ger) and (Spa), and keep this preference in sessionStorage so it persists after page reloads.

1. Setting up Axios

First, configure an Axios instance to handle all API calls. For security, use environment variables for your base URL:

// src/store/apiInstance.ts
import axios from 'axios';

const URL = process.env.NEXT_PUBLIC_API_URL;

const instance = axios.create({
baseURL: `${URL}/api/`,
});

export default instance;

2. Creating GET Requests

Page-Specific Content (Example: “Profile” Page)

If you have a page called , set up a corresponding file apiProfile.ts:

// src/store/apiProfile.ts
import instance from './apiInstance';

const getProfileContent = async (lang: string) => {…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Written by Aleksei Aleinikov

Insights in AI, Python, JavaScript, React & iOS. Sharing IT news, data engineering tips & cloud solutions. Follow for deep dives into tech!

No responses yet

Write a response