Purpose: Front-end (only uses thymeleaf template + jquery) to achieve internationalization
Reason: The front-end does not use popular vue.js angular and other frameworks, pure HTML cannot reference constants defined in js
Using jquery assignment (maintaining 2 templates (Chinese, English) interface) Direct out
solution: use https://github.com/coderifous/jquery-localize/ a localization plug-in
a jQuery plugin that makes it easy to internationalize your web site
Steps:
1 html
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Language</title> <!-- 引用三个js文件 language_cookie.js实现记忆功能 下一次用户刷新界面之后 记得之前用户使用了那个语种 --> <script src="jquery.js" type="text/javascript" charset="utf-8"></script> <script src="jquery.localize.js" type="text/javascript" charset="utf-8"></script> <script src="language_cookie.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div> <select id="ddlSomoveLanguage" onchange="chgLang();"> <option value="">LAGUAGE</option> <option value="ja">日本</option> <option value="en">ENGLISH</option> </select> </div> <div id="prod_navright"> <ul> <!-- data-localize="hpt.management" 固定写法 对应语言包文件中的key --> <li><a data-localize="hpt.management" href="hospitality_management_ja.html" >MANAGEMENT</a></li> <li><a data-localize="hpt.support" href="hospitality_support_ja.html">SUPPORT</a></li> <li><a data-localize="hpt.tutorial" href="hospitality_tutorial_ja.html">TUTORIAL</a></li> <li><a data-localize="hpt.features" href="hospitality_features_ja.html">FEATURES</a></li> </ul> </div> </body> </html>
2 Language pack file
text-en.json
{ "hpt":{ "features": "FEATURES", "tutorial": "TUTORIAL", "support": "SUPPORT", "management": "MANAGEMENT" } }
text-ja.json
·······
3 language_cookie.js needs to be executed natively on the server and cannot be obtained Required json file Main code The bottom layer of the code at the mark may use xmlHttpRequest to obtain the .json language package file
var name = "somoveLanguage"; function chgLang() { var value = $("#ddlSomoveLanguage").children('option:selected').val(); SetCookie(name, value); console.log("come in chgLang" + name + value); location.reload(); } function SetCookie(name, value) { var Days = 30; //此 cookie 将被保存 30 天 var exp = new Date(); //new Date("December 31, 9998"); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); } function getCookie(name){ //取cookies函数 var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) return unescape(arr[2]); return null } $(function() { var uulanguage = (navigator.language || navigator.browserLanguage).toLowerCase(); console.log("come in readly" + uulanguage); if (uulanguage.indexOf("en") > -1) { $("[data-localize]").localize("text", { //**主要的代码** jquery.localize.js 底层实现切换逻辑 pathPrefix: "lang", language: "en" }); console.log("come in en"); } else if (uulanguage.indexOf("ja") > -1) { $("[data-localize]").localize("text", { pathPrefix: "lang", language: "ja" }); console.log("come in ja"); } else { $("[data-localize]").localize("text", { pathPrefix: "lang", language: "en" }); console.log("come in moren en"); }; //根据cookie选择语言 if (getCookie(name) != "") { if (getCookie(name) == "ja") { $("[data-localize]").localize("text", { pathPrefix: "lang", language: "ja" }); console.log("come in cookie ja"); } if (getCookie(name) == "en") { $("[data-localize]").localize("text", { pathPrefix: "lang", language: "en" }); console.log("come in cookie en"); } } });

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.