Language change with JSON

Asked

Viewed 150 times

0

I am developing a website and it will have two languages. For this, I have set up a JSON that changes the languages by clicking on select box of the corresponding language. However, the code does not work and does not show any error in the console.

Follows the code:

var arrLang = {
  'en' : {
      'home' : 'Home',
      'sobre' : 'About us',
      'equipe' : 'Team',
      'ferramentas' : 'Tools',
      'exchanges' : 'Supported Exchanges',
      'contato' : 'Contact',
  },
  'pt' : {
      'home' : 'Home',
      'sobre' : 'Quem somos',
      'equipe' : 'Equipe',
      'ferramentas' : 'Ferramentas',
      'exchanges' : 'Exchanges Suportadas',
      'contato' : 'Contato',
  }
};

$(function(){
  $('#year').change(function(){
      var lang = $(this).attr('id');

      $('.lang').each(function(index, element){
          $(this).text(arrLang[lang][$(this).attr('key')]);
      });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="language">
  <select id="year">
      <option value="pt" id="pt">PT</option>
      <option value="en" id="en">EN</option>
  </select>
</div>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav ml-auto">
        <li class="nav-item active" data-menuanchor="home">
            <a class="nav-link lang" href="#home" key="home">Home</a>
        </li>

        <li class="nav-item"  data-menuanchor="quem-somos">
            <a class="nav-link lang" href="#quem-somos" key="sobre">Quem Somos</a>
        </li>

        <li class="nav-item"  data-menuanchor="equipe">
            <a class="nav-link lang" href="#equipe" key="equipe">Equipe</a>
        </li>

        <li class="nav-item"  data-menuanchor="ferramentas">
            <a class="nav-link lang" href="#ferramentas" key="ferramentas">Ferramentas</a>
        </li>

        <li class="nav-item"  data-menuanchor="exchanges">
            <a class="nav-link lang" href="#exchanges" key="exchanges">Exchanges Suportadas</a>
        </li>

        <li class="nav-item"  data-menuanchor="contato" key="contato">
            <a class="nav-link lang" href="#contato">Contato</a>
        </li>
    </ul>
</div>

1 answer

1


The problem lies in this line of your Javascript code:

var lang = $(this).attr('id');

To capture the value of a select at the event change jQuery, just use the $(this).val().

Therefore, the correct is:

var lang = $(this).val();

And the final code:

var arrLang = {
  'en' : {
      'home' : 'Home',
      'sobre' : 'About us',
      'equipe' : 'Team',
      'ferramentas' : 'Tools',
      'exchanges' : 'Supported Exchanges',
      'contato' : 'Contact',
  },
  'pt' : {
      'home' : 'Home',
      'sobre' : 'Quem somos',
      'equipe' : 'Equipe',
      'ferramentas' : 'Ferramentas',
      'exchanges' : 'Exchanges Suportadas',
      'contato' : 'Contato',
  }
};

$(function(){
  $('#year').change(function(){
      var lang = $(this).val();

      $('.lang').each(function(index, element){
          $(this).text(arrLang[lang][$(this).attr('key')]);
      });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="language">
  <select id="year">
      <option value="pt" id="pt">PT</option>
      <option value="en" id="en">EN</option>
  </select>
</div>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav ml-auto">
        <li class="nav-item active" data-menuanchor="home">
            <a class="nav-link lang" href="#home" key="home">Home</a>
        </li>

        <li class="nav-item"  data-menuanchor="quem-somos">
            <a class="nav-link lang" href="#quem-somos" key="sobre">Quem Somos</a>
        </li>

        <li class="nav-item"  data-menuanchor="equipe">
            <a class="nav-link lang" href="#equipe" key="equipe">Equipe</a>
        </li>

        <li class="nav-item"  data-menuanchor="ferramentas">
            <a class="nav-link lang" href="#ferramentas" key="ferramentas">Ferramentas</a>
        </li>

        <li class="nav-item"  data-menuanchor="exchanges">
            <a class="nav-link lang" href="#exchanges" key="exchanges">Exchanges Suportadas</a>
        </li>

        <li class="nav-item"  data-menuanchor="contato" key="contato">
            <a class="nav-link lang" href="#contato">Contato</a>
        </li>
    </ul>
</div>


Note in relation to attribute key

I wouldn’t use the attribute key to define the object key, since it is an invalid attribute according to the HTML5 specification. Instead of key, prefer to use data-key. :)

  • So putting the data-key changes something

  • 1

    Technically, yes, since the key is an invalid attribute and may cause errors while validating your website’s HTML. But as I explained in the answer, the error is not due to the attribute name. The data-key is preferable only as a good practice. :)

  • I put the data key on that line and I should change the key to the data-key $(this). text(arrLang[lang][$(this).attr('key')]); I made the change here and it didn’t work on my site quite strange that’s why I’m using the plugin called full-page js

  • 1

    You should switch to your HTML as well. Basically, instead of key, use data-key. And in Javascript, instead of capturing the attribute key, capture the attribute data-key. But I must reiterate that the code does not work is not due to this.

  • then it didn’t work but here your example works that strange this is the site if you can take a look only http://creativestudiodesigner.com.br/test/reactioon/

  • 1

    The problem is because you are using one select custom. If you remove the class select-hidden of <select> and changing the language directly there will work. However, this is no longer the scope of this question.

  • Thanks for the help I opened another topic related to this problem actually as I stylized the field it does not work opened here if you can help me there https://answall.com/questions/383187/problema-com-select-box-para-mudan%C3%A7a-de-lingual

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.