V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
CSGO
V2EX  ›  前端开发

用 AI 写了个自动根据浏览器语言跳转 url 的代码,问关于语言标签的问题。

  •  
  •   CSGO ·
    wyfang · 161 天前 · 476 次点击
    这是一个创建于 161 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 都写入这个代码?
    AoEiuV020JP
        1
    AoEiuV020JP  
       161 天前   ❤️ 1
    那就别用 switch , 用 if startsWith 之类的判断条件,或者干脆 split 分成两个部分分别判断,
    CSGO
        2
    CSGO  
    OP
       160 天前
    @AoEiuV020JP OK

    if (lang.startsWith('ja')) {
    window.location.href = 'ja.html';
    break;
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2216 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 02:22 · PVG 10:22 · LAX 19:22 · JFK 22:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.