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>
So putting the data-key changes something
– Felipe Henrique
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. Thedata-key
is preferable only as a good practice. :)– Luiz Felipe
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
– Felipe Henrique
You should switch to your HTML as well. Basically, instead of
key
, usedata-key
. And in Javascript, instead of capturing the attributekey
, capture the attributedata-key
. But I must reiterate that the code does not work is not due to this.– Luiz Felipe
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/
– Felipe Henrique
The problem is because you are using one
select
custom. If you remove the classselect-hidden
of<select>
and changing the language directly there will work. However, this is no longer the scope of this question.– Luiz Felipe
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
– Felipe Henrique