update_i18n 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env php
  2. <?php
  3. $gherkinFile = __DIR__ . '/../vendor/cucumber/cucumber/gherkin/gherkin-languages.json';
  4. $json = file_get_contents($gherkinFile);
  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'), $type1] <=> [mb_strlen($type1, 'utf8'), $type2];
  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. * https://github.com/cucumber/cucumber/blob/master/gherkin/gherkin-languages.json
  49. * Please send your translation changes there.
  50. */
  51. return $arrayString;
  52. EOD
  53. );