treckstar

Localization & i18n support for next export | TypeScript Context for Locale Switchers that are SSG

May 25th, 2023
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | Source Code | 0 0
  1. To create a context that keeps track of the current locale of the website in a Next.js site using TypeScript and class-based paradigm, you can follow these steps:
  2.  
  3. 1. First, create a file named `LocaleContext.tsx` in your project's `contexts` directory. This file will define the context and its provider component.
  4.  
  5. ```typescript
  6. // contexts/LocaleContext.tsx
  7. import React, { createContext, useState } from 'react';
  8.  
  9. export type Locale = 'en' | 'es' | 'fr';
  10.  
  11. type LocaleContextValue = {
  12. locale: Locale;
  13. setLocale: (locale: Locale) => void;
  14. };
  15.  
  16. export const LocaleContext = createContext<LocaleContextValue | undefined>(undefined);
  17.  
  18. export const LocaleProvider: React.FC = ({ children }) => {
  19. const [locale, setLocale] = useState<Locale>('en');
  20.  
  21. return (
  22. <LocaleContext.Provider value={{ locale, setLocale }}>
  23. {children}
  24. </LocaleContext.Provider>
  25. );
  26. };
  27. ```
  28.  
  29. 2. Next, create a custom `_app.tsx` file in your project's `pages` directory. This file will wrap your application with the `LocaleProvider` to make the context available to all components.
  30.  
  31. ```typescript
  32. // pages/_app.tsx
  33. import React from 'react';
  34. import { AppProps } from 'next/app';
  35. import { LocaleProvider } from '../contexts/LocaleContext';
  36.  
  37. const MyApp: React.FC<AppProps> = ({ Component, pageProps }) => {
  38. return (
  39. <LocaleProvider>
  40. <Component {...pageProps} />
  41. </LocaleProvider>
  42. );
  43. };
  44.  
  45. export default MyApp;
  46. ```
  47.  
  48. 3. Now, in your dynamic route file `[[...slug]].tsx`, you can access the `locale` value from the `LocaleContext` and use it as needed.
  49.  
  50. ```typescript
  51. // pages/[lang]/[[...slug]].tsx
  52. import { useContext } from 'react';
  53. import { LocaleContext, Locale } from '../../contexts/LocaleContext';
  54.  
  55. const DynamicPage: React.FC = () => {
  56. const { locale } = useContext(LocaleContext);
  57.  
  58. // Use the locale value to render content based on the current locale
  59.  
  60. return (
  61. <div>
  62. <h1>{`Current Locale: ${locale}`}</h1>
  63. {/* Rest of the page content */}
  64. </div>
  65. );
  66. };
  67.  
  68. export default DynamicPage;
  69. ```
  70.  
  71. 4. Finally, create a language picker component that allows the user to change the current locale by updating the context value.
  72.  
  73. ```typescript
  74. // components/LanguagePicker.tsx
  75. import React, { useContext } from 'react';
  76. import { LocaleContext, Locale } from '../contexts/LocaleContext';
  77.  
  78. const LanguagePicker: React.FC = () => {
  79. const { locale, setLocale } = useContext(LocaleContext);
  80.  
  81. const handleLocaleChange = (selectedLocale: Locale) => {
  82. setLocale(selectedLocale);
  83. };
  84.  
  85. return (
  86. <div>
  87. <button onClick={() => handleLocaleChange('en')}>English</button>
  88. <button onClick={() => handleLocaleChange('es')}>Spanish</button>
  89. <button onClick={() => handleLocaleChange('fr')}>French</button>
  90. <p>Current Locale: {locale}</p>
  91. </div>
  92. );
  93. };
  94.  
  95. export default LanguagePicker;
  96. ```
  97.  
  98. Now, you can use the `LanguagePicker` component wherever you need a language picker UI in your Next.js application. The selected locale will be stored and managed by the `LocaleContext`, and the dynamic route component `[[...slug]].tsx` can access the current locale from the context to render content based on the selected language.
Advertisement
Add Comment
Please, Sign In to add comment