Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headers are hard-coded in Help.formatHelp() #1608

Closed
edukisto opened this issue Sep 20, 2021 · 5 comments
Closed

Headers are hard-coded in Help.formatHelp() #1608

edukisto opened this issue Sep 20, 2021 · 5 comments

Comments

@edukisto
Copy link

edukisto commented Sep 20, 2021

Is there a cleaner way to internationalize the headers, which are hard-coded in Help.formatHelp(), i. e. Arguments, Commands, Options, Usage?

import process from 'process';
import { Command, Help } from 'commander';

class MyHelp extends Help {
  formatHelp(cmd, helper) {
    const result = super.formatHelp(cmd, helper);
    return result.replace('Usage:', 'Verwendung:'); // Translate!
  }
}

class MyCommand extends Command {
  createHelp() {
    return Object.assign(new MyHelp(), this.configureHelp());
  }
}

const program = new MyCommand();
program.parse(process.argv);
@shadowspawn
Copy link
Collaborator

There is not support to internationalize strings so that approach is reasonable. You can either reimplement the routine, or modify the string it returns as you did.

You can also achieve the same thing without making a subclass using .configureHelp(), but same idea.

Related: #128 #774

@edukisto
Copy link
Author

Command.configureHelp()? Did you mean Command.configureOutput()?

import process from 'process';
import { Command } from 'commander';

const program = new Command();

program.configureOutput({
  writeErr: (str) => process.stderr.write(
    str
      .replace('error:', 'ошибка:')
      // Proceed to replace other tokens from lib/command.js.
  ),

  writeOut: (str) => process.stdout.write(
    str
      .replace('Arguments:', 'Аргументы:')
      .replace('Commands:', 'Команды:')
      .replace('Options:', 'Опции:')
      .replace('Usage:', 'Использование:')
  ),
});

program.parse(process.argv);

@shadowspawn
Copy link
Collaborator

shadowspawn commented Sep 20, 2021

configureOutput works too! But be aware that the help can be displayed using writeErr too, like for no subcommand specified for program with subcommands.

I did mean configureHelp:

import * as process from 'process';
import { Command, Help } from 'commander';

const program = new Command();

program.configureHelp({
  formatHelp: (cmd, helper) => {
    const originalHelp = new Help();
    return originalHelp.formatHelp(cmd, helper)
      .replace('Arguments:', 'Аргументы:')
      .replace('Commands:', 'Команды:')
      .replace('Options:', 'Опции:')
      .replace('Usage:', 'Использование:');
  }
});

program.parse(process.argv);

@edukisto
Copy link
Author

Ah, now I see how to use configureHelp() to override formatHelp(). This is a pretty clean way.
Thank you very much!

@shadowspawn
Copy link
Collaborator

Anyone following this issue is likely to be interested in #1801

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants