Advertisement
Guest User

Raddit Translation Tool

a guest
Dec 11th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.80 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Raddit Translator</title>
  5.     <meta charset="utf-8" />
  6.     <style>
  7.         body {
  8.             font-family: sans-serif;
  9.         }
  10.         code {
  11.             padding: 2px;
  12.             background: #eee;
  13.             border: 1px solid #999;
  14.             border-radius: 4px;
  15.         }
  16.         li {
  17.             margin-bottom: 3px;
  18.         }
  19.         table {
  20.             width: 100%;
  21.         }
  22.         th {
  23.             text-align: left;
  24.         }
  25.         a#return, input[type="submit"] {
  26.             text-decoration: none;
  27.             color: #333;
  28.             border: 1px solid #999;
  29.             background: #eeeeee;
  30.             border-radius: 3px;
  31.             padding: 4px;
  32.             margin: 10px 0 0 3px;
  33.             display: inline-block;
  34.         }
  35.     </style>
  36. </head>
  37. <body>
  38. <?php
  39.  
  40. include '../libraries/autoload.php';
  41.  
  42. use Alchemy\Component\Yaml\Yaml;
  43.  
  44. ob_end_flush();
  45. function getTranslationFile($fileName) {
  46.     $fileName = str_replace('/', '', $fileName);
  47.     return @file_get_contents('https://gitlab.com/edgyemma/Postmill/raw/master/translations/'. $fileName .'.yml');
  48. }
  49.  
  50. function errMsg($title, $message) {
  51.     echo '<h1>'.$title.'</h1>'.PHP_EOL;
  52.     echo '<p>'.$message.'</p>';
  53.     echo '<a href="?" id="return">&larr; Return</a>';
  54. }
  55.  
  56. function readLines($string, callable $handler) {
  57.     $tok = strtok($string, "\r\n");
  58.     while($tok !== false) {
  59.         $handler($tok);
  60.         $tok = strtok("\r\n");
  61.     }
  62. }
  63.  
  64. function readLines2($string) {
  65.     $i = 0;
  66.     $lines = [];
  67.     $tok = strtok($string, "\r\n");
  68.     while($tok !== false) {
  69.         $lines[] = $tok;
  70.         $tok = strtok("\r\n");
  71.     }
  72.     return $lines;
  73. }
  74.  
  75.  
  76. // Form sent
  77. if (isset($_GET['lang'])):
  78.  
  79.     $lang = $_GET['lang'];
  80.     if (strlen($lang) != 2) {
  81.         errmsg('Invalid language', 'This language is invalid.');
  82.     } else {
  83.  
  84.         $engMessages = getTranslationFile('messages.en');
  85.         $langMessages = getTranslationFile('messages.'. $lang);
  86.  
  87.         if ($langMessages === false) {
  88.             errmsg('Invalid language', 'This language is invalid.');
  89.         } else {
  90.             $yaml = new Yaml();
  91.  
  92.             $engGroups = $yaml->loadString($engMessages);
  93.             $langGroups = $yaml->loadString($langMessages);
  94.  
  95.             $mustTranslate = [];
  96.             foreach($engGroups as $groupName => $strings) {
  97.                 if (!isset($langGroups[$groupName]))
  98.                 $mustTranslate[$groupName] = $strings;
  99.                 else {
  100.                     foreach($strings as $k => $v) {
  101.                         if (!isset($langGroups[$groupName][$k]))
  102.                         $mustTranslate[$groupName][$k] = $v;
  103.                     }
  104.                 }
  105.             }
  106.  
  107.             echo '<h1>The following messages still need to be translated:</h1>';
  108.  
  109.             echo '<table><tr><th>Path</th><th>English translation</th></tr>';
  110.             foreach($mustTranslate as $group => $strings) {
  111.                 foreach($strings as $k => $v) {
  112.                     echo '<tr><td><code>'. $group .'.'. $k .'</code></td><td>'. strip_tags($v). '</td></tr>';
  113.                 }
  114.             }
  115.             if(count($mustTranslate) === 0) {
  116.                 echo '<tr><td colspan=2>Everything has been translated so far.</td></tr>';
  117.             }
  118.             echo '</table><a href="?" id="return">&larr; Return</a>';
  119.         }
  120.     }
  121.  
  122. else:
  123. ?>
  124.     <h1>Raddit Language translation to-do tool</h1>
  125.     <p>
  126.         With this tool, you can see what text has not been translated in the specified language.
  127.     </p>
  128.     <form>
  129.         <label for="lang">
  130.             Language code
  131.         </label>
  132.         <input name="lang" id="lang" placeholder="de" /><br />
  133.         <input type="submit" value="Check" />
  134.     </form>
  135. <?php endif; ?>
  136. </body>
  137. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement