我需要同样的事情,我自己,所以我只是写这样的:
我有2个下拉列表与现有的语言,从语言1〜语言2,他的想法。
选择两种语言我将json文件读入数组,并使用双向绑定将其显示为ng-repeat,因此当输入字段的内容发生更改时,数组会立即更新。
apply方法然后将文件名和数组上传到一个将其写入文档的php文件。
eng。JSON:
{
"ALBUM":{
"TITEL":"album",
"LAAD_MEER":"Load more",
"ALBUMS":"Back to albums"
},
"INFO":{
"TITEL":"information",
"HOTELS":"Hotels",
"SPORTHAL":"Sportscenter",
"INTHISHOTEL":"In this facility"
}
}
我的html:
<section ng-if="toSelected"
class="bg-g-r bg-u-1 card flyin"
ng-repeat="section in fromContents"
id="{{'trans'+section.TITEL}}"
class="translatorSection">
<p class="paddedText bg-u-1 blueElement">{{section.TITEL}}</p>
<div class="textContainer bg-u-1"
ng-repeat="line in section">
<p style="color:grey">{{line}}</p>
<textarea class="bg-u-1" ng-model="toContents[getKeys(toContents,$parent.$index)][getKeys(section,$index)]"
style="padding: 8px;border-radius: 10px"
></textarea>
</div>
</section>
我的控制器:
//Bound to the dropdowns in my case
$scope.fromSelected = null; //language from wich to start
$scope.toSelected = null; //language i wish to extend
$scope.fromContents = null;
$scope.toContents = null;
$scope.$watch('fromSelected', function (abbr) {
if(abbr) {
jsonFactory.getLanguageContents(abbr).then(function (data) {
$scope.fromContents = data.data;
});
}
});
$scope.$watch('toSelected', function (abbr) {
if(abbr) {
jsonFactory.getLanguageContents(abbr).then(function(data){
$scope.toContents = data.data;
});
}
});
$scope.getKeys = function (array,index){
return Object.keys(array)[index];
};
$scope.getToValueByKey = function (key){
return $scope.toContents[key];
};
$scope.apply = function(){
jsonFactory.UploadLanguage($scope.toSelected,$scope.toContents)
.then(function(data){
alert('update succesfull, please reload')
});
};
Jsonfactory:
function getLanguageContents(lang) {
var deferred = $q.defer(),
httpPromise = $http.get('languages/'+lang+'.json');
httpPromise.then(function (response) {
deferred.resolve(response);
}, function (error) {
console.error(error);
});
return deferred.promise;
}
function UploadLanguage(lang,content){
return $q(function(resolve,reject){
var xmlhttp,
params = 'lang='+lang+'&content='+JSON.stringify(content);
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.open('POST', 'http://localhost:63342/website/app/php/translator.php', true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
var data = xmlhttp.responseText;
resolve(data);
}else if (xmlhttp.readyState === 4){
reject('not found');
}
};
xmlhttp.send(params);
});
}
了解您的JSON文件的文件夹中的JavaScript为一些阵列, 对于每个数组,只需使用输入字段创建一个ng-repeat,以便您可以看到密钥和所有翻译,当您进行更改时,创建一个按钮,将数组和语言文件名发送给此脚本: (我只接受当时的一种语言)
<?php
header("Access-Control-Allow-Origin: *");
$taal = $_POST["lang"];
$content = $_POST["content"];
$myfile = fopen("../languages/".$taal.".json", "w");
fwrite($myfile, $content);
fclose($myfile);
echo $taal;
echo $content;
?>
它实际上是很容易创建,我想将它添加到我的网站,让每个人都可以帮助翻译,但它从来没有在那里。
我不能给所有的代码,但我thik这应该让你开始。
可能的重复:http://stackoverflow.com/questions/16877062/program-for-managing-json-localization-files – Ganbin
可能的重复:http://stackoverflow.com/questions/12664823/angular-js -support-for-localization –