i18next Integration
Demonstrates how to integrate cli-forge with i18next localization library using the function-based localization API.
1 FILESTry in Playground
All Example Files
FILE EXPLORER
i18next-integration.ts
1import { cli } from 'cli-forge';
2import i18next from 'i18next';
3
4async function main() {
5 // Initialize i18next with translations for multiple locales
6 await i18next.init({
7 lng: 'en-US', // Set default language
8 fallbackLng: 'en-US',
9 resources: {
10 'en-US': {
11 translation: {
12 name: 'name',
13 port: 'port',
14 verbose: 'verbose',
15 serve: 'serve',
16 build: 'build',
17 },
18 },
19 'es-ES': {
20 translation: {
21 name: 'nombre',
22 port: 'puerto',
23 verbose: 'detallado',
24 serve: 'servir',
25 build: 'construir',
26 },
27 },
28 'fr-FR': {
29 translation: {
30 name: 'nom',
31 port: 'port',
32 verbose: 'verbeux',
33 serve: 'servir',
34 build: 'construire',
35 },
36 },
37 },
38 });
39
40 // You can change the language dynamically:
41 // await i18next.changeLanguage('es-ES');
42
43 await cli('server-app')
44 // Pass i18next.t as the localization function
45 // This integrates cli-forge with i18next's translation system
46 .localize((key) => i18next.t(key))
47 .option('name', {
48 type: 'string',
49 description: 'Server name',
50 default: 'MyServer',
51 })
52 .option('port', {
53 type: 'number',
54 description: 'Port to listen on',
55 default: 8080,
56 })
57 .option('verbose', {
58 type: 'boolean',
59 description: 'Enable verbose logging',
60 alias: ['v'],
61 })
62 .command('serve', {
63 builder: (cmd) => cmd,
64 handler: (args) => {
65 console.log('Starting server...');
66 console.log(`Name: ${args.name}`);
67 console.log(`Port: ${args.port}`);
68 if (args.verbose) {
69 console.log('Verbose logging enabled');
70 }
71 },
72 description: 'Start the development server',
73 })
74 .command('build', {
75 builder: (cmd) => cmd,
76 handler: (args) => {
77 console.log('Building application...');
78 console.log(`Name: ${args.name}`);
79 if (args.verbose) {
80 console.log('Verbose logging enabled');
81 }
82 },
83 description: 'Build for production',
84 })
85 .command('$0', {
86 handler: (args) => {
87 console.log('Starting server...');
88 console.log(`Name: ${args.name}`);
89 console.log(`Port: ${args.port}`);
90 if (args.verbose) {
91 console.log('Verbose logging enabled');
92 }
93 },
94 })
95 .forge();
96}
97
98main();
99