Skip to content

Commit c5eb72c

Browse files
authored
refactor(locale)!: move title to metadata (#1978)
1 parent dbbc785 commit c5eb72c

File tree

132 files changed

+636
-171
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+636
-171
lines changed

scripts/generateLocales.ts

+23-52
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { resolve } from 'node:path';
2323
import type { Options } from 'prettier';
2424
import { format } from 'prettier';
2525
import options from '../.prettierrc.cjs';
26-
import type { Definitions, LocaleDefinition } from '../src/definitions';
26+
import type { LocaleDefinition, MetadataDefinitions } from '../src/definitions';
2727

2828
// Constants
2929

@@ -45,7 +45,7 @@ type PascalCase<S extends string> = S extends `${infer P1}_${infer P2}`
4545
: Capitalize<S>;
4646

4747
type DefinitionsType = {
48-
[key in keyof Definitions]: PascalCase<`${key}Definitions`>;
48+
[key in keyof LocaleDefinition]-?: PascalCase<`${key}Definitions`>;
4949
};
5050

5151
/**
@@ -64,6 +64,7 @@ const definitionsTypes: DefinitionsType = {
6464
internet: 'InternetDefinitions',
6565
location: 'LocationDefinitions',
6666
lorem: 'LoremDefinitions',
67+
metadata: 'MetadataDefinitions',
6768
music: 'MusicDefinitions',
6869
person: 'PersonDefinitions',
6970
phone_number: 'PhoneNumberDefinitions',
@@ -153,43 +154,11 @@ function generateLocaleFile(locale: string): void {
153154
writeFileSync(resolve(pathLocale, `${locale}.ts`), content);
154155
}
155156

156-
function tryLoadLocalesMainIndexFile(
157-
pathModules: string
158-
): LocaleDefinition | undefined {
159-
let localeDef: LocaleDefinition | undefined;
160-
// This call might fail, if the module setup is broken.
161-
// Unfortunately, we try to fix it with this script
162-
// Thats why have a fallback logic here, we only need the title anyway
163-
try {
164-
// eslint-disable-next-line @typescript-eslint/no-var-requires
165-
localeDef = require(pathModules).default;
166-
} catch (e) {
167-
try {
168-
console.log(
169-
`Failed to load ${pathModules}. Attempting manual parse instead...`
170-
);
171-
const localeIndex = readFileSync(
172-
resolve(pathModules, 'index.ts'),
173-
'utf-8'
174-
);
175-
const title = localeIndex.match(/title: '(.*)',/)?.[1];
176-
if (title) {
177-
localeDef = { title };
178-
}
179-
} catch {
180-
console.error(`Failed to load ${pathModules} or manually parse it.`, e);
181-
}
182-
}
183-
184-
return localeDef;
185-
}
186-
187157
function generateLocalesIndexFile(
188158
path: string,
189159
name: string,
190160
type: string,
191-
depth: number,
192-
extra: string = ''
161+
depth: number
193162
): void {
194163
let modules = readdirSync(path);
195164
modules = removeIndexTs(modules);
@@ -214,7 +183,6 @@ function generateLocalesIndexFile(
214183
);
215184

216185
content.push(`\nconst ${name}${fieldType} = {
217-
${extra}
218186
${modules.map((module) => `${escapeField(name, module)},`).join('\n')}
219187
};\n`);
220188

@@ -230,10 +198,9 @@ function generateRecursiveModuleIndexes(
230198
path: string,
231199
name: string,
232200
definition: string,
233-
depth: number,
234-
extra?: string
201+
depth: number
235202
): void {
236-
generateLocalesIndexFile(path, name, definition, depth, extra);
203+
generateLocalesIndexFile(path, name, definition, depth);
237204

238205
let submodules = readdirSync(path);
239206
submodules = removeIndexTs(submodules);
@@ -255,8 +222,7 @@ function generateRecursiveModuleIndexes(
255222
pathModule,
256223
submodule,
257224
moduleDefinition,
258-
depth + 1,
259-
undefined
225+
depth + 1
260226
);
261227
}
262228
}
@@ -311,10 +277,21 @@ let localizationLocales = '| Locale | Name | Faker |\n| :--- | :--- | :--- |\n';
311277

312278
for (const locale of locales) {
313279
const pathModules = resolve(pathLocales, locale);
314-
315-
const localeDef = tryLoadLocalesMainIndexFile(pathModules);
316-
// We use a fallback here to at least generate a working file.
317-
const localeTitle = localeDef?.title ?? `TODO: Insert Title for ${locale}`;
280+
const pathMetadata = resolve(pathModules, 'metadata.ts');
281+
let localeTitle = 'No title found';
282+
try {
283+
// eslint-disable-next-line @typescript-eslint/no-var-requires
284+
const metadata: MetadataDefinitions = require(pathMetadata).default;
285+
localeTitle = metadata.title;
286+
if (!localeTitle) {
287+
throw new Error(`No title property found on ${JSON.stringify(metadata)}`);
288+
}
289+
} catch (e) {
290+
console.error(
291+
`Failed to load ${pathMetadata}. Please make sure the file exists and exports MetadataDefinitions.`
292+
);
293+
console.error(e);
294+
}
318295

319296
const localizedFaker = `faker${locale.replace(/^([a-z]+)/, (part) =>
320297
part.toUpperCase()
@@ -330,13 +307,7 @@ for (const locale of locales) {
330307
generateLocaleFile(locale);
331308

332309
// src/locales/**/index.ts
333-
generateRecursiveModuleIndexes(
334-
pathModules,
335-
locale,
336-
'LocaleDefinition',
337-
1,
338-
`title: '${localeTitle}',`
339-
);
310+
generateRecursiveModuleIndexes(pathModules, locale, 'LocaleDefinition', 1);
340311
}
341312

