Localization Support

Demonstrates how to use the localization feature to support multiple languages for option keys and command names.

All Example Files

FILE EXPLORER
localization.ts
1import { cli, LocalizationDictionary } from 'cli-forge';
2
3// Define localization dictionary
4const dictionary: LocalizationDictionary = {
5  name: {
6    default: 'name',
7    'es-ES': 'nombre',
8    'fr-FR': 'nom',
9  },
10  port: {
11    default: 'port',
12    'es-ES': 'puerto',
13    'fr-FR': 'port',
14  },
15  verbose: {
16    default: 'verbose',
17    'es-ES': 'detallado',
18    'fr-FR': 'verbeux',
19  },
20};
21
22cli('server-app')
23  // Set up localization (uses Spanish for this example)
24  .localize(dictionary, 'es-ES')
25  // Register options - they will accept both default and localized keys
26  .option('name', {
27    type: 'string',
28    description: 'Server name',
29    default: 'MyServer',
30  })
31  .option('port', {
32    type: 'number',
33    description: 'Port to listen on',
34    default: 8080,
35  })
36  .option('verbose', {
37    type: 'boolean',
38    description: 'Enable verbose logging',
39    alias: ['v'],
40  })
41  .command('$0', {
42    handler: (args) => {
43      console.log('Starting server...');
44      console.log(`Name: ${args.name}`);
45      console.log(`Port: ${args.port}`);
46      if (args.verbose) {
47        console.log('Verbose logging enabled');
48      }
49    },
50  })
51  .forge();
52