
您当前的语言切换实现方式是通过 javascript 直接修改当前页面的 dom 元素内容。当用户点击语言切换按钮时,脚本会遍历页面上的特定元素(如导航栏、页脚、内容区域等),并将其文本内容替换为目标语言的对应文本。
这种方法存在一个核心问题:它是客户端一次性的操作,且不具备记忆功能。 一旦用户导航到网站的另一个页面,或者刷新当前页面,浏览器会重新加载该页面,并执行原始的 HTML 和 JavaScript。由于您的脚本没有在页面加载时检查并应用之前用户的语言选择,页面会恢复到默认语言,导致用户体验不佳。
为了解决这个问题,我们需要一种机制来“记住”用户的语言偏好,并在每次页面加载时应用它。
要使语言设置在页面跳转或刷新后依然有效,我们需要将用户的语言选择持久化存储起来。常用的持久化方案有两种:
客户端持久化: 利用浏览器提供的存储机制,如 localStorage 或 sessionStorage。
服务器端持久化: 通过后端技术(如 PHP, Node.js, Python 等)结合 Cookie 或 Session 来存储语言偏好。服务器根据这些信息渲染不同语言版本的页面。
考虑到您目前的代码是纯客户端 JavaScript 实现,且您提到对后端编码不熟悉,我们将重点讲解如何使用 localStorage 实现客户端语言偏好持久化。
localStorage 是一个 Web API,允许网站在浏览器中存储键值对数据,且这些数据在浏览器会话结束后依然保留。
为了避免代码重复,并使 localStorage 的集成更清晰,我们首先将原有的语言切换逻辑封装成独立的函数。
// 定义一个函数来切换到荷兰语
function setLanguageNL() {
// 语言选择器显示
$(".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!");
}
// 定义一个函数来切换到英语
function setLanguageEN() {
// 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以上就是实现跨页面语言偏好持久化教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号