Javascript 使用i18翻译库翻译警报消息

Javascript 使用i18翻译库翻译警报消息,javascript,jquery,internationalization,i18next,Javascript,Jquery,Internationalization,I18next,我写了一个代码,将文本从en-US翻译成id(印度尼西亚语)。此处按钮有一条文本,内容为如果您是认真的,请单击我。应用翻译后,它被成功地翻译为Klik saya jika anda serius。如果我单击该按钮,将显示一条警告消息,如下所示:您已收到警告,请将其编码下来。我希望将该警报消息转换为id。这是我的密码: <!DOCTYPE html> <html> <head> <title>Translation</title>

我写了一个代码,将文本从
en-US
翻译成
id
(印度尼西亚语)。此处按钮有一条文本,内容为
如果您是认真的,请单击我。应用翻译后,它被成功地翻译为
Klik saya jika anda serius
。如果我单击该按钮,将显示一条警告消息,如下所示:
您已收到警告,请将其编码下来
。我希望将该警报消息转换为
id
。这是我的密码:

<!DOCTYPE html>
<html>

<head>
    <title>Translation</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <button onclick="alertbox();" id="btn" >Click me if you are serious</button>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="i18next-1.10.1.min.js"></script>
    <script>
    function alertbox () {
      alert("You have been alerted, code it down");
    }
    $.i18n.init({
        lng: 'id-ID',
        ns: {
            namespaces: ['ns.common', 'ns.special'],
            defaultNs: 'ns.special'
        },
        useLocalStorage: false,
        debug: true
    }, function(t) {
        $('#btn').text($.t('app.btn', {
            btn: ''
        }))
    });


    </script>
</body>

</html>  
en-US(ns.special.json)


如何将警报消息转换为
id

你的问题在哪里?你甚至都不尝试:)这里有所有记录: 您可以找到许多使用jquery的示例。您甚至可以在JSFIDLE上找到活生生的示例,如:

$(document).ready(function () {
i18n.init({
    "lng": 'en',
    "resStore": resources,
    "fallbackLng" : 'en'
}, function (t) {
    $(document).i18n();
});

$('.lang').click(function () {
    var lang = $(this).attr('data-lang');
    i18n.init({
        lng: lang
    }, function (t) {
        $(document).i18n();
    });
});
});


完成您的代码并编辑您的问题,以便我们可以帮助您。首先声明i18n变量,然后使用$.t

问题出在哪里?你甚至都不尝试:)这里有所有记录: 您可以找到许多使用jquery的示例。您甚至可以在JSFIDLE上找到活生生的示例,如:

$(document).ready(function () {
i18n.init({
    "lng": 'en',
    "resStore": resources,
    "fallbackLng" : 'en'
}, function (t) {
    $(document).i18n();
});

$('.lang').click(function () {
    var lang = $(this).attr('data-lang');
    i18n.init({
        lng: lang
    }, function (t) {
        $(document).i18n();
    });
});
});

完成您的代码并编辑您的问题,以便我们可以帮助您。首先声明i18n变量,然后使用$.t

我的代码:

<!DOCTYPE html>
<html>

<head>
    <title>Translation</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="test.css">
</head>

<body>
    <button id="btn">Click me if you are serious</button>
    <div id="qwe"></div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="i18next-1.10.1.min.js"></script>
    <script>
    /*$("#btn").click(function() {
        alert("You have been alerted, code it down!!!");
    });*/
    $.i18n.init({
        lng: 'en-US',
        ns: {
            namespaces: ['ns.common', 'ns.special'],
            defaultNs: 'ns.special'
        },
        useLocalStorage: false,
        debug: true
    }, function(t) {
        var key = $('#qwe').text();
        $('#btn').text($.t('app.btn', {
            btn: ''
        }))
        $('#qwe').text($.t('app.alert', {
          alert: ''
        }))
    });
    $("#btn").click(function() {
        alert($('#qwe').text());
    });
    </script>
</body>

</html>  
ns.special.json(美国英语):

ns.special.json(id):

我的代码:

<!DOCTYPE html>
<html>

<head>
    <title>Translation</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="test.css">
</head>

<body>
    <button id="btn">Click me if you are serious</button>
    <div id="qwe"></div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="i18next-1.10.1.min.js"></script>
    <script>
    /*$("#btn").click(function() {
        alert("You have been alerted, code it down!!!");
    });*/
    $.i18n.init({
        lng: 'en-US',
        ns: {
            namespaces: ['ns.common', 'ns.special'],
            defaultNs: 'ns.special'
        },
        useLocalStorage: false,
        debug: true
    }, function(t) {
        var key = $('#qwe').text();
        $('#btn').text($.t('app.btn', {
            btn: ''
        }))
        $('#qwe').text($.t('app.alert', {
          alert: ''
        }))
    });
    $("#btn").click(function() {
        alert($('#qwe').text());
    });
    </script>
</body>

</html>  
ns.special.json(美国英语):

ns.special.json(id):


嗯。。。。有人在吗?嗯。。。。有人吗?我在试。。。我设法将按钮文本翻译成另一种语言,但不是alert Message,而是这么简单:alert($.t(“key”));所以试着告诉我们它是否有效。记得在脚本之前定义插件和键!看起来你还没理解我的代码。我的代码是完整的。我会把我的工作代码作为答案我试着。。。我设法将按钮文本翻译成另一种语言,但不是alert Message,而是这么简单:alert($.t(“key”));所以试着告诉我们它是否有效。记得在脚本之前定义插件和键!看起来你还没理解我的代码。我的代码是完整的。我将使用我的工作代码作为答案
div {
    display: none;
}  
{
    "app": {
        "btn": "Click me if you are serious",
        "alert": "You have been alerted, code it down!!!"
    }
}  
{
    "app": {
        "btn": "Klik saya jika anda serius",
        "alert": "Anda telah diperingatkan, kode itu turun!!!"
    }
}