342313
// src/locale/index.ts

src/definitions/definitions.ts

+26-32
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { HackerDefinitions } from './hacker';
1010
import type { InternetDefinitions } from './internet';
1111
import type { LocationDefinitions } from './location';
1212
import type { LoremDefinitions } from './lorem';
13+
import type { MetadataDefinitions } from './metadata';
1314
import type { MusicDefinitions } from './music';
1415
import type { PersonDefinitions } from './person';
1516
import type { PhoneNumberDefinitions } from './phone_number';
@@ -18,44 +19,37 @@ import type { SystemDefinitions } from './system';
1819
import type { VehicleDefinitions } from './vehicle';
1920
import type { WordDefinitions } from './word';
2021

21-
export type LocaleEntry<T> = Partial<T> &
22-
// Unsupported & custom modules
23-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24-
Record<string, any>;
25-
2622
/**
27-
* The definitions as used by the Faker modules.
23+
* Wrapper type for all definition categories that will make all properties optional and allow extra properties.
2824
*/
29-
export interface Definitions {
30-
airline: AirlineDefinitions;
31-
animal: AnimalDefinitions;
32-
color: ColorDefinitions;
33-
commerce: CommerceDefinitions;
34-
company: CompanyDefinitions;
35-
database: DatabaseDefinitions;
36-
date: DateDefinitions;
37-
finance: FinanceDefinitions;
38-
hacker: HackerDefinitions;
39-
internet: InternetDefinitions;
40-
location: LocationDefinitions;
41-
lorem: LoremDefinitions;
42-
music: MusicDefinitions;
43-
person: PersonDefinitions;
44-
phone_number: PhoneNumberDefinitions;
45-
science: ScienceDefinitions;
46-
system: SystemDefinitions;
47-
vehicle: VehicleDefinitions;
48-
word: WordDefinitions;
49-
}
25+
export type LocaleEntry<T extends Record<string, unknown>> = Partial<T> &
26+
// Unsupported & custom entries
27+
Record<string, unknown>;
5028

5129
/**
5230
* The definitions as used by the translations/locales.
5331
* This is basically the same as Definitions with the exception,
5432
* that most properties are optional and extra properties are allowed.
5533
*/
5634
export type LocaleDefinition = {
57-
/**
58-
* The English name of the language (and the specific country, if defined).
59-
*/
60-
title: string;
61-
} & LocaleEntry<Definitions>;
35+
metadata: MetadataDefinitions;
36+
airline?: AirlineDefinitions;
37+
animal?: AnimalDefinitions;
38+
color?: ColorDefinitions;
39+
commerce?: CommerceDefinitions;
40+
company?: CompanyDefinitions;
41+
database?: DatabaseDefinitions;
42+
date?: DateDefinitions;
43+
finance?: FinanceDefinitions;
44+
hacker?: HackerDefinitions;
45+
internet?: InternetDefinitions;
46+
location?: LocationDefinitions;
47+
lorem?: LoremDefinitions;
48+
music?: MusicDefinitions;
49+
person?: PersonDefinitions;
50+
phone_number?: PhoneNumberDefinitions;
51+
science?: ScienceDefinitions;
52+
system?: SystemDefinitions;
53+
vehicle?: VehicleDefinitions;
54+
word?: WordDefinitions;
55+
} & Record<string, Record<string, unknown>>;

