compile.plurals.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. require_once __DIR__ . '/_cli.php';
  5. require_once __DIR__ . '/i18n/PluralFormsCompiler.php';
  6. $cliOptions = new class extends CliOptionsParser {
  7. public bool $all;
  8. public string $file;
  9. public string $formula;
  10. public bool $help;
  11. public function __construct() {
  12. $this->addOption('all', (new CliOption('all', 'a'))->withValueNone());
  13. $this->addOption('file', new CliOption('file', 'f'));
  14. $this->addOption('formula', new CliOption('formula', 'p'));
  15. $this->addOption('help', (new CliOption('help', 'h'))->withValueNone());
  16. parent::__construct();
  17. }
  18. };
  19. if (!empty($cliOptions->errors)) {
  20. fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
  21. }
  22. if ($cliOptions->help || (!isset($cliOptions->formula) && !isset($cliOptions->file) && !$cliOptions->all)) {
  23. compilePluralsHelp();
  24. }
  25. $compiler = new PluralFormsCompiler();
  26. if (isset($cliOptions->formula)) {
  27. echo $compiler->compileFormulaToLambda($cliOptions->formula) . "\n";
  28. done();
  29. }
  30. if (isset($cliOptions->file)) {
  31. $compiler->compileFile($cliOptions->file);
  32. echo 'Compiled ' . $cliOptions->file . "\n";
  33. done();
  34. }
  35. $changed = $compiler->compileAll();
  36. echo 'Compiled ' . $changed . " plural file(s).\n";
  37. done();
  38. function compilePluralsHelp(): never {
  39. $file = str_replace(__DIR__ . '/', '', __FILE__);
  40. echo <<<HELP
  41. NAME
  42. $file
  43. SYNOPSIS
  44. php $file [ --all | --file=<path> | --formula='<plural-forms>' ]
  45. DESCRIPTION
  46. Compile gettext plural formulas into PHP callables for runtime consumption.
  47. -a, --all compile all app/i18n/*/plurals.php files in place.
  48. -f, --file=FILE compile a single plural file in place.
  49. -p, --formula=FORMULA output the compiled PHP lambda for a gettext plural formula.
  50. -h, --help display this help and exit.
  51. EXAMPLES
  52. php $file --formula 'nplurals=2; plural=(n != 1);'
  53. php $file --file app/i18n/en/plurals.php
  54. php $file --all
  55. REFERENCES
  56. https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
  57. https://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html
  58. HELP, PHP_EOL;
  59. exit();
  60. }