update_i18n 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env php
  2. <?php
  3. $gherkinUrl = 'https://raw.githubusercontent.com/cucumber/cucumber/master/gherkin/gherkin-languages.json';
  4. $json = file_get_contents($gherkinUrl);
  5. $array = array();
  6. foreach (json_decode($json, true) as $lang => $keywords) {
  7. $langMessages = array();
  8. foreach ($keywords as $type => $words) {
  9. if (!is_array($words)) {
  10. $words = array($words);
  11. }
  12. if ('scenarioOutline' === $type) {
  13. $type = 'scenario_outline';
  14. }
  15. if (in_array($type, array('given', 'when', 'then', 'and', 'but'))) {
  16. $formattedKeywords = array();
  17. foreach ($words as $word) {
  18. $formattedWord = trim($word);
  19. if ($formattedWord === $word) {
  20. $formattedWord = $formattedWord.'<'; // Convert the keywords to the syntax used by Gherkin 2, which is expected by our Lexer.
  21. }
  22. $formattedKeywords[] = $formattedWord;
  23. }
  24. $words = $formattedKeywords;
  25. }
  26. usort($words, function($type1, $type2) {
  27. return mb_strlen($type2, 'utf8') - mb_strlen($type1, 'utf8');
  28. });
  29. $langMessages[$type] = implode('|', $words);
  30. }
  31. // ensure that the order of keys is consistent between updates
  32. ksort($langMessages);
  33. $array[$lang] = $langMessages;
  34. }
  35. // ensure that the languages are sorted to avoid useless diffs between updates. We keep the English first though as it is the reference.
  36. $enData = $array['en'];
  37. unset($array['en']);
  38. ksort($array);
  39. $array = array_merge(array('en' => $enData), $array);
  40. $arrayString = var_export($array, true);
  41. file_put_contents(__DIR__.'/../i18n.php', <<<EOD
  42. <?php
  43. /*
  44. * DO NOT TOUCH THIS FILE!
  45. *
  46. * This file is automatically generated by `bin/update_i18n`.
  47. * Actual Gherkin translations live in cucumber/gherkin repo:
  48. * {$gherkinUrl}
  49. * Please send your translation changes there.
  50. */
  51. return $arrayString;
  52. EOD
  53. );