Я хочу изменить язык URL с помощью jquery

Я хочу изменить язык URL с помощью jquery. Мой URL-адрес https://beta.yourtaxi.ch/de_DE/about-us/ и я хочу изменить его, просто https://beta.yourtaxi.ch/en_Us/about-us/. Я пробовал код ниже, но не работает.

$('a[rel="group"]').on('click', function(e) {
  e.preventDefault();
  location.href = $(this).data('wpurl');
  $(this).attr('href', wpurl);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sidebar_lang">
  <span>Language:</span>
  <div class="lang_option">
    <a href="<?php echo site_url(); ?>/en_Us/" data-wpurl="<?php echo site_url(); ?>/de_DE/" class="lang_txt" rel="group">DE</a>
    <a href="<?php echo site_url(); ?>/de_DE/" data-wpurl="<?php echo site_url(); ?>/en_Us/" class="lang_txt" rel="group">EN</a>
  </div>
</div>

1 ответ

Вы можете получить текущий путь, а затем удалить первую часть, чтобы добавить к URL в вашем <a> теги.

Например

const appendPath = location.pathname // current full path, eg "/de_DE/about-us/"
    .split('/') // split on "/" -> ["", "de_DE", "about-us", ""]
    .slice(2) // omit the first two parts -> ["about-us", ""]
    .join('/') // join in to a slash-delimited string -> "about-us/"

location.href = $(this).data('wpurl') + appendPath
Другие вопросы по тегам