-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
132 lines (112 loc) · 5.5 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
$languages = glob('languages/*.po');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Scratch translator</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
.form {
width: 100%;
padding: 19px 29px 29px;
}
textarea {
box-sizing: border-box;
width: 96%;
height: 300px;
}
</style>
<link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">
</head>
<body>
<form class="form" method="post">
<h2 class="form-heading">Scratch translator</h2>
<p>
How to use: Paste a file from the <a href="https://github.com/CodeClub/scratch-curriculum/" target="_blank">Scratch curriculum</a> into the text area below, select the
target language and click "translate". The code blocks will automatically be translated to their counterparts from the selected target language.
</p>
<p>
Refer to the <a href="https://github.com/CodeClubLux/scratch-translator" target="_blank">GitHub project</a> if you encounter bugs or would like to add a language.
</p>
<p>
<label for="original">Source</label>
<textarea id="original" name="original"><?php if (isset($_POST['original'])) echo htmlentities($_POST['original']) ?></textarea><br/>
<label for="language">Target language</label>
<select id="language" name="language">
<?php
foreach ($languages as $language) {
echo '<option'.(isset($_POST['language']) && $language == $_POST['language'] ? ' selected="selected"' : '').'>'.$language.'</option>';
}
?>
</select><br/>
<button class="btn btn-large btn-primary" type="submit">Translate</button>
</p>
<?php
if (isset($_POST['original']) && isset($_POST['language']) && in_array($_POST['language'], $languages)) {
$translated_text = $original_text = $_POST['original'];
require('poparser/poparser.php');
$poparser = new I18n_Pofile();
$entries = $poparser->read($_POST['language']);
$entries_additional = $poparser->read($_POST['language'] . '.additional');
$entries = array_merge($entries, $entries_additional);
// Iterate over the entries in the po file
foreach ($entries as $entry) {
// Ignore entries that have a missing or empty msgid
if (!isset($entry['msgid']) || !$entry['msgid']) {
continue;
}
// Construct the regex for the source text
$source = $entry['msgid'];
$source = preg_replace('/%[a-zA-Z]/', '(\s?[a-zA-Z0-9_.\-!\?]+(?:\s[a-zA-Z0-9_.\-!\?]+){0,5})', $source);
$source = '(^[\s\t]*|\(|"|__)' . $source . '([\s\t]*$|\)|"|__)';
// Construct the replacement pattern
$target = implode('', $entry['msgstr']); // the msgstr can be made of several lines which we need to join
$i = 1;
while (preg_match('|%[a-zA-Z]|', $target)) {
$target = preg_replace('|%[a-zA-Z]|', '\\\\' . ++$i, $target, 1);
}
$target = '\\1' . $target . '\\' . ++$i;
$delimiter_start = '{';
$delimiter_end = '}';
// Function to handle the translations
$handle_translations = function ($matches) use ($entries, $delimiter_start, $delimiter_end, $source, $target) {
$translated_text = preg_replace($delimiter_start . $source . $delimiter_end . 'Umui', $target, $matches[0]);
// Handle "inner" translations
foreach ($entries as $entry) {
if (!isset($entry['msgid']) || !$entry['msgid']) {
continue;
}
// Replace the '%s', '%n', etc placeholders from the po file with a generic regex
$source = preg_replace('/%[a-zA-Z]/', '([a-zA-Z0-9_.\-!\?]+)', $entry['msgid']);
$target = implode('', $entry['msgstr']);
$i = 1;
while (preg_match('|%[a-zA-Z]|', $target)) {
$target = preg_replace('|%[a-zA-Z]|', '\\\\' . ++$i, $target, 1);
}
$translated_text = preg_replace($delimiter_start . '(\W)' . $source . '(\W)' . $delimiter_end . 'Umui', '\\1' . $target . '\\' . ++$i . '\\' . ++$i, $translated_text);
}
return $translated_text;
};
// Check if the current entry from the po file can be used
$translated_text = preg_replace_callback($delimiter_start . $source . $delimiter_end . 'Umui', $handle_translations, $translated_text);
}
// Output the result
echo '<p><label for="translation">Translation:</label><textarea id="translation">' . htmlentities($translated_text) . '</textarea></p>';
// Output a diff (helpful for a quick check)
require __DIR__ . '/php-diff/lib/Diff.php';
require __DIR__ . '/php-diff/lib/Diff/Renderer/Html/SideBySide.php';
$options = array(
'ignoreWhitespace' => true,
);
$diff = new Diff(explode("\n", $original_text), explode("\n", $translated_text), $options);
$renderer = new Diff_Renderer_Html_SideBySide;
echo $diff->Render($renderer);
}
?>
</form>
</body>
</html>