Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
Keeping selected language in dropdown after page reload using cookies

The problem is:

There is a landing page, a language switch added with ant design, specifically, through Select / Option. The translation was done via i18react lib. So, it is necessary that when the page is reloaded, the selected language is displayed in the dropdown itself, and not reset to the default Language. I know what can be done through document.cookie. But how? Well, I'll put languages ​​in cookies. And then, how to screw them into this Select so that the display of the selected language is saved on reboot?

My code is here and I don't know what to do.

const Option = Select.Option;
function ChangeLanguageDropdown({}) {
   const {i18n} = useTranslation();
   const changeLanguage = (lang) => {
       i18n.changeLanguage(lang);
   };

   Cookies.set('ru', 'Русский', { expires: 7 });
   Cookies.set('en', 'English', { expires: 7 });
   

   return (

       <div>
           <Select defaultValue={"Language"} style={{ width: 110}} onChange={changeLanguage}>
              <Option value="ru">{i18next.t("Русский")}</Option>
              <Option value="en">{i18next.t("English")}</Option>
           </Select>
               </div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

const {useState} = React
const { Select } = antd

const { Option } = Select;

const ChangeLanguageDropdown  = ({}) => {
   const [lang, setLang] = useState('en' /* Cookie.get('path.to.cookie') */);
   const changeLanguage = (lang) => {
     setLang(lang);
     /* Cookie.set('path.to.cookie', lang); */
     /* call i18n.changeLanguage(lang); */
   };

   return (
     <div><div>{lang}</div>
    <Select defaultValue={lang} style={{ width: 500}}  onChange={changeLanguage}>
     <Option value="ru">"Русский"</Option>
     <Option value="en">"English"</Option>
   </Select>
</div>);
}

ReactDOM.render(<ChangeLanguageDropdown />, document.body)

Note that I've removed all i18n related code as well as Cookie lib (your setup of Codepen was #R%T$). Next time, start with some example that actually works (e.g. https://codepen.io/mugiseyebrows/pen/ExyJJQQ?editors=1111)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda