Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
77cb903f
Commit
77cb903f
authored
Mar 08, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Telemetica: Added assignment 2 code.
parent
4a67180c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
0 deletions
+41
-0
telematica/ass2/translate
telematica/ass2/translate
+41
-0
No files found.
telematica/ass2/translate
0 → 100755
View file @
77cb903f
#!/usr/bin/php
<?php
// Call Google Translate API
function
translate
(
$text
,
$from
,
$to
)
{
$key
=
"AIzaSyD5sFp2R2IMtDVWdu-4X-TptJCSod0chVc"
;
$url
=
sprintf
(
"https://www.googleapis.com/language/translate/v2?key=%s&source=%s&target=%s&q=%s"
,
$key
,
$from
,
$to
,
urlencode
(
$text
));
if
(
(
$response
=
file_get_contents
(
$url
))
===
false
)
die
(
"Cannot connect to Google Translate server."
);
$response
=
json_decode
(
$response
,
true
);
return
$response
[
"data"
][
"translations"
][
0
][
"translatedText"
];
}
// Parse input
$argc
!=
3
&&
die
(
sprintf
(
"Usage: %s FROM TO
\n
"
,
$argv
[
0
]));
list
(
$from
,
$to
)
=
array_slice
(
$argv
,
1
);
$lang_format
=
"/^[a-z]
{
2
}
$/i"
;
if
(
!
preg_match
(
$lang_format
,
$from
)
||
!
preg_match
(
$lang_format
,
$to
)
)
die
(
"A language should be specified as two characters.
\n
"
);
// Show parsed input
printf
(
"Input language: %s
\n
Output language: %s
\n
"
,
$from
=
strtolower
(
$from
),
$to
=
strtolower
(
$to
));
// Read input
$handle
=
fopen
(
"php://stdin"
,
"r"
);
echo
"
\n
Enter the text that you want to translate (use ctrl-D to "
.
"end the program).
\n\n
Translate: "
;
while
(
(
$read
=
fgets
(
$handle
))
!==
false
)
printf
(
"Translation: %s
\n\n
Translate: "
,
translate
(
$read
,
$from
,
$to
));
fclose
(
$handle
);
?>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment