Static HTML - localization using jquery.i18n.js not working - javascript

I've tried to make a simple HTML test where the content will be translated into specific languages using jquery.i18n.js library. It partially works meaning the default (English) text is being loaded, but the lang switch is not.
my jsfiddle is here:
https://jsfiddle.net/g8uwotsx/12/
HTML code:
<header>
<nav>
<ul class="switch-locale">
<li><a class="dropdown-item" href="#" data-locale="en">EN</a></li>
<li><a class="dropdown-item" href="#" data-locale="pl">PL</a></li>
<li><a class="dropdown-item" href="#" data-locale="fr">FR</a></li>
<li><a class="dropdown-item" href="#" data-locale="de">DE</a></li>
<li><a class="dropdown-item" href="#" data-locale="it">IT</a></li>
<li><a class="dropdown-item" href="#" data-locale="es">ES</a></li>
<li><a class="dropdown-item" href="#" data-locale="ru">RU</a></li>
</ul>
</nav>
</header>
<div class="container">
<div class="row">
<div class="col text-center">
<h1 data-i18n="app-title">placeholder</h1>
</div>
</div>
</div>
JS code:
jQuery(function() {
$.i18n().load( {
'en': {
'app-title': 'english headline'
},
'it': {
'app-title': 'italian headline'
},
'es': {
'app-title': 'spanish headline'
}
}).done(function() {
$('.switch-locale').on('click', 'a', function(e) {
e.preventDefault();
$.i18n().locale = $(this).data('locale');
});
});
$('body').i18n();
});
CSS code:
.switch-locale li {
display:inline-block;
list-style-type:none;
text-align:center;
}
.switch-locale {
text-align:center;
border-bottom:1px solid grey;
}
the inspiration was official documentation and this link:
https://phrase.com/blog/posts/jquery-i18n-the-advanced-guide/
expected behaviour would be:
by default user sees only English texts
after changing the language via menu the text will be replaced with a local one
in this example I'm using a solution without separate json files per lang (in the final one I would like to make it that way)
Issues:
text is being displayed only in English
text is not translating despite the clicking on different language

Related

how to run ajax from Drop down item in laravel

