【问题标题】:Redirect user to specific language url based on OS language in Cordova webview app根据 Cordova webview 应用程序中的操作系统语言将用户重定向到特定语言 URL
【发布时间】:2017-11-04 23:53:00
【问题描述】:

我在一个 Cordova 项目的 index.html 中有以下代码:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="img-src * 'self' data:; default-src * 'self' gap: wss: ws: ; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval';">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>MyBilli</title>
    </head>
    <body>
    <script type="text/javascript">
        document.addEventListener('deviceready', function() {
            var url = 'http://sub.domain.com/app/nl/login/' // change to whatever you want
            cordova.InAppBrowser.open(url, '_blank', 'location=no,zoom=no');
        }, false)
    </script>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

当前 Cordova 插件列表:

cordova-plugin-camera 2.4.1 "Camera"
cordova-plugin-compat 1.1.0 "Compat"
cordova-plugin-device 1.1.6 "Device"
cordova-plugin-file 4.3.3 "File"
cordova-plugin-file-transfer 1.6.3 "File Transfer"
cordova-plugin-inappbrowser 1.7.1 "InAppBrowser"
cordova-plugin-media 3.0.1 "Media"
cordova-plugin-media-capture 1.4.3 "Capture"
cordova-plugin-network-information 1.3.3 "Network Information"
cordova-plugin-splashscreen 4.0.3 "Splashscreen"
cordova-plugin-statusbar 2.2.3 "StatusBar"
cordova-plugin-whitelist 1.3.2 "Whitelist"

是否可以根据用户的操作系统语言(仅允许 3 种语言:enfrnl)将用户重定向到不同的网址:sub.domain.com/app/[en|fr|nl]/login/

如果 enfrnl 不是用户的操作系统语言,则默认为 en

【问题讨论】:

    标签: android ios cordova localization inappbrowser


    【解决方案1】:

    看来您可以使用https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-globalization/ 获取设备语言

    然后,您只需替换 url 部分即可获取所获取的内容

    【讨论】:

    • 你能解释一下我应该在我的代码中的什么地方使用它吗?在 index.html 或其他地方?谢谢!
    • 您可以在定义 url 的地方使用它,您可以将 url 的 /nl/ 部分替换为 navigator.globalization.getPreferredLanguage(successCallback, errorCallback); 的结果。 cordova.apache.org/docs/en/latest/reference/…
    猜你喜欢
    • 1970-01-01
    • 2021-03-13
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多