mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 11:42:49 -06:00
Updated AJAX translator to make use of canParse and the fixed parseAllLangs.
This commit is contained in:
@@ -44,7 +44,7 @@ cgiMain pgf =
|
|||||||
mto <- getLang pgf "to"
|
mto <- getLang pgf "to"
|
||||||
outputJSON $ translate pgf input mcat mfrom mto
|
outputJSON $ translate pgf input mcat mfrom mto
|
||||||
"/categories" -> outputJSON $ PGF.categories pgf
|
"/categories" -> outputJSON $ PGF.categories pgf
|
||||||
"/languages" -> outputJSON $ PGF.languages pgf
|
"/languages" -> outputJSON $ toJSObject $ listLanguages pgf
|
||||||
_ -> outputNotFound path
|
_ -> outputNotFound path
|
||||||
|
|
||||||
getCat :: PGF -> String -> CGI (Maybe PGF.Category)
|
getCat :: PGF -> String -> CGI (Maybe PGF.Category)
|
||||||
@@ -75,12 +75,26 @@ outputStrict x | x == x = output x
|
|||||||
|
|
||||||
translate :: PGF -> String -> Maybe PGF.Category -> Maybe PGF.Language -> Maybe PGF.Language -> Translation
|
translate :: PGF -> String -> Maybe PGF.Category -> Maybe PGF.Language -> Maybe PGF.Language -> Translation
|
||||||
translate pgf input mcat mfrom mto =
|
translate pgf input mcat mfrom mto =
|
||||||
Record [(from, [Record [(to, PGF.linearize pgf to tree) | to <- toLangs] | tree <- trees])
|
Record [(from,[Record (linearize' pgf mto tree) | tree <- trees])
|
||||||
| from <- fromLangs, let trees = PGF.parse pgf from cat input, not (null trees)]
|
| (from,trees) <- parse' pgf input mcat mfrom]
|
||||||
where cat = fromMaybe (PGF.startCat pgf) mcat
|
|
||||||
fromLangs = maybe (PGF.languages pgf) (:[]) mfrom
|
|
||||||
toLangs = maybe (PGF.languages pgf) (:[]) mto
|
|
||||||
|
|
||||||
|
parse' :: PGF -> String -> Maybe PGF.Category -> Maybe PGF.Language -> [(PGF.Language,[PGF.Tree])]
|
||||||
|
parse' pgf input mcat mfrom =
|
||||||
|
case mfrom of
|
||||||
|
Nothing -> PGF.parseAllLang pgf cat input
|
||||||
|
Just from -> [(from, PGF.parse pgf from cat input)]
|
||||||
|
where cat = fromMaybe (PGF.startCat pgf) mcat
|
||||||
|
|
||||||
|
linearize' :: PGF -> Maybe PGF.Language -> PGF.Tree -> [(PGF.Language,String)]
|
||||||
|
linearize' pgf mto tree =
|
||||||
|
case mto of
|
||||||
|
Nothing -> PGF.linearizeAllLang pgf tree
|
||||||
|
Just to -> [(to,PGF.linearize pgf to tree)]
|
||||||
|
|
||||||
|
listLanguages :: PGF -> [(PGF.Language,JSObject JSValue)]
|
||||||
|
listLanguages pgf = [(l,toJSObject (info l)) | l <- PGF.languages pgf]
|
||||||
|
where info l = [("languageCode", showJSON (fromMaybe "" (PGF.languageCode pgf l))),
|
||||||
|
("canParse", showJSON (PGF.canParse pgf l))]
|
||||||
|
|
||||||
-- * General CGI Error exception mechanism
|
-- * General CGI Error exception mechanism
|
||||||
|
|
||||||
|
|||||||
@@ -23,8 +23,10 @@
|
|||||||
var f = document.getElementById('fromLang');
|
var f = document.getElementById('fromLang');
|
||||||
var t = document.getElementById('toLang');
|
var t = document.getElementById('toLang');
|
||||||
for (var i in langs) {
|
for (var i in langs) {
|
||||||
addOption(f, langs[i], langs[i]);
|
if (langs[i].canParse) {
|
||||||
addOption(t, langs[i], langs[i]);
|
addOption(f, i, i);
|
||||||
|
}
|
||||||
|
addOption(t, i, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +34,7 @@
|
|||||||
gf.getLanguages(populateLangs);
|
gf.getLanguages(populateLangs);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<title>Web-based GF Translator</title>
|
<title>AJAX GF Translator</title>
|
||||||
</head>
|
</head>
|
||||||
<body onload="initialize()">
|
<body onload="initialize()">
|
||||||
<form id="translate">
|
<form id="translate">
|
||||||
|
|||||||
Reference in New Issue
Block a user