Skip to content

Commit df48704

Browse files
authored
feat: auto generate/update locale files (#252)
1 parent 5e6754d commit df48704

Some content is hidden

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

62 files changed

+406
-5
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ If you want to make `Faker` a better, please read the following contribution gui
1313
Make sure you **build** the project before running the docs, cause some files depend on `dist`.
1414
Use `pnpm run docs:dev` to edit them in live mode.
1515
- The tests are executing `vitest` against `test/**/*.spec.ts`
16+
- If you update the locales, make sure to run `pnpm run generate:locales` to generate/update the related files.
1617

1718
## Architecture
1819

docs/api/localization.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ faker.setLocale('de');
1313
faker.locale = 'de';
1414
```
1515

16+
<!-- LOCALES-AUTO-GENERATED-START -->
17+
18+
<!-- Run 'pnpm run generate:locales' to update. -->
19+
1620
| Locale | Name |
1721
| :---------- | :------------------------ |
1822
| af_ZA | Afrikaans |
@@ -72,6 +76,8 @@ faker.locale = 'de';
7276
| zh_TW | Chinese (Taiwan) |
7377
| zu_ZA | Zulu (South Africa) |
7478

79+
<!-- LOCALES-AUTO-GENERATED-END -->
80+
7581
## Individual Localization Packages
7682

7783
As of version `v3.0.0` Faker supports incremental loading of locales.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"build:code": "esno ./scripts/bundle.ts",
5757
"build:types": "tsc --emitDeclarationOnly --outDir dist/types",
5858
"build": "run-s build:clean build:code build:types",
59-
"generate:api-docs": "esno scripts/apidoc.ts",
59+
"generate:api-docs": "esno ./scripts/apidoc.ts",
60+
"generate:locales": "esno ./scripts/generateLocales.ts",
6061
"docs:build": "run-s docs:prepare docs:build:run",
6162
"docs:build:run": "vitepress build docs",
6263
"docs:build:ci": "run-s build docs:build",
@@ -89,6 +90,7 @@
8990
"@types/node": "~16.11.21",
9091
"@typescript-eslint/eslint-plugin": "~5.10.1",
9192
"@typescript-eslint/parser": "~5.10.1",
93+
"@types/prettier": "~2.4.3",
9294
"@types/validator": "~13.7.1",
9395
"@vitest/ui": "~0.2.5",
9496
"c8": "~7.11.0",

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/generateLocales.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
2+
import { resolve } from 'node:path';
3+
import type { Options } from 'prettier';
4+
import { format } from 'prettier';
5+
import options from '../.prettierrc.cjs';
6+
import type { LocaleDefinition } from '../src';
7+
8+
const pathRoot = resolve(__dirname, '..');
9+
const pathLocale = resolve(pathRoot, 'src', 'locale');
10+
const pathLocales = resolve(pathRoot, 'src', 'locales');
11+
const pathLocalesIndex = resolve(pathLocales, 'index.ts');
12+
const pathDocsApiLocalization = resolve(
13+
pathRoot,
14+
'docs',
15+
'api',
16+
'localization.md'
17+
);
18+
19+
const scriptCommand = 'pnpm run generate:locales';
20+
const prettierTsOptions: Options = { ...options, parser: 'typescript' };
21+
const prettierMdOptions: Options = { ...options, parser: 'markdown' };
22+
23+
const locales = readdirSync(pathLocales);
24+
locales.splice(locales.indexOf('index.ts'), 1);
25+
26+
let localeIndexImports = "import type { LocaleDefinition } from '..';\n";
27+
let localeIndexType = 'export type KnownLocale =\n';
28+
let localeIndexLocales = 'const locales: KnownLocales = {\n';
29+
30+
let localizationLocales = '| Locale | Name |\n| :--- | :--- |\n';
31+
32+
const autoGeneratedCommentHeader = `/*
33+
* This file is automatically generated.
34+
* Run '${scriptCommand}' to update.
35+
*/`;
36+
37+
for (const locale of locales) {
38+
// eslint-disable-next-line @typescript-eslint/no-var-requires
39+
const localeDef: LocaleDefinition = require('../src/locales/' +
40+
locale).default;
41+
const localeTitle = localeDef.title;
42+
43+
localeIndexImports += `import ${locale} from './${locale}';\n`;
44+
localeIndexType += ` | '${locale}'\n`;
45+
localeIndexLocales += ` ${locale},\n`;
46+
localizationLocales += `| ${locale} | ${localeTitle} |\n`;
47+
48+
// src/locale/<locale>.ts
49+
let content = `
50+
${autoGeneratedCommentHeader}
51+
52+
import { Faker } from '..';
53+
import ${locale} from '../locales/${locale}';
54+
${locale !== 'en' ? "import en from '../locales/en';" : ''}
55+
56+
const faker = new Faker({
57+
locale: '${locale}',
58+
localeFallback: 'en',
59+
locales: {
60+
${locale},
61+
${locale !== 'en' ? 'en,' : ''}
62+
},
63+
});
64+
65+
export = faker;
66+
`;
67+
68+
content = format(content, prettierTsOptions);
69+
writeFileSync(resolve(pathLocale, locale + '.ts'), content);
70+
}
71+
72+
// src/locales/index.ts
73+
74+
let indexContent = `
75+
${autoGeneratedCommentHeader}
76+
77+
${localeIndexImports}
78+
79+
${localeIndexType};
80+
81+
export type KnownLocales = Record<KnownLocale, LocaleDefinition>;
82+
83+
${localeIndexLocales}};
84+
85+
export default locales;
86+
`;
87+
88+
indexContent = format(indexContent, prettierTsOptions);
89+
90+
writeFileSync(pathLocalesIndex, indexContent);
91+
92+
// docs/api/localization.md
93+
94+
localizationLocales = format(localizationLocales, prettierMdOptions);
95+
96+
let localizationContent = readFileSync(pathDocsApiLocalization, 'utf-8');
97+
localizationContent = localizationContent.replace(
98+
/(^<!-- LOCALES-AUTO-GENERATED-START -->$).*(^<!-- LOCALES-AUTO-GENERATED-END -->$)/gms,
99+
`$1\n\n<!-- Run '${scriptCommand}' to update. -->\n\n${localizationLocales}\n$2`
100+
);
101+
writeFileSync(pathDocsApiLocalization, localizationContent);

src/locale/af_ZA.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import af_ZA from '../locales/af_ZA';
38
import en from '../locales/en';

src/locale/ar.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import ar from '../locales/ar';
38
import en from '../locales/en';

src/locale/az.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import az from '../locales/az';
38
import en from '../locales/en';

src/locale/cz.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import cz from '../locales/cz';
38
import en from '../locales/en';

src/locale/de.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import de from '../locales/de';
38
import en from '../locales/en';

src/locale/de_AT.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import de_AT from '../locales/de_AT';
38
import en from '../locales/en';

src/locale/de_CH.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import de_CH from '../locales/de_CH';
38
import en from '../locales/en';

src/locale/el.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import el from '../locales/el';
38
import en from '../locales/en';

src/locale/en.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en from '../locales/en';
38

src/locale/en_AU.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_AU from '../locales/en_AU';
38
import en from '../locales/en';

src/locale/en_AU_ocker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_AU_ocker from '../locales/en_AU_ocker';
38
import en from '../locales/en';

src/locale/en_BORK.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_BORK from '../locales/en_BORK';
38
import en from '../locales/en';

src/locale/en_CA.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_CA from '../locales/en_CA';
38
import en from '../locales/en';

src/locale/en_GB.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_GB from '../locales/en_GB';
38
import en from '../locales/en';

src/locale/en_GH.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_GH from '../locales/en_GH';
38
import en from '../locales/en';

src/locale/en_IE.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_IE from '../locales/en_IE';
38
import en from '../locales/en';

src/locale/en_IND.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_IND from '../locales/en_IND';
38
import en from '../locales/en';

src/locale/en_NG.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_NG from '../locales/en_NG';
38
import en from '../locales/en';

src/locale/en_US.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_US from '../locales/en_US';
38
import en from '../locales/en';

src/locale/en_ZA.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import en_ZA from '../locales/en_ZA';
38
import en from '../locales/en';

src/locale/es.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import es from '../locales/es';
38
import en from '../locales/en';

src/locale/es_MX.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import es_MX from '../locales/es_MX';
38
import en from '../locales/en';

src/locale/fa.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import fa from '../locales/fa';
38
import en from '../locales/en';

src/locale/fi.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import fi from '../locales/fi';
38
import en from '../locales/en';

src/locale/fr.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is automatically generated.
3+
* Run 'pnpm run generate:locales' to update.
4+
*/
5+
16
import { Faker } from '..';
27
import fr from '../locales/fr';
38
import en from '../locales/en';

0 commit comments

Comments
 (0)