我来分享html页面如何实现中英文切换。

在HTML页面中实现中英文切换,可以通过以下几种方法:

html页面如何实现中英文切换

(图片来源网络,侵删)

1、使用JavaScript和CSS

2、使用i18next库

3、使用localStorage存储语言设置

下面分别介绍这几种方法的实现过程。

1、使用JavaScript和CSS

我们需要创建两个CSS文件,一个用于中文样式,另一个用于英文样式,我们可以将它们命名为style_zh.cssstyle_en.css,在HTML文件中引入这两个CSS文件,并为它们添加一个类名langzhlangen,接下来,我们需要编写一个JavaScript函数来切换这些类名,从而实现中英文切换。

HTML代码:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF8">
    <title>中英文切换示例</title>
    <link rel="stylesheet" href="style_zh.css">
</head>
<body>
    <button onclick="switchLanguage(\'zh\')">中文</button>
    <button onclick="switchLanguage(\'en\')">English</button>
    <p class="content">这是一个中英文切换的示例。</p>
    <script src="main.js"></script>
</body>
</html>

CSS代码(style_zh.css):

body {
    backgroundcolor: #f0f0f0;
}
.content {
    color: #333;
}

CSS代码(style_en.css):

body {
    backgroundcolor: #fff;
}
.content {
    color: #000;
}

JavaScript代码(main.js):

function switchLanguage(lang) {
    var body = document.querySelector(\'body\');
    if (lang === \'zh\') {
        body.className = \'langzh\';
    } else if (lang === \'en\') {
        body.className = \'langen\';
    } else {
        console.error(\'Invalid language code:\', lang);
        return;
    }
}

2、使用i18next库

i18next是一个强大的国际化框架,可以帮助我们轻松实现多语言切换,我们需要安装i18next库:

npm install i18next save

在HTML文件中引入i18next库,并创建一个i18next实例:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF8">
    <title>i18next示例</title>
    <script src="https://unpkg.com/i18next@21.6.5/dist/umd/i18next.min.js"></script>
</head>
<body>
    <button onclick="changeLanguage(\'zh\')">中文</button>
    <br>
    <button onclick="changeLanguage(\'en\')">English</button>
    <br>
    <p id="example">这是一个中英文切换的示例。</p>
    <script src="main.js"></script>
</body>
</html>

JavaScript代码(main.js):

const i18nextInstance = i18next.createInstance({}); // 创建i18next实例,不传递任何配置参数表示使用默认配置,更多配置选项请参考官方文档。
i18nextInstance.init({}); // 初始化实例,传入空对象表示使用默认配置,更多配置选项请参考官方文档。
i18nextInstance.changeLanguage(\'zh\'); // 切换到中文,更多语言代码请参考官方文档。
document.getElementById(\'example\').innerText = i18nextInstance.t(\'example\'); // 使用t函数翻译文本,更多用法请参考官方文档。

3、使用localStorage存储语言设置

我们可以使用localStorage来存储用户的语言设置,以便在下次访问时恢复之前的语言设置,我们需要修改上述方法中的按钮事件处理函数,以便将当前语言设置为localStorage的值:

HTML代码:

<!省略其他代码 >
<button onclick="changeLanguage(\'zh\')">中文</button> | <button onclick="changeLanguage(\'en\')">English</button> | <a href="/zh/">中文网站</a> | <a href="/en/">English website</a> | <a href="/de/">German website</a> | <a href="/es/">Spanish website</a> | <a href="/fr/">French website</a> | <a href="/it/">Italian website</a> | <a href="/ja/">Japanese website</a> | <a href="/ko/">Korean website</a> | <a href="/ru/">Russian website</a> | <a href="/pt/">Portuguese website</a> | <a href="/tr/">Turkish website</a> | <a href="/ar/">Arabic website</a> | <a href="/th/">Thai website</a> | <a href="/id/">Indonesian website</a> | <a href="/vi/">Vietnamese website</a> | <a href="/hi/">Hindi website</a> | <a href="/pl/">Polish website</a> | <a href="/bg/">Bulgarian website</a> | <a href="/ro/">Romanian website</a> | <a href="/hr/">Croatian website</a> | <a href="/sr/">Serbian website</a> | <a href="/sk/">Slovakian website</a> | <a href="/sl/">Slovenian website</a> | <a href="/uk/">Ukrainian website</a> | <a href="/cs/">Czech website</a> | <a href="/sk/">Slovakian website</a> | <a href="/sl/">Slovenian website</a> | <a href="/uk/">Ukrainian website</a> | <a href="/cs/">Czech website</a><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>&nbsp;| &nbsp;|| &nbsp;|| &nbsp;|| &nbsp;|| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;| &nbsp;|&nbsp;&nbsp;<span style="fontfamily: Arial, PingFang, PingFangSC;"><span style="fontsize: small;"><strong></strong></span></span></p> >

本文来自投稿,不代表科技代码立场,如若转载,请注明出处https://www.cwhello.com/440726.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
上一篇 2024年6月24日 09:50
下一篇 2024年6月24日 09:50

相关推荐

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息