Skip to content

Commit eb4e4f8

Browse files
committed
fix: init command
1 parent ba26b37 commit eb4e4f8

3 files changed

Lines changed: 44 additions & 18 deletions

File tree

packages/cli/src/cli/cmd/init.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ export default new InteractiveCommand()
130130
});
131131
} else {
132132
spinner.succeed("No existing locale files found.");
133+
}
134+
135+
if (selectedPatterns.length === 0) {
133136
const useDefault = await confirm({
134137
message: `Use (and create) default path ${patterns.join(", ")}?`,
135138
});

packages/cli/src/cli/utils/find-locale-paths.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ describe("findLocaleFiles", () => {
6363
"src/locales/fr/messages.json",
6464
"src/i18n/es/strings.json",
6565
"src/translations/es.json",
66+
"src/aa/translations/en.json",
67+
"src/aa/bb/foobar/cc/translations/es/values.json",
68+
"src/aa/en.json",
69+
"src/aa/translations/bb/en.json",
6670

6771
// not a valid locale
6872
"src/xx/settings.json",
@@ -77,6 +81,10 @@ describe("findLocaleFiles", () => {
7781
"src/locales/[locale]/messages.json",
7882
"src/i18n/[locale]/strings.json",
7983
"src/translations/[locale].json",
84+
"src/aa/translations/[locale].json",
85+
"src/aa/bb/foobar/cc/translations/[locale]/values.json",
86+
"src/aa/[locale].json",
87+
"src/aa/translations/bb/[locale].json",
8088
],
8189
});
8290
});
@@ -97,13 +105,18 @@ describe("findLocaleFiles", () => {
97105
"ios/MyApp/Localizable.xcstrings",
98106
"ios/MyApp/Onboarding/Localizable.xcstrings",
99107
"ios/MyApp/Onboarding/fr.xcstrings",
108+
"ios/MyApp/xx/Localizable.xcstrings",
100109
]);
101110

102111
const result = findLocaleFiles("xcode-xcstrings");
103112

104113
expect(result).toEqual({
105114
found: true,
106-
patterns: ["ios/MyApp/Localizable.xcstrings", "ios/MyApp/Onboarding/Localizable.xcstrings"],
115+
patterns: [
116+
"ios/MyApp/Localizable.xcstrings",
117+
"ios/MyApp/Onboarding/Localizable.xcstrings",
118+
"ios/MyApp/xx/Localizable.xcstrings",
119+
],
107120
});
108121
});
109122

packages/cli/src/cli/utils/find-locale-paths.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,38 @@ function findLocaleFilesWithExtension(ext: string) {
3434
ignore: ["node_modules/**", "package*.json", "i18n.json", "lingo.json"],
3535
});
3636

37-
const localeFilePattern = new RegExp(`[\/\\\\]([a-z]{2}(-[A-Z]{2})?)${ext}$`);
38-
const localeDirectoryPattern = new RegExp(`[\/\\\\]([a-z]{2}(-[A-Z]{2})?)[\/\\\\][^\/\\\\]+${ext}$`);
37+
const localeFilePattern = new RegExp(`\/([a-z]{2}(-[A-Z]{2})?)${ext}$`);
38+
const localeDirectoryPattern = new RegExp(`\/([a-z]{2}(-[A-Z]{2})?)\/[^\/]+${ext}$`);
3939
const potentialLocaleFiles = files.filter(
4040
(file: string) => localeFilePattern.test(file) || localeDirectoryPattern.test(file),
4141
);
42-
const localeFilesAndPatterns = potentialLocaleFiles
42+
43+
const potantialLocaleFilesAndPatterns = potentialLocaleFiles
4344
.map((file: string) => {
44-
const match = file.match(new RegExp(`[\/|\\\\]([a-z]{2}(-[A-Z]{2})?)(\/|\\\\|${ext})`));
45-
const locale = match?.[1];
46-
const localeInDir = match?.[3] !== ext;
47-
const filePattern = localeInDir
48-
? file.replace(`/${locale}/`, `/[locale]/`)
49-
: path.join(path.dirname(file), `[locale]${ext}`);
50-
return { file, locale, pattern: filePattern };
45+
const matchPotentialLocales = file
46+
.matchAll(new RegExp(`\/([a-z]{2}(-[A-Z]{2})?|[^\/]+)(?=\/|${ext})`, "g"))
47+
.toArray();
48+
const potantialLocales = matchPotentialLocales.map((match) => match[1]);
49+
return { file, potantialLocales };
50+
})
51+
.map(({ file, potantialLocales }) => {
52+
for (const locale of potantialLocales) {
53+
try {
54+
resolveLocaleCode(locale as LocaleCode);
55+
return { locale, file };
56+
} catch (e) {}
57+
}
58+
return { file, locale: null };
5159
})
52-
.filter(({ locale }) => {
53-
try {
54-
resolveLocaleCode(locale as LocaleCode);
55-
return true;
56-
} catch (e) {}
57-
return false;
58-
});
60+
.filter(({ locale }) => locale !== null);
61+
62+
const localeFilesAndPatterns = potantialLocaleFilesAndPatterns.map(({ file, locale }) => {
63+
const localeInDir = file.match(`/${locale}/`);
64+
const pattern = localeInDir
65+
? file.replace(`/${locale}/`, `/[locale]/`)
66+
: path.join(path.dirname(file), `[locale]${ext}`);
67+
return { pattern, file };
68+
});
5969

6070
const grouppedFilesAndPatterns = _.groupBy(localeFilesAndPatterns, "pattern");
6171
const patterns = Object.keys(grouppedFilesAndPatterns);

0 commit comments

Comments
 (0)