gpt4 book ai didi

javascript - 如何在 javascript 弹出警报上显示 angularjs 翻译结果

转载 作者:行者123 更新时间:2023-11-28 00:20:50 26 4
gpt4 key购买 nike

我正在使用 AngularJS 翻译,当我想在 javascript 警报弹出窗口上显示翻译结果时遇到问题。谁能告诉我该怎么做吗?谢谢。这是我的 angularjs 翻译脚本:

<script>
angular.module('app',['pascalprecht.translate'])
.config(function($translateProvider){
$translateProvider.translations('chs',{
"Name" : "名字",
"Address" : "地址"

});
$translateProvider.translations('esp',{
"Name" : "Как пользоваться",
"Address" : "dirección"

});
$translateProvider.preferredLanguage('chs');
})
.controller('TranslateController', function($translate, $scope) {

$scope.changeLanguage = function (langKey) {
$translate.use(langKey);
};
});

我想显示 javascript 的翻译结果

var getLang = function()
{ alert (); // translation result from angularjs}

这是 html 上的所有代码:

 <html ng-app='app'>

<head>
<meta charset="utf-8">
<title translate="TITLE">Basic usage</title>
<style>body { text-align: center; }</style>
<script src="Scripts/jquery-2.1.3.js" type="text/javascript"></script>
<script>

var getLang = function()
{ alert (); // how to alert translation result from angularjs}

</script>

</head>
<body>
<div id="result"></div>
<div id="timeResult"></div>

<table>
<tr>
<td>{{'Name' | translate}} : </td>
<td></td>
</tr>
<tr>
<td>{{'Address' | translate}} : </td>
<td><input type="button" value="test" onClick="getLang()"/></td>
</tr>

</table>
<script src="Scripts/angular.js" type="text/javascript"></script>
<script src="Scripts/angular-translate.js" type="text/javascript"></script>
<script>
angular.module('app',['pascalprecht.translate'])
.config(function($translateProvider){
$translateProvider.translations('chs',{
"Name" : "名字",
"Address" : "地址"

});
$translateProvider.translations('esp',{
"Name" : "Как пользоваться",
"Address" : "dirección"

});
$translateProvider.preferredLanguage('chs');
});

</script>


</body>
</html>

谢谢

最佳答案

肯定还缺少一些东西。首先,您需要将按钮绑定(bind)到某个 Controller ,以便 Angular 知道它必须执行某些操作。定义 Controller 后,您可能需要注入(inject)翻译提供程序,然后您将能够获取翻译后的名称和地址。

完整代码示例:

<html ng-app='app'>
<head>
<meta charset="utf-8">
<title translate="TITLE">Basic usage</title>
<style>body { text-align: center; }</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

</head>

<body ng-controller="TranslateController">
<div id="result"></div>
<div id="timeResult"></div>

<table>
<tr>
<td>Name: {{'Name' | translate}} : </td>
<td></td>
</tr>
<tr>
<td>Address: {{'Address' | translate}} : </td>
<td><input type="button" value="Get Preferred Language Translation" ng-click="getLang()"/></td>
</tr>

</table>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-translate/2.7.0/angular-translate.min.js" type="text/javascript"></script>
<script>
angular.module('app',['pascalprecht.translate'])
.config(function($translateProvider){
$translateProvider.translations('chs',{
"Name" : "名字",
"Address" : "地址"

});
$translateProvider.translations('esp',{
"Name" : "Как пользоваться",
"Address" : "dirección"

});
$translateProvider.preferredLanguage('chs');
})
.controller('TranslateController', function($scope, $translate) {
$scope.getLang = function()
{
$translate(['Name', 'Address']).then(function (translations) {
alert('Name is:' + translations.Name + ' Address is: ' + translations.Address);
});

}

});

</script>

</body>
</html>

关于javascript - 如何在 javascript 弹出警报上显示 angularjs 翻译结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30043842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com