tôi cần điều tương tự bản thân mình và vì vậy tôi chỉ viết nó như thế này:
Tôi đã có 2 Dropdowns với các ngôn ngữ hiện có, từ ngôn ngữ từ 1 tới ngôn ngữ 2 là ý tưởng.
khi chọn cả hai ngôn ngữ Tôi đọc các tệp json thành các mảng và hiển thị nó bằng ng-repeat bằng cách kết hợp hai cách để nội dung của trường nhập thay đổi, mảng đã được cập nhật ngay lập tức.
Phương pháp áp dụng sau đó tải lên tên tệp và mảng đối với tệp php đã ghi nó vào tài liệu.
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 của tôi:
<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>
điều khiển của tôi:
//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);
});
}
đọc bạn thư mục với các file json trong javascript vào một số mảng, Đối với mỗi mảng chỉ cần tạo ra một ng-lặp lại sử dụng các trường đầu vào để bạn thấy khóa và tất cả các bản dịch với nó, khi bạn thực hiện thay đổi tạo một nút gửi cho bạn mảng và tên tệp ngôn ngữ tới tập lệnh php này: (tôi chỉ chấp nhận một ngôn ngữ tại thời điểm đó)
<?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;
?>
Nó thực sự rất dễ tạo, tôi muốn thêm nó vào trang web của mình, để mọi người có thể dịch nhưng không bao giờ đến đó.
Tôi không thể cung cấp tất cả mã, nhưng tôi thik điều này sẽ giúp bạn bắt đầu.
Có thể sao chép: http://stackoverflow.com/questions/16877062/program-for-managing-json-localization-files – Ganbin
Có thể sao chép vào: http://stackoverflow.com/questions/12664823/angular-js -support-for-localization –