gtx-cli 2.1.10 → 2.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # gtx-cli
2
2
 
3
+ ## 2.1.11
4
+
5
+ ### Patch Changes
6
+
7
+ - [#610](https://github.com/generaltranslation/gt/pull/610) [`bfb4f53`](https://github.com/generaltranslation/gt/commit/bfb4f53658c785520373af53a1e9fadb6eca2d0b) Thanks [@SamEggert](https://github.com/SamEggert)! - create loadTranslations.js when user specifies local translations in gtx-cli init
8
+
3
9
  ## 2.1.10
4
10
 
5
11
  ### Patch Changes
package/dist/cli/base.js CHANGED
@@ -18,6 +18,7 @@ import { attachTranslateFlags } from './flags.js';
18
18
  import { handleStage } from './commands/stage.js';
19
19
  import { handleDownload, handleTranslate, postProcessTranslations, } from './commands/translate.js';
20
20
  import updateConfig from '../fs/config/updateConfig.js';
21
+ import { createLoadTranslationsFile } from '../fs/createLoadTranslationsFile.js';
21
22
  export class BaseCLI {
22
23
  library;
23
24
  additionalModules;
@@ -241,17 +242,22 @@ See the docs for more information: https://generaltranslation.com/docs/react/tut
241
242
  defaultValue: true,
242
243
  })
243
244
  : false;
244
- if (isUsingGT && !usingCDN) {
245
- logMessage(`Make sure to add a loadTranslations function to your app configuration to correctly use local translations.
246
- See https://generaltranslation.com/en/docs/next/guides/local-tx`);
247
- }
248
245
  // Ask where the translations are stored
249
246
  const translationsDir = isUsingGT && !usingCDN
250
247
  ? await promptText({
251
248
  message: 'What is the path to the directory where you would like to locally store your translations?',
252
- defaultValue: './public/locales',
249
+ defaultValue: './public/_gt',
253
250
  })
254
251
  : null;
252
+ // Determine final translations directory with fallback
253
+ const finalTranslationsDir = translationsDir?.trim() || './public/_gt';
254
+ if (isUsingGT && !usingCDN) {
255
+ // Create loadTranslations.js file for local translations
256
+ await createLoadTranslationsFile(process.cwd(), finalTranslationsDir);
257
+ logMessage(`Created ${chalk.cyan('loadTranslations.js')} file for local translations.
258
+ Make sure to add this function to your app configuration.
259
+ See https://generaltranslation.com/en/docs/next/guides/local-tx`);
260
+ }
255
261
  const message = !isUsingGT
256
262
  ? 'What is the format of your language resource files? Select as many as applicable.\nAdditionally, you can translate any other files you have in your project.'
257
263
  : `${chalk.dim('(Optional)')} Do you have any separate files you would like to translate? For example, extra Markdown files for docs.`;
@@ -278,9 +284,9 @@ See https://generaltranslation.com/en/docs/next/guides/local-tx`);
278
284
  };
279
285
  }
280
286
  // Add GT translations if using GT and storing locally
281
- if (isUsingGT && !usingCDN && translationsDir) {
287
+ if (isUsingGT && !usingCDN) {
282
288
  files.gt = {
283
- output: path.join(translationsDir, `[locale].json`),
289
+ output: path.join(finalTranslationsDir, `[locale].json`),
284
290
  };
285
291
  }
286
292
  let configFilepath = 'gt.config.json';
@@ -0,0 +1 @@
1
+ export declare function createLoadTranslationsFile(appDirectory: string, translationsDir?: string): Promise<void>;
@@ -0,0 +1,36 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { logInfo } from '../console/logging.js';
4
+ import chalk from 'chalk';
5
+ export async function createLoadTranslationsFile(appDirectory, translationsDir = './public/_gt') {
6
+ const usingSrcDirectory = fs.existsSync(path.join(appDirectory, 'src'));
7
+ // Calculate the relative path from the loadTranslations.js location to the translations directory
8
+ const loadTranslationsDir = usingSrcDirectory
9
+ ? path.join(appDirectory, 'src')
10
+ : appDirectory;
11
+ const relativePath = path.relative(loadTranslationsDir, path.resolve(appDirectory, translationsDir));
12
+ const publicPath = relativePath ? `${relativePath}/` : './';
13
+ const filePath = usingSrcDirectory
14
+ ? path.join(appDirectory, 'src', 'loadTranslations.js')
15
+ : path.join(appDirectory, 'loadTranslations.js');
16
+ if (!fs.existsSync(filePath)) {
17
+ const loadTranslationsContent = `
18
+ export default async function loadTranslations(locale) {
19
+ try {
20
+ // Load translations from ${translationsDir} directory
21
+ // This matches the GT config files.gt.output path
22
+ const t = await import(\`${publicPath}\${locale}.json\`);
23
+ return t.default;
24
+ } catch (error) {
25
+ console.warn(\`Failed to load translations for locale \${locale}:\`, error);
26
+ return {};
27
+ }
28
+ }
29
+ `;
30
+ await fs.promises.writeFile(filePath, loadTranslationsContent);
31
+ logInfo(`Created ${chalk.cyan('loadTranslations.js')} file at ${chalk.cyan(filePath)}.`);
32
+ }
33
+ else {
34
+ logInfo(`Found ${chalk.cyan('loadTranslations.js')} file at ${chalk.cyan(filePath)}. Skipping creation...`);
35
+ }
36
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gtx-cli",
3
- "version": "2.1.10",
3
+ "version": "2.1.11",
4
4
  "main": "dist/index.js",
5
5
  "bin": "dist/main.js",
6
6
  "files": [