src/definitions/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ export type {
88
export type { CompanyDefinitions } from './company';
99
export type { DatabaseDefinitions } from './database';
1010
export type { DateDefinitions, DateEntryDefinition } from './date';
11-
export type { Definitions, LocaleDefinition } from './definitions';
11+
export type { LocaleDefinition, LocaleEntry } from './definitions';
1212
export type { FinanceDefinitions } from './finance';
1313
export type { HackerDefinitions } from './hacker';
1414
export type { InternetDefinitions } from './internet';
1515
export type { LocationDefinitions } from './location';
1616
export type { LoremDefinitions } from './lorem';
17+
export type { MetadataDefinitions } from './metadata';
1718
export type { MusicDefinitions } from './music';
1819
export type { PersonDefinitions, PersonTitleDefinitions } from './person';
1920
export type { PhoneNumberDefinitions } from './phone_number';

src/definitions/metadata.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type MetadataDefinitions = {
2+
/**
3+
* The English name of the language (and the specific country, if defined).
4+
*/
5+
title: string;
6+
} & Record<string, unknown>;

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export type {
1212
HackerDefinitions,
1313
InternetDefinitions,
1414
LocaleDefinition,
15+
LocaleEntry,
1516
/** @deprecated Use LocationDefinitions instead */
1617
LocationDefinitions as AddressDefinitions,
1718
LocationDefinitions,
1819
LoremDefinitions,
20+
MetadataDefinitions,
1921
MusicDefinitions,
2022
/** @deprecated Use PersonDefinitions instead */
2123
PersonDefinitions as NameDefinitions,

src/locales/af_ZA/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import cell_phone from './cell_phone';
77
import company from './company';
88
import internet from './internet';
99
import location from './location';
10+
import metadata from './metadata';
1011
import person from './person';
1112
import phone_number from './phone_number';
1213

1314
const af_ZA: LocaleDefinition = {
14-
title: 'Afrikaans',
1515
cell_phone,
1616
company,
1717
internet,
1818
location,
19+
metadata,
1920
person,
2021
phone_number,
2122
};

src/locales/af_ZA/metadata.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { MetadataDefinitions } from '../..';
2+
3+
const metadata: MetadataDefinitions = {
4+
title: 'Afrikaans',
5+
};
6+
7+
export default metadata;

src/locales/ar/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ import color from './color';
88
import commerce from './commerce';
99
import date from './date';
1010
import location from './location';
11+
import metadata from './metadata';
1112
import person from './person';
1213
import phone_number from './phone_number';
1314
import team from './team';
1415
import vehicle from './vehicle';
1516

1617
const ar: LocaleDefinition = {
17-
title: 'Arabic',
1818
cell_phone,
1919
color,
2020
commerce,
2121
date,
2222
location,
23+
metadata,
2324
person,
2425
phone_number,
2526
team,

src/locales/ar/metadata.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { MetadataDefinitions } from '../..';
2+
3+
const metadata: MetadataDefinitions = {
4+
title: 'Arabic',
5+
};
6+
7+
export default metadata;

src/locales/az/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import company from './company';
99
import date from './date';
1010
import internet from './internet';
1111
import location from './location';
12+
import metadata from './metadata';
1213
import person from './person';
1314
import phone_number from './phone_number';
1415

1516
const az: LocaleDefinition = {
16-
title: 'Azerbaijani',
1717
color,
1818
commerce,
1919
company,
2020
date,
2121
internet,
2222
location,
23+
metadata,
2324
person,
2425
phone_number,
2526
};

src/locales/az/metadata.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { MetadataDefinitions } from '../..';
2+
3+
const metadata: MetadataDefinitions = {
4+
title: 'Azerbaijani',
5+
};
6+
7+
export default metadata;

src/locales/base/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import database from './database';
88
import hacker from './hacker';
99
import internet from './internet';
1010
import location from './location';
11+
import metadata from './metadata';
1112
import system from './system';
1213

1314
const base: LocaleDefinition = {
14-
title: 'Base',
1515
color,
1616
database,
1717
hacker,
1818
internet,
1919
location,
20+
metadata,
2021
system,
2122
};
2223

src/locales/base/metadata.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { MetadataDefinitions } from '../..';
2+
3+
const metadata: MetadataDefinitions = {
4+
title: 'Base',
5+
};
6+
7+
export default metadata;

src/locales/cz/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ import date from './date';
88
import internet from './internet';
99
import location from './location';
1010
import lorem from './lorem';
11+
import metadata from './metadata';
1112
import person from './person';
1213
import phone_number from './phone_number';
1314

1415
const cz: LocaleDefinition = {
15-
title: 'Czech',
1616
company,
1717
date,
1818
internet,
1919
location,
2020
lorem,
21+
metadata,
2122
person,
2223
phone_number,
2324
};

src/locales/cz/metadata.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { MetadataDefinitions } from '../..';
2+
3+
const metadata: MetadataDefinitions = {
4+
title: 'Czech',
5+
};
6+
7+
export default metadata;

0 commit comments

Comments
 (0)