Skip to content

Function: commanderTranslator() ​

commanderTranslator(options?): (module) => Promise<CLITranslate>

Translate the help and the prompts of the Commander CLI adapter with the messages of an I18n component: give it to the translate option of commanderCLIEntrypoint(). Each text is the message of its key under the prefix (cli.description, cli.commands.<command>.description, cli.commands.<command>.arguments.<name>, cli.commands.<command>.options.<property>, cli.help.usage, cli.help.options, cli.help.default, cli.prompt.json…), formatted with the text's parameters ({value}); a text without a message keeps its English text. The language is chosen once, when the adapter starts: the environment variable of the environment option, the environment (environmentLocales), then the process's language, negotiated with the component's supported languages. The names of the commands and of the options are never translated.

Parameters ​

options? ​

CommanderTranslatorOptions

The component, the key prefix and the environment variable.

Returns ​

The translate function of the adapter, which gets the component from the application's module.

(module) => Promise<CLITranslate>

Example ​

typescript
import {Application} from 'lakutata'
import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
import {commanderCLIEntrypoint} from 'lakutata/adapter/commander'
import {CLIAction} from 'lakutata/decorator/ctrl'
import {buildI18nOptions, commanderTranslator, I18nMiddleware} from 'lakutata/com/i18n'

class GreetController extends Controller {
    @CLIAction('greet', {description: 'Greet someone'})
    public async greet(): Promise<string> {
        return 'Hello'
    }
}

Application.run(() => ({
    id: 'tool.app',
    name: 'Tool',
    components: {
        i18n: buildI18nOptions({
            messages: {
                'zh-CN': {cli: {commands: {greet: {description: '问候某人'}}, help: {usage: '用法:', options: '选项:'}}}
            }
        }),
        entrypoint: buildEntrypoints({
            controllers: [GreetController],
            //The controllers' Translator in the same language
            middlewares: [I18nMiddleware],
            //LANG=zh_CN.UTF-8 tool --help: the help in Chinese
            cli: commanderCLIEntrypoint({bin: 'tool', translate: commanderTranslator({environment: 'TOOL_LANG'})})
        })
    },
    bootstrap: ['entrypoint']
}))

Released under the MIT License.