Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
¿Cómo agregar dinámicamente el idioma a la URL en React usando i18next?

Necesito agregar idioma a la URL, es decir, así es como se ve la URL ahora http://localhost:3000/, http://localhost:3000/noticias, pero debe ser así http://localhost:3000/en , http://localhost:3000/en/noticias.

Al mismo tiempo, cuando alguien envía un enlace a este sitio, se debe tomar el idioma del enlace. Aquí está mi código. Los idiomas funcionan, cambia al hacer clic.

 import i18n from 'i18next' import HttpApi from 'i18next-http-backend' import LanguageDetector from 'i18next-browser-languagedetector' import { initReactI18next } from 'react-i18next' i18n .use(LanguageDetector) .use(initReactI18next) .use(HttpApi) .init({ supportedLngs: ['ru', 'en', 'kk'], fallbackLng: "en", detection: { order: ["cookie", "localStorage", "htmlTag", "path", "subdomain"], caches: ["cookie"], }, interpolation: { escapeValue: false } }); export default i18n;

Pero, ¿cómo agregar a la URL automáticamente? ¿Existen soluciones preparadas para esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

es posible obtener los parámetros de la URL con la ayuda del gancho useParams() disponible en react-routerv5.

Al definir la URL, debe agregar un param value de parámetro como este: <Route path="/:lang/news" component={News} /> donde :lang es el valor que recibirá de la URL.

Digamos que alguien ingresa yourapp.com/en/news , aquí el parámetro lang se cargará con el valor en . Entonces, ahora para extraer este valor, puede usar import { useParams } from "react-router-dom"; y luego dentro del componente de la función (ya que los ganchos funcionan solo dentro de los componentes funcionales): const { lang } = useParams(); donde lang es tu parámetro.
Después de esto, puede usar i18n para traducir según el idioma recibido.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!