Refactor js translator to be able to share some code with the ajax version.

This commit is contained in:
bjorn
2008-08-14 18:51:06 +00:00
parent b8de42f050
commit 5939be16b8
2 changed files with 23 additions and 16 deletions

View File

@@ -6,6 +6,26 @@
<script type="text/javascript" src="gflib.js"></script>
<script type="text/javascript" src="grammar.js"></script>
<script type="text/javascript" src="translator.js"></script>
<script type="text/javascript">
/* CHANGE ME */
var grammar = Food;
function updateTranslation () {
var input = document.getElementById('inputText').value;
var fromLang = document.getElementById('fromLang').value;
var toLang = document.getElementById('toLang').value;
addTranslation('output', grammar.translate(input, fromLang, toLang));
}
function populateLangs () {
var f = document.getElementById('fromLang');
var t = document.getElementById('toLang');
for (var c in grammar.concretes) {
addOption(f, c, c);
addOption(t, c, c);
}
}
</script>
<title>Web-based GF Translator</title>
</head>
<body onload="populateLangs(Food, 'fromLang', 'toLang')">
@@ -16,7 +36,7 @@
<p>
From: <select name="fromLang" id="fromLang" onchange=""><option value="">Any language</option></select>
To: <select name="toLang" id="toLang"><option value="">All languages</option></select>
<input type="button" value="Translate" onclick="updateTranslation(Food, 'inputText','fromLang','toLang','output'); return false;" />
<input type="button" value="Translate" onclick="updateTranslation()" />
</p>
</form>
<div id="output"></div>

View File

@@ -1,18 +1,5 @@
function updateTranslation (grammar, inputID, fromLangID, toLangID, outputID) {
var input = document.getElementById(inputID).value;
var fromLang = document.getElementById(fromLangID).value;
var toLang = document.getElementById(toLangID).value;
var output = document.getElementById(outputID);
output.appendChild(formatTranslation(grammar.translate(input, fromLang, toLang)));
}
function populateLangs (grammar, fromLang, toLang) {
var f = document.getElementById(fromLang);
var t = document.getElementById(toLang);
for (var c in grammar.concretes) {
addOption(f, c, c);
addOption(t, c, c);
}
function addTranslation (outputID, translation) {
document.getElementById(outputID).appendChild(formatTranslation(translation));
}
function formatTranslation (outputs) {