var app = angular.module('app', []);
app.controller('ctrl', function ($scope) {
$scope.done = false;
$scope.open = function () {
var reader = new FileReader();
reader.onload = function (e) {
$scope.$apply(function () {
var records = reader.result.split('\n');
var keySet = {};
var x = 0;
$scope.labels = [];
for (var i = 0; i < records.length; i++) {
var tuple = records[i].split('\t');
tuple[0] = tuple[0].trim();
if (tuple.length >= 3 && !keySet[tuple[0]]) { // check for 3 columns and duplicate keys
keySet[tuple[0]] = true;
tuple[2] = tuple[2].trim()
.replace(/^"(.+(?="$))"$/, '$1') // remove double quotes
.trim()
.replace(/\'/g, "''"); // escape single quotes
$scope.labels[x++] = {
key: tuple[0],
value: tuple[2]
};
}
}
$scope.done = true;
});
// highlight the syntax
sh_highlightDocument();
}
// get <input> element and the selected file
var tsvFileInput = document.getElementById('fileInput');
var tsvFile = tsvFileInput.files[0];
// use reader to read the selected file
// when read operation is successfully finished the load event is triggered
// and handled by our reader.onload function
reader.readAsText(tsvFile);
// use the file name as the locale
var locale = tsvFile.name.substring(0, tsvFile.name.lastIndexOf('.'));
locale = locale.substring(0, 3) + locale.substring(3, 5).toUpperCase();
$scope.locale = locale;
}
});
<!-- ** Cross-platform code generator in AngularJS **
Input: A 3-column tab dilimited file (Local *.txt file in UTF-8)
# Caption key
# Original caption
# Translated caption
e.g.:
lblClientName Client Name Client Naam
btnContinue Continue Voortzetten
ddlSelectClient -- Select Client -- -- Selecteer Client --
Output: SQL (Oracle) script (Syntax highlight with SHJS http://shjs.sourceforge.net/)
-->
<div ng-app="app" ng-controller="ctrl">
<form ng-hide="done">
<input onchange="angular.element(this).scope().open()" type="file" accept=".txt" id="fileInput" />
</form>
<pre ng-show="done" class="sh_oracle">
SET DEFINE OFF;
<span ng-repeat="label in labels">
MERGE INTO LABEL USING DUAL ON (LANGUAGE_CODE = '{{locale}}' AND LABEL_KEY='{{label.key}}')
WHEN NOT MATCHED THEN INSERT (LANGUAGE_CODE, LABEL_KEY, LABEL_VALUE)
VALUES '{{locale}}', '{{label.key}}', '{{label.value}}')
WHEN MATCHED THEN UPDATE
SET LABEL_VALUE='{{label.value}}';
</span>
COMMIT;
</pre>
</div>
External resources loaded into this fiddle: