i18nexus-cli 3.2.0 → 3.3.1

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.
Files changed (3) hide show
  1. package/README.md +23 -10
  2. package/commands/pull.js +49 -11
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -32,9 +32,11 @@ The commands below have options that can be set with environment variables. Sinc
32
32
  i18nexus pull -k <PROJECT_API_KEY>
33
33
  ```
34
34
 
35
- The above snippet will download all of your latest translations to your current directory. Your translation files will be downloaded in a file structure based on the convention of your i18n library:
35
+ The above snippet will download all of your latest translations to your current directory. Your translation files will be downloaded in a file structure based on the convention of your i18n library.
36
36
 
37
- If your i18nexus project is set to `i18next`:
37
+ ### Default download path:
38
+
39
+ If your i18nexus project is set to **`i18next`**:
38
40
 
39
41
  ```
40
42
  .
@@ -46,7 +48,18 @@ If your i18nexus project is set to `i18next`:
46
48
  └── common.json
47
49
  ```
48
50
 
49
- If your i18nexus project is set to `next-intl`:
51
+ If your i18nexus project is set to **`i18next`** and the CLI detects you're using **Next.js + App Router**:
52
+
53
+ ```
54
+ .
55
+ └── locales
56
+ ├── en
57
+ | └── common.json
58
+ └── de
59
+ └── common.json
60
+ ```
61
+
62
+ If your i18nexus project is set to **`next-intl`**:
50
63
 
51
64
  ```
52
65
  .
@@ -55,7 +68,7 @@ If your i18nexus project is set to `next-intl`:
55
68
  └── de.json
56
69
  ```
57
70
 
58
- If your i18nexus project is set to `react-intl`:
71
+ If your i18nexus project is set to **`react-intl`**:
59
72
 
60
73
  ```
61
74
  .
@@ -71,12 +84,12 @@ If you wish to download your files to a different directory, you can use the `--
71
84
 
72
85
  ### Options
73
86
 
74
- | Option | Default value |
75
- | ------------------- | ------------------ |
76
- | `--api-key` or `-k` | |
77
- | `--path` or `-p` | `./public/locales` |
78
- | `--ver` or `-v` | `latest` |
79
- | `--clean` | `false` |
87
+ | Option | Default value |
88
+ | ------------------- | ------------- |
89
+ | `--api-key` or `-k` | |
90
+ | `--path` or `-p` | (See above) |
91
+ | `--ver` or `-v` | `latest` |
92
+ | `--clean` | `false` |
80
93
 
81
94
  ### Notes
82
95
 
package/commands/pull.js CHANGED
@@ -40,7 +40,20 @@ const pull = async opt => {
40
40
  }
41
41
 
42
42
  if (projectLibrary === 'i18next') {
43
- path = `${process.cwd()}/public/locales`;
43
+ const hasAppDir =
44
+ fs.existsSync(`${process.cwd()}/app`) ||
45
+ fs.existsSync(`${process.cwd()}/src/app`);
46
+ const usingAppRouter =
47
+ hasAppDir &&
48
+ (fs.existsSync(`${process.cwd()}/.next`) ||
49
+ fs.existsSync(`${process.cwd()}/next.config.js`) ||
50
+ fs.existsSync(`${process.cwd()}/next.config.ts`));
51
+
52
+ if (usingAppRouter) {
53
+ path = `${process.cwd()}/locales`;
54
+ } else {
55
+ path = `${process.cwd()}/public/locales`;
56
+ }
44
57
  } else {
45
58
  path = `${process.cwd()}/messages`;
46
59
  }
@@ -48,26 +61,51 @@ const pull = async opt => {
48
61
 
49
62
  const usingVersion = opt.version !== 'latest';
50
63
 
51
- let url = usingVersion
52
- ? `https://cdn.i18nexus.com/versions/${opt.version}/translations.json`
53
- : `${baseUrl}/project_resources/translations.json`;
54
-
55
- url += `?api_key=${opt.apiKey}`;
56
-
57
64
  console.log(`Downloading translations to ${path}...`);
58
65
 
59
- const response = await handleFetch(url);
66
+ const lngResponse = await handleFetch(
67
+ `${baseUrl}/project_resources/languages.json?api_key=${opt.apiKey}`
68
+ );
60
69
 
61
- if (response.status !== 200) {
70
+ if (lngResponse.status !== 200) {
62
71
  return handleError(
63
- response,
72
+ lngResponse,
64
73
  usingVersion
65
74
  ? 'There was a problem fetching your translations. Please ensure you are using the correct API key and a valid version number.'
66
75
  : 'There was a problem fetching your translations. Please try again in a moment.'
67
76
  );
68
77
  }
69
78
 
70
- const translations = await response.json();
79
+ const languageCodes = (await lngResponse.json()).collection.map(
80
+ lng => lng.full_code
81
+ );
82
+
83
+ const translations = {};
84
+
85
+ for (let index = 0; index < languageCodes.length; index++) {
86
+ const lng = languageCodes[index];
87
+
88
+ let url = usingVersion
89
+ ? `https://cdn.i18nexus.com/versions/${opt.version}/translations/${lng}.json`
90
+ : `${baseUrl}/project_resources/translations/${lng}.json`;
91
+
92
+ url += `?api_key=${opt.apiKey}`;
93
+
94
+ const response = await handleFetch(url);
95
+
96
+ if (response.status !== 200) {
97
+ return handleError(
98
+ response,
99
+ usingVersion
100
+ ? 'There was a problem fetching your translations. Please ensure you are using the correct API key and a valid version number.'
101
+ : 'There was a problem fetching your translations. Please try again in a moment.'
102
+ );
103
+ }
104
+
105
+ const lngTranslations = await response.json();
106
+
107
+ translations[lng] = lngTranslations;
108
+ }
71
109
 
72
110
  if (opt.clean) {
73
111
  cleanDirectory(path);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18nexus-cli",
3
- "version": "3.2.0",
3
+ "version": "3.3.1",
4
4
  "description": "Command line interface (CLI) for accessing the i18nexus API",
5
5
  "main": "index.js",
6
6
  "bin": {