Good Morning,
I am new to coding and learning laravel for home project. I am trying to get data from database as per dropdown selection. The code for dropdown item is as below
<div class="dropdown">
<button class="btn p-0" type="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="bx bx-dots-vertical-rounded"></i>
</button>
<div id="milkstatementdropdown" name="milkstatementdropdown"class="dropdown-menu dropdown-menu- end" aria-labelledby="milkstatementdropdown">
<li><a class="dropdown-item" href="javascript:void(0);">Yesterday</a></li>
<li><a class="dropdown-item" href="javascript:void(0);">Last 7 Days</a></li>
<li><a class="dropdown-item" href="javascript:void(0);">Last Month</a></li>
<li><a class="dropdown-item" href="javascript:void(0);">Last 3 Month</a></li>
<li><a class="dropdown-item" href="javascript:void(0);">Last 6 Month</a></li>
</div>
</div>
`
After selection of drop down item, need to get value and then pass the same to controller using AJAX.
below is the script for getting the selection value and process ajax.
`
$("#milkstatementdropdown").change(function(){
var duration = $(this).val();
alert(course);
// ajax call
});
`
i am not able to get the Alert..
Did i miss something and make mistake in code...
Thanks in Advance.
try this
$('#id').on('select2:close', function () {
//do anything
});
Try this one // You can use Anything
$('#milkstatementdropdown li a').on('click', function(){
alert($(this).text());
//do anything
});
You have to use onClick beacuase your dropdown is not an Select Tag:-
<li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Yesterday')">Yesterday</a></li>
<li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last 7 Days')">Last 7 Days</a></li>
<li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last Month')">Last Month</a></li>
<li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last 3 Month')">Last 3 Month</a></li>
<li><a class="dropdown-item" href="javascript:void(0);" onclick="get_data('Last 6 Month')">Last 6 Month</a></li>
And then in your Javascript part you have make get_data function to hanle onClick event.
function get_data(duration){
// Now you can get data from controller according to received 'duration'
// ajax call
}

Bootstrap Navbar Toggler - Custom JS causes navbar menu to close on dropdown click

I'm new to JS and am in the process of learning while coding a website. I'm using a template that I found online with some custom modifications so I'm not super familiar with the JS used. Here is the issue.
While using the navbar toggler there is some custom JS to close the menu when the once a click happens in the menu. The issue is that I added a dropdown into the menu, and when it's clicked instead of showing the drop down details, the JS executes and closes the menu. here is the JS:
window.addEventListener('DOMContentLoaded', event => {
// Navbar shrink function
var navbarShrink = function () {
const navbarCollapsible = document.body.querySelector('#mainNav');
if (!navbarCollapsible) {
return;
}
if (window.scrollY === 0) {
navbarCollapsible.classList.remove('navbar-shrink')
} else {
navbarCollapsible.classList.add('navbar-shrink')
}
};
// Shrink the navbar
navbarShrink();
// Shrink the navbar when page is scrolled
document.addEventListener('scroll', navbarShrink);
// Activate Bootstrap scrollspy on the main nav element
const mainNav = document.body.querySelector('#mainNav');
if (mainNav) {
new bootstrap.ScrollSpy(document.body, {
target: '#mainNav',
offset: 74,
});
};
// Collapse responsive navbar when toggler is visible
const navbarToggler = document.body.querySelector('.navbar-toggler');
const responsiveNavItems = [].slice.call(
document.querySelectorAll('#navbarResponsive .nav-link')
);
responsiveNavItems.map(function (responsiveNavItem) {
responsiveNavItem.addEventListener('click', () => {
if (window.getComputedStyle(navbarToggler).display !== 'none') {
navbarToggler.click();
}
});
});
});
Only the bottom function is running the menu click/close function, but I provided all the code as for the custom menu actions as I'm not super familiar with JS. I understand, more or less, what it is doing, but I'm not sure how to modify it appropriately.
I'd imagine there is a simple way to modify that last "if" statement to check to see what was actually clicked (for example, if a .dropdown-menu was clicked). I just don't know how to use the code that is there and check what click caused the event trigger. I'd like to keep the close behavior when the .dropdown-items and other .nav-items are clicked. I just don't want the behavior when the .dropdown-menu is clicked.
Here is the html:
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav text-uppercase ms-auto py-4 py-lg-0">
<li class="nav-item"><a class="nav-link" href="#portfolio">Products</a></li>
<li class="nav-item"><a class="nav-link" href="#services">Services</a></li>
<li class="nav-item"><a class="nav-link" href="#how-to-order">How Order</a></li>
<li class="nav-item dropdown"><a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false">F.A.Q.</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="dropdownMenuButton2">
<li><a class="dropdown-item" href="#">Knowledge Base</a></li>
<li><hr class="dropdown-divider"></li>
<li><h4 class="dropdown-header">Most Common Questions:</h4></li>
<li><a class="dropdown-item" href="#">Where are you located</a></li>
<li><a class="dropdown-item" href="#">How long is shipping</a></li>
<li><a class="dropdown-item" href="#">How do I pay</a></li>
<li><hr class="dropdown-divider"></li>
</ul>
</li>
<li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
</ul>
</div>
I'm pretty sure this is a super simple JS problem that any coder worth his $.02 would know, but not knowing JS I don't even know where or what to search for. Any help?
The issue with your code is that when you are using querySelectorAll to access the nav-links the code is trying to access all the nav-links i.e., you are considering all the nav-links then the dropdown in your nav which is also a nav-link is also considered. So the better way to solve this is you can create a new class and attach it to the dropdown nav-link in your case F.A.Q and exclude that class from the function.
Your code:
<li class="nav-item dropdown"><a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false">F.A.Q.</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="dropdownMenuButton2">
<li><a class="dropdown-item" href="#">Knowledge Base</a></li>
<li><hr class="dropdown-divider"></li>
<li><h4 class="dropdown-header">Most Common Questions:</h4></li>
<li><a class="dropdown-item" href="#">Where are you located</a></li>
<li><a class="dropdown-item" href="#">How long is shipping</a></li>
<li><a class="dropdown-item" href="#">How do I pay</a></li>
<li><hr class="dropdown-divider"></li>
</ul>
</li>
const responsiveNavItems = [].slice.call(
document.querySelectorAll('#navbarResponsive .nav-link')
);
You can add a class called faq and exlude it from the querySelectorAll using :not()
<!-- added a new class faq to the dropdown link -->
<li class="nav-item dropdown"><a class="nav-link dropdown-toggle faq" data-bs-toggle="dropdown" role="button" aria-expanded="false">F.A.Q.</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="dropdownMenuButton2">
<li><a class="dropdown-item" href="#">Knowledge Base</a></li>
<li><hr class="dropdown-divider"></li>
<li><h4 class="dropdown-header">Most Common Questions:</h4></li>
<li><a class="dropdown-item" href="#">Where are you located</a></li>
<li><a class="dropdown-item" href="#">How long is shipping</a></li>
<li><a class="dropdown-item" href="#">How do I pay</a></li>
<li><hr class="dropdown-divider"></li>
</ul>
</li>
// select all links except faq class
const responsiveNavItems = [].slice.call(
document.querySelectorAll('#navbarResponsive a.nav-link:not(.faq)')
);
Just add the below scripts within your tags, preferably before the closing tag
<!-- JavaScript -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

Change language code within URL javascript/nodejs

I have common header file includes navbar in which have multilanguage dropdown.
when i select dropdown language, translates page without any issues.
But when i move to other pages, if should reflect the language selected on first page (eg) if th is selected on first page, other page should be /th instead of '/en'
currently common header file is always /en, when i chose dropdown how i update a href links javascript
need to update all href links when particular language is selected from dropdown.
//header.ejs
<body>
<nav>
<ul>
<li class="nav-item">
<a class="nav-link" href="/en/about" style="margin-right: 1.5rem!important;">about us</a>//change this link
</li>
<li class=" nav-item">
<a class="nav-link" href="/en/contact"
style="margin-right: 1.5rem!important;">contact us</a>
</li>//change this link
</ul>
<div class="dropdown">
<button id="language" class="btn btn-warning dropdown-toggle" type="button" id="dropdownMenu2"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="clickButton()">
English
</button>
<div id="languagelist" class="dropdown-menu" aria-labelledby="dropdownMenu2" onclick="clickItem(); return false">
<a class="dropdown-item" href="javascript:" onclick="setLanguage('en')">English</a>
<a class="dropdown-item" href="javascript:" onclick="setLanguage('th')">Thai</a>
</div>
</div>
</nav>
</body>
on load see if language is set or not in cookie.
set selected language in cookie on setLanguage call.
$(document).ready(function() {
//read previously set cookie value
var selectedLanguage = $.cookie( 'selectedLanguage' );
// 1. on load see if language is set or not in cookie.
selectedLanguage = selectedLanguage ? selectedLanguage : 'en';
setLanguage(selectedLanguage);
function setLanguage(lan){
$.cookie('selectedLanguage', lan);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js" integrity="sha256-1A78rJEdiWTzco6qdn3igTBv9VupN3Q1ozZNTR4WE/Y=" crossorigin="anonymous"></script>

jquery toggle between 2 multiple classes

I'm trying to toggle between 2 multiple classes in jquery. I want it to change on click from an own icon to a font-awesome icon. It works with only one class to change however when i want it to change multiple classes i can't get it to work.
jQuery(document).ready(function () {
var $menu = 'icon '+'icon-menu';
var $close = 'fa '+'fa-close';
$('.icon-toggle-menu').click(function (evt) {
$('.nav-screen').fadeToggle();
$($menu, $close).toggleClass($menu +' '+ $close);
$('.nav-logo-black').fadeToggle();
$('.nav-icons').toggleClass('nav-fixed');
$('.nav').toggleClass('nav-padding');
})
});
<nav class="nav">
<a class="nav-logo-black" href="index.html">
<img src="assets/img/logo/forcitx2.png" alt="forcit-logo-banner">
</a>
<ul class="pull-right nav-icons">
<li><a class="icon-chat" href="#"><span class="icon icon-message"></span></a></li>
<li><a class="icon-toggle-menu" href="#"><span class="icon icon-menu"></span></a></li>
</ul>
</nav>
Okay, I'm going to do an attempt at fixing your code. Even though it's not fully clear what you're aiming for:
$(function() {
$('.icon-toggle-menu').click(function(evt) {
//$('.nav-screen').fadeToggle(); Missing in HTML
$('.icon.icon-menu, .fa.fa-close').toggleClass('icon icon-menu fa fa-close');
$('.nav-logo-black').fadeToggle();
$('.nav-icons').toggleClass('nav-fixed');
$('.nav').toggleClass('nav-padding');
})
});
.icon {
font-weight: bold;
}
.fa.fa-close {
color: red;
}
.icon.icon-menu {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="nav">
<a class="nav-logo-black" href="index.html">
Logo
</a>
<ul class="pull-right nav-icons">
<li><a class="icon-chat" href="#"><span class="icon icon-message">Icon chat</span></a></li>
<li><a class="icon-toggle-menu" href="#"><span class="icon icon-menu">Icon toggle menu (click me)</span></a></li>
</ul>
</nav>

Bootstrap Pagination is removing href, making links unclickable

I have setup my pagination exactly how bootstrap pagination examples are given. Everything works except the links. Apparently the Jquery script removes the href="link" from the <a> tag.
This is what my php script generates in the html without the pagination script
<div id="alphabet">
<ul>
<li>All</li>
<li><a href="/retailers.php?letter=0-9" >0-9</a></li>
<li><a href="/retailers.php?letter=A" >A</a></li>
<li><a href="/retailers.php?letter=B" >B</a></li> </ul>
</div>
This is what the pagination script does to my pagination links, as you can see it takes the links out:
<div id="alphabet" class="pagination">
<ul>
<li><a data-original-title="" title=""><<</a></li>
<li><a data-original-title="" title=""><</a></li>
<li><a data-original-title="" title="">1</a></li>
<li><a data-original-title="" title="">2</a></li>
<li class="active"><a data-original-title="" title="">3</a></li>
<li><a data-original-title="" title="">4</a></li>
<li><a data-original-title="" title="">5</a></li>
<li><a data-original-title="" title="">></a></li>
<li><a data-original-title="" title="">>></a></li>
</ul></div>
This is a screenshot of the my pagination with the script, as you can see it displays them exactly like the examples on the pagination site:
This is the options javascript I am using:
var options = {
currentPage: 3,
totalPages: 10,
useBootstrapTooltip:true
bootstrapTooltipOptions: {
html: true,
placement: 'bottom'
}
}
$('#alphabet').bootstrapPaginator(options);
pageUrl: function(type, page, current){
return "http://example.com/list/page/"+page;
}
I think you should adapt the code to get the letter instead of the page; you probably can do that with data-attributes.

Categories