首页
注册
登录
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请
登录
V2EX
›
前端开发
用 AI 写了个自动根据浏览器语言跳转 url 的代码,问关于语言标签的问题。
CSGO
·
wyfang
·
191 天前
· 494 次点击
这是一个创建于 191 天前的主题,其中的信息可能已经有所发展或是发生改变。
AI 给了这样的代码:
<script>
var lang = (navigator.language || navigator.userLanguage).toLowerCase();
switch (lang) {
case 'it':
window.location.href = 'it.html';
break;
case 'de':
window.location.href = 'de.html';
break;
case 'ja':
window.location.href = 'ja.html';
break;
case 'zh-cn':
window.location.href = 'zh-cn.html';
break;
case 'zh-tw':
window.location.href = 'zh-tc.html';
break;
case 'zh-hk':
window.location.href = 'zh-tc.html';
break;
default:
window.location.href = 'en.html'; // Default to English if no match found
}
</script>
我的疑问是,我通过 chrome 调试,选了 Tokyo ,它语言是 ja-JP ,那么这个 ja-JP 不会跳转 ja.html?
那么其他如 de-DE 也不会跳转 de.html ?那我岂不是还得收集全部的 de-XX 和 ja-XX 都写入这个代码?
break
Case
ML
lang
2 条回复
1
AoEiuV020JP
191 天前
1
那就别用 switch , 用 if startsWith 之类的判断条件,或者干脆 split 分成两个部分分别判断,
2
CSGO
OP
191 天前
@
AoEiuV020JP
OK
if (lang.startsWith('ja')) {
window.location.href = 'ja.html';
break;
}
关于
·
帮助文档
·
博客
·
API
·
FAQ
·
实用小工具
·
2503 人在线
最高记录 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 26ms ·
UTC 03:43
·
PVG 11:43
·
LAX 20:43
·
JFK 23:43
Developed with
CodeLauncher
♥ Do have faith in what you're doing.