我正在为我的公司做一个网站--我最近在我的网站上添加了一个语言转换程序,但是现在我的问题是,它不会自动更改上一页所选择的语言--我试着查找一些信息,但是我唯一得到的是关于cookie的东西,但是我不知道如何将cookie添加到我的网站,因为我对所有编码都不熟悉。
我的网站:nl.profskyshot.com
我的守则:
语言开关:
//Netherlands
$(function() {
$(".nl").click(function() {
// Language Select
$(".language-selected").text("nl-NL");
$(".language-selected").removeClass("change-en");
$(".language-selected").addClass("change-nl");
// Navbar
$("#navbar-over-ons").text("Over Ons");
$("#navbar-winkel").text("Winkel");
$("#navbar-beoordelingen").text("Beoordelingen");
$("#navbar-contact").text("Contact");
$("#navbar-portfolio").text("Portfolio");
$("#navbar-winkelwagen").text("Winkelwagen");
// Footer
$("#footer-diensten").text("Diensten");
$("#footer-over").text("Over");
$("#footer-overig").text("Overig");
$("#footer-winkel").text("Winkel");
$("#footer-over-ons").text("Over Ons");
$("#footer-beoordelingen").text("Beoordelingen");
$("#footer-contact").text("Contact");
$("#footer-portfolio").text("Portfolio");
$("#footer-changelog").text("Changelog");
// Index
$("#index-producten").text("Producten");
$("#index-keuzes").text("U kunt kiezen uit deze 4 keuzes");
$("#index-air-photo").text("Lucht Foto");
$("#index-air-video").text("Lucht Video");
$("#index-air-combination").text("Lucht Combinatie");
$("#index-complete-package").text("Complete Pakket");
$("#index-air-photo-paragraph").text("We maken meerdere foto's waaruit u kunt kiezen, in verschillende hoeken en hoogtes.");
$("#index-air-video-paragraph").text("Wij maken een video in verschillende hoeken, hoogtes en bereik van uw wensen.");
$("#index-air-combination-paragraph").text("Wij maken een combinatie van video's en foto's.");
$("#index-complete-package-paragraph").text("Wij maken meerdere foto's en video's voor bijvoorbeeld bruiloften en zijn in te huren voor langere tijd.");
$("#index-lees-verder").text("Lees Verder»");
$("#index-faq").text("FAQ");
$("#index-faq-v1").text("Als ik iets haal, Hoe kan ik dan betalen?");
$("#index-faq-v2").text("Waar kan ik met vragen terecht?");
$("#index-faq-v3").text("Als ik niet tevreden ben, kan ik dan een refund krijgen?");
$("#index-faq-v4").text("Als ik iets haal kan ik dan annuleren?");
$("#index-faq-v5").text("Wat als het slecht weer is, krijg ik dan regen foto's?");
$("#index-faq-a1").text("U moet gelijk betalen via onze site tijdens het bestellen van een van onze diensten.");
$("#index-faq-a2").text("U kunt ons contacteren via het kopje 'Contact' Op onze site voor enige vragen over onze diensten.");
$("#index-faq-a3").text("U kan geen refund krijgen, We kunnen u wel korting geven op uw volgende bestelling bij Prof Sky Shot.");
$("#index-faq-a4").text("U kan uw dienst annuleren, U moet dit dan wel 24 uur van te voren doen. Of u kan een andere tijd afspreken met onze medewerkers, Dit moet wel 24 uur van te voren gedaan worden. Dit kan allemaal gedaan worden via onze 'Contact' pagina op onze site.");
$("#index-faq-a5").text("Met slecht weer word er een andere tijd afgesproken met u en onze medewerkers, Tenzij het een van onze speciale diensten zijn zoals de 'Complete Package'. Dan gaan we verder in de regen.");
// Changelog
$("#website-changelog").text("Website Changelog");
$("#changelog-pagina").text("Pagina");
$("#changelog-tijd").text("Tijd");
$("#changelog-toegevoegt").text("Toegevoegt");
$("#changelog-Bug-Fix").text("Bug-Fix");
$("#changelog-aangepast-toegevoegt").text("Aangepast-Toegevoegt");
$("#changelog-item-pagina").text("Changelog Pagina");
$("#niet-klaar-popup").text("Niet Klaar Popup");
$("#aparthotel-las-mariposas-features-lees-meer-knop").text("Aparthotel Las Mariposas Features Lees Meer Knop");
$("#pagina-kopje").text("Pagina Kopje");
// 404
$("#404-pagina-niet-gevonden").text("Pagina niet gevonden!");
$("#404-pagina-niet-gevonden-paragraph").text("Deze pagina is niet gevonden, druk op de knop hieronder om terug te gaan naar de vorige pagina.");
$("#404-ga-terug").text("Ga Terug!");
// Aparthotel Las Mariposas
$("#aparthotel-las-mariposas-features").text("Features");
$("#aparthotel-las-mariposas-features-klik-hier").text("Klik Hier");
$("#aparthotel-las-mariposas-apparatuur").text("Apparatuur");
$("#aparthotel-las-mariposas-locatie").text("Locatie");
$("#aparthotel-las-mariposas-contact").text("Contact");
$("#aparthotel-las-mariposas-over-ons").text("Over Ons");
$("#aparthotel-las-mariposas-apparatuur-paragraph").text("Elke appartement is in bezit van een: Kluis, Koelkast, Magnetron, Broodrooster, Waterkoker, Eigen badkamer, Nieuwe matras, 90X200 Bedden, Terras met tafel met stoelen, Satelliet/Kleuren TV, Eettafel en stoelen, Volledig ingerichte kitchenette, Bed- en badlinnen aanwezig, Comfortabel loungemeubilair.");
$("#aparthotel-las-mariposas-locatie-paragraph").text("Het hotel zit dichtbij het strand, er zit een tankstation dichtbij, een ziekenhuis een paar restaurants en een paar winkels.");
$("#aparthotel-las-mariposas-contact-paragraph").text("U kunt ons contacteren op het Telefoon Nummer:");
$("#aparthotel-las-mariposas-contact-paragraph-email").text("Of E-Mail Adres:");
// Cancel
$("#cancel-betaling-mislukt").text("Betaling Mislukt!");
$("#cancel-betaling-komt-niet-door").text("De betaling kon niet door komen, neem a.u.b. contact met ons op!");
// Contact
$("#contact-contacteer-ons").text("Contacteer Ons");
$("#contact-faq").text("FAQ");
$("#contact-faq-v1").text("Als ik iets haal, Hoe kan ik dan betalen?");
$("#contact-faq-v2").text("Waar kan ik met vragen terecht?");
$("#contact-faq-v3").text("Als ik niet tevreden ben, kan ik dan een refund krijgen?");
$("#contact-faq-v4").text("Als ik iets haal kan ik dan annuleren?");
$("#contact-faq-v5").text("Wat als het slecht weer is, krijg ik dan regen foto's?");
$("#contact-faq-a1").text("U moet gelijk betalen via onze site tijdens het bestellen van een van onze diensten.");
$("#contact-faq-a2").text("U kunt ons contacteren via het kopje 'Contact' Op onze site voor enige vragen over onze diensten.");
$("#contact-faq-a3").text("U kan geen refund krijgen, We kunnen u wel korting geven op uw volgende bestelling bij Prof Sky Shot.");
$("#contact-faq-a4").text("U kan uw dienst annuleren, U moet dit dan wel 24 uur van te voren doen. Of u kan een andere tijd afspreken met onze medewerkers, Dit moet wel 24 uur van te voren gedaan worden. Dit kan allemaal gedaan worden via onze 'Contact' pagina op onze site.");
$("#contact-faq-a5").text("Met slecht weer word er een andere tijd afgesproken met u en onze medewerkers, Tenzij het een van onze speciale diensten zijn zoals de 'Complete Package'. Dan gaan we verder in de regen.");
// Order ID Template
$("#order-order-id").text("Order#ID");
$("#order-order-id-paragraph").text("Hier kunt u uw foto's en video's downloaden");
$("#order-disclaimer").text("DISCLAIMER: Alles word na 48 uur verwijdert!");
// Order Status
$("#orderstatus-order-details").text("Uw Bestellings Gegevens!");
// Over Ons
$("#over-ons-ons-bedrijf").text("Ons Bedrijf");
$("#over-ons-ons-bedrijf-paragraph").text("Wij zijn een startend bedrijf, ik heb zelf een passie voor het vliegen met drones en fotografie, toen ik op vakantie was kon ik de omgeving van het hotel waar ik heen ging niet vinden en daarom kwam ik op het idee om dit bedrijf te starten. Ik zou graag hun belangrijke momenten voor mensen vastleggen en zelf wat zakgeld verdienen, ik heb net een dronepilootlicentie gekregen om legaal te kunnen vliegen, ik heb ook een kamer van koophandel gekocht. We gebruiken een professionele drone, we maken foto's en video's in 4K-kwaliteit, we hebben een zeer groot vlieggebied zodat we zeer mooie omgevingsfilms kunnen maken, zonder schokken en schieten met een zeer goede beeldkwaliteit.");
// Portfolio
$("#portfolio-onze-portfolio").text("Onze Portfolio");
$("#portfolio-onze-portfolio-paragraph").text("Dit is onze portfolio");
// Reviews
$("#beoordelingen-beoordelingen").text("Beoordelingen");
$("#beoordelingen-nog-geen-informatie").text("Nog Geen Informatie");
$("#beoordelingen-niemand").text("NIEMAND");
$("#beoordelingen-beoordelingen-paragraph").text("Lees hier want onze klanten over ons vinden.");
$("#beoordelingen-niemand-paragraph").text("NIEMAND");
// Success
$("#success-bedankt").text("Bedankt voor uw bestelling!");
$("#success-bedankt-paragraph").text("Check A.U.B uw mailbox voor uw bestelling confirmatie!");
});
});
// English
$(function() {
$(".en").click(function() {
// Language Select
$(".language-selected").text("en-US");
$(".language-selected").removeClass("change-nl");
$(".language-selected").addClass("change-en");
// Navbar
$("#navbar-over-ons").text("About Us");
$("#navbar-winkel").text("Store");
$("#navbar-beoordelingen").text("Reviews");
$("#navbar-contact").text("Contact");
$("#navbar-portfolio").text("Portfolio");
$("#navbar-winkelwagen").text("Cart");
// Footer
$("#footer-diensten").text("Services");
$("#footer-over").text("About");
$("#footer-overig").text("Other");
// Items
$("#footer-winkel").text("Store");
$("#footer-over-ons").text("About Us");
$("#footer-beoordelingen").text("Reviews");
$("#footer-contact").text("Contact");
$("#footer-portfolio").text("Portfolio");
$("#footer-changelog").text("Changelog");
// Index
$("#index-producten").text("Products");
$("#index-keuzes").text("You can choose from these 4 choices");
$("#index-air-photo").text("Air Photo");
$("#index-air-video").text("Air Video");
$("#index-air-combination").text("Air Combination");
$("#index-complete-package").text("Complete Package");
$("#index-air-photo-paragraph").text("We take multiple photos for you to choose from, at different angles and heights.");
$("#index-air-video-paragraph").text("We make a video in different angles, heights and range of your wishes.");
$("#index-air-combination-paragraph").text("We make a combination of videos and photos.");
$("#index-complete-package-paragraph").text("We take several photos and videos for, for example, weddings and can be hired for a longer period of time.");
$("#index-lees-verder").text("Read More»");
$("#index-faq").text("FAQ");
$("#index-faq-v1").text("If I get something, how can I pay?");
$("#index-faq-v2").text("Where can I go with questions?");
$("#index-faq-v3").text("If I am not satisfied, can I get a refund?");
$("#index-faq-v4").text("Can I cancel if I get something?");
$("#index-faq-v5").text("What if the weather is bad, will I get rain photos?");
$("#index-faq-a1").text("You must pay directly through our site when ordering one of our services.");
$("#index-faq-a2").text("You can contact us via the 'Contact' section on our site for any questions about our services.");
$("#index-faq-a3").text("You cannot get a refund, but we can give you a discount on your next order at Prof Sky Shot.");
$("#index-faq-a4").text("You can cancel your service, you must do this 24 hours in advance. Or you can arrange a different time with our employees. This must be done 24 hours in advance. This can all be done through our 'Contact' page on our site.");
$("#index-faq-a5").text("In bad weather, a different time will be agreed with you and our employees, unless it is one of our special services such as the 'Complete Package'. Then we continue in the rain.");
// Changelog
$("#website-changelog").text("Website Changelog");
$("#changelog-pagina").text("Page");
$("#changelog-tijd").text("Time");
$("#changelog-toegevoegt").text("Added");
$("#changelog-Bug-Fix").text("Bug-Fix");
$("#changelog-aangepast-toegevoegt").text("Changed-Added");
$("#changelog-item-pagina").text("Changelog Page");
$("#niet-klaar-popup").text("Not Finished Popup");
$("#aparthotel-las-mariposas-features-lees-meer-knop").text("Aparthotel Las Mariposas Features Read More Button");
$("#pagina-kopje").text("Pagina Header");
// 404
$("#404-pagina-niet-gevonden").text("Page not found!");
$("#404-pagina-niet-gevonden-paragraph").text("This page was not found, press the button below to go back to the previous page.");
$("#404-ga-terug").text("Go Back!");
// Aparthotel Las Mariposas
$("#aparthotel-las-mariposas-features").text("Features");
$("#aparthotel-las-mariposas-features-klik-hier").text("Click Here");
$("#aparthotel-las-mariposas-apparatuur").text("Equipment");
$("#aparthotel-las-mariposas-locatie").text("Location");
$("#aparthotel-las-mariposas-contact").text("Contact");
$("#aparthotel-las-mariposas-over-ons").text("About Us");
$("#aparthotel-las-mariposas-apparatuur-paragraph").text("Each apartment has a: Safe, Fridge, Microwave, Toaster, Kettle, Private bathroom, New mattress, 90X200 Beds, Terrace with table with chairs, Satellite / Color TV, Dining table and chairs, Fully equipped kitchenette, Bed and bath linen present, Comfortable lounge furniture.");
$("#aparthotel-las-mariposas-locatie-paragraph").text("The hotel is close to the beach, there is a gas station nearby, a hospital, a few restaurants and a few shops.");
$("#aparthotel-las-mariposas-contact-paragraph").text("You can contact us at the Phone Number:");
$("#aparthotel-las-mariposas-contact-paragraph-email").text("Or E-mail Address:");
// Cancel
$("#cancel-betaling-mislukt").text("Payment Failed!");
$("#cancel-betaling-komt-niet-door").text("The payment could not go through, please contact us!");
// Contact
$("#contact-contacteer-ons").text("Contact Us");
$("#contact-faq").text("FAQ");
$("#contact-faq-v1").text("If I get something, how can I pay?");
$("#contact-faq-v2").text("Where can I go with questions?");
$("#contact-faq-v3").text("If I am not satisfied, can I get a refund?");
$("#contact-faq-v4").text("Can I cancel if I get something?");
$("#contact-faq-v5").text("What if the weather is bad, will I get rain photos?");
$("#contact-faq-a1").text("You must pay directly through our site when ordering one of our services.");
$("#contact-faq-a2").text("You can contact us via the 'Contact' section on our site for any questions about our services.");
$("#contact-faq-a3").text("You cannot get a refund, but we can give you a discount on your next order at Prof Sky Shot.");
$("#contact-faq-a4").text("You can cancel your service, you must do this 24 hours in advance. Or you can arrange a different time with our employees. This must be done 24 hours in advance. This can all be done through our 'Contact' page on our site.");
$("#contact-faq-a5").text("In bad weather, a different time will be agreed with you and our employees, unless it is one of our special services such as the 'Complete Package'. Then we continue in the rain.");
// Order ID Template
$("#order-order-id").text("Order#ID");
$("#order-order-id-paragraph").text("Here you can download your photos and videos");
$("#order-disclaimer").text("DISCLAIMER: Everything will be deleted after 48 hours!");
// Order Status
$("#orderstatus-order-details").text("Your Order Details!");
// Over Ons
$("#over-ons-ons-bedrijf").text("Our Company");
$("#over-ons-ons-bedrijf-paragraph").text("We are a start-up company, I myself have a passion for flying drones and photography, when I was on vacation I couldn't find the area around the hotel I was going to and that's why I came up with the idea to start this company. I would like to capture their important moments in front of people and earn some pocket money myself, I just got a drone pilot license to fly legally, I also bought a chamber of commerce. We use a professional drone, we take pictures and videos in 4K quality, we have a very large flying area so that we can make very nice environmental movies, without jerking and shooting with very good image quality.");
// Portfolio
$("#portfolio-onze-portfolio").text("Our Portfolio");
$("#portfolio-onze-portfolio-paragraph").text("This is our portfolio");
// Reviews
$("#beoordelingen-beoordelingen").text("Reviews");
$("#beoordelingen-nog-geen-informatie").text("No Information Yet");
$("#beoordelingen-niemand").text("NOBODY");
$("#beoordelingen-beoordelingen-paragraph").text("Read here because our customers think about us.");
$("#beoordelingen-niemand-paragraph").text("NIEMAND");
// Success
$("#success-bedankt").text("Thank you for your order!");
$("#success-bedankt-paragraph").text("Please check your mailbox for your order confirmation!");
});
});a {
text-decoration: none;
}
* {
margin: 0;
padding: 0;
}
.language {
position: relative;
width: 120px;
font-weight: bold;
}
.language-selected {
display: flex;
align-items: center;
color: #fff;
cursor: pointer;
}
.language-selected:before {
content: '';
display: inline-block;
width: 30px;
height: 30px;
background-image: url(https://cdn-icons-png.flaticon.com/512/197/197441.png);
background-size: contain;
background-repeat: no-repeat;
margin-right: 10px;
}
.language-selected.change-nl:before {
background-image: url(https://cdn-icons-png.flaticon.com/512/197/197441.png);
}
.language-selected.change-en:before {
background-image: url(https://cdn-icons-png.flaticon.com/512/197/197484.png);
}
.language ul {
position: absolute;
width: 140px;
background-color: #fff;
border: 1px solid #f8f8f8;
padding-top: 10px;
box-shadow: 0px 1px 10px rgba(0, 0, 0, 0.5);
display: none;
}
.language ul li {
list-style: none;
}
.language ul li a {
display: block;
color: #252525;
padding: 5px 10px;
}
.language ul li:hover {
background-color: #f8f8f8;
}
.language ul li a:before {
content: '';
display: inline-block;
width: 25px;
height: 25px;
background-size: contain;
background-repeat: no-repeat;
vertical-align: middle;
margin-right: 10px;
}
a.nl:before {
background-image: url(https://cdn-icons-png.flaticon.com/512/197/197441.png);
}
a.en:before {
background-image: url(https://cdn-icons-png.flaticon.com/512/197/197484.png);
}
.language:hover ul {
display: block;
}<div class="language">
<div class="language-selected">
<div id="nl-NL">
nl-NL
</div>
</div>
<ul>
<li><a class="nl" href="#">Netherlands</a></li>
<li><a class="en" href="#">English</a></li>
</ul>
</div>
发布于 2022-06-27 18:34:50
我有个朋友看了我的代码,他把它修好了
let activeLanguage = window.localStorage.getItem('language');
if(activeLanguage == "nl") {
changeNL()
} else if(activeLanguage == "en") {
changeEN()
} else {
window.localStorage.setItem('language', 'nl');
changeNL()
}
// Dutch
async function changeNL() {
window.localStorage.setItem('language', 'nl');
// Language Select
document.getElementsByClassName("language-selected")[0].textContent = "nl-NL"
document.getElementsByClassName("language-selected")[0].classList.remove("change-en")
document.getElementsByClassName("language-selected")[0].classList.add("change-nl")
// Navbar
if(document.getElementById("navbar-over-ons")) document.getElementById("navbar-over-ons").textContent = "Over Ons"
};
// English
async function changeEN() {
window.localStorage.setItem('language', 'en');
// Language Select
document.getElementsByClassName("language-selected")[0].textContent = "en-US"
document.getElementsByClassName("language-selected")[0].classList.remove("change-nl")
document.getElementsByClassName("language-selected")[0].classList.add("change-en")
// Navbar
if(document.getElementById("navbar-over-ons")) document.getElementById("navbar-over-ons").textContent = "About Us"
};<div class="language">
<div class="language-selected">
<div id="nl-NL">
nl-NL
</div>
</div>
<ul>
<li><a class="nl" onClick="changeNL()" href="#">Dutch</a></li>
<li><a class="en" onClick="changeEN()" href="#">English</a></li>
</ul>
</div>
发布于 2022-06-27 02:42:06
您要做的是在单击其中一个语言按钮时更改当前页面的HTML或内容。
这种情况发生在客户端(您的浏览器),并且只影响当前页面,为了在从一个页面导航到另一个页面时保存/使用相同的设置或语言,您需要一些逻辑来持久化该配置。
您可以选择后端路由,让服务器通过使用会话或cookie提供正确的语言,或者通过在本地存储中保存语言指示符来走前端路线,并在每次页面加载时让Javascript读取该指示符(除非您无法控制服务器/后端--我不推荐最后一个)。
Javascript方法的一个示例:
// At the top of the script
const lang = localStorage.getItem("lang");
if (lang == "en") {
// Call the change to EN function
} else if (lang == "nl") {
// Call the change to NL function
} else {
// Set the default language for new users that didn't visit the website before
localStorage.setItem("lang", "nl");
}
// Inside change language to EN code
localStorage.setItem("lang", "en");
// Inside change language to NL code
localStorage.setItem("lang", "nl");https://stackoverflow.com/questions/72766289
复制相似问题