-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: auto generate/update locale files #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f2cdf13
feat: auto generate locale files
ST-DDT a1b9dff
feat: auto generate locales in docs
ST-DDT 7142012
chore(docs): use locale table instead of list
ST-DDT e18a4a6
feat: add autogenerated header
ST-DDT f347a56
chore: add command to generate locale related files
ST-DDT db6d136
chore: restore original export behavior
ST-DDT 37754e7
fix: also generate en locale file
ST-DDT d36cb66
chore: rename generation command
ST-DDT e2b7473
chore: use const instead of let
Shinigami92 7a5497b
chore: fix lint warnings
ST-DDT 31c5ebc
Merge branch 'main' into feature/locales/autogenerate
ST-DDT 7e5a160
Merge branch 'main' into feature/locales/autogenerate
ST-DDT fd3ca20
Merge branch 'main' into feature/locales/autogenerate
ST-DDT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { readdirSync, readFileSync, writeFileSync } from 'node:fs'; | ||
| import { resolve } from 'node:path'; | ||
| import type { Options } from 'prettier'; | ||
| import { format } from 'prettier'; | ||
| import options from '../.prettierrc.cjs'; | ||
| import type { LocaleDefinition } from '../src'; | ||
|
|
||
| const pathRoot = resolve(__dirname, '..'); | ||
| const pathLocale = resolve(pathRoot, 'src', 'locale'); | ||
| const pathLocales = resolve(pathRoot, 'src', 'locales'); | ||
| const pathLocalesIndex = resolve(pathLocales, 'index.ts'); | ||
| const pathDocsApiLocalization = resolve( | ||
| pathRoot, | ||
| 'docs', | ||
| 'api', | ||
| 'localization.md' | ||
| ); | ||
|
|
||
| const scriptCommand = 'pnpm run generate:locales'; | ||
| const prettierTsOptions: Options = { ...options, parser: 'typescript' }; | ||
| const prettierMdOptions: Options = { ...options, parser: 'markdown' }; | ||
|
|
||
| const locales = readdirSync(pathLocales); | ||
| locales.splice(locales.indexOf('index.ts'), 1); | ||
|
|
||
| let localeIndexImports = "import type { LocaleDefinition } from '..';\n"; | ||
| let localeIndexType = 'export type KnownLocale =\n'; | ||
| let localeIndexLocales = 'const locales: KnownLocales = {\n'; | ||
|
|
||
| let localizationLocales = '| Locale | Name |\n| :--- | :--- |\n'; | ||
|
|
||
| const autoGeneratedCommentHeader = `/* | ||
| * This file is automatically generated. | ||
| * Run '${scriptCommand}' to update. | ||
| */`; | ||
|
|
||
| for (const locale of locales) { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const localeDef: LocaleDefinition = require('../src/locales/' + | ||
| locale).default; | ||
| const localeTitle = localeDef.title; | ||
|
|
||
| localeIndexImports += `import ${locale} from './${locale}';\n`; | ||
| localeIndexType += ` | '${locale}'\n`; | ||
| localeIndexLocales += ` ${locale},\n`; | ||
| localizationLocales += `| ${locale} | ${localeTitle} |\n`; | ||
|
|
||
| // src/locale/<locale>.ts | ||
| let content = ` | ||
| ${autoGeneratedCommentHeader} | ||
|
|
||
| import { Faker } from '..'; | ||
| import ${locale} from '../locales/${locale}'; | ||
| ${locale !== 'en' ? "import en from '../locales/en';" : ''} | ||
|
|
||
| const faker = new Faker({ | ||
| locale: '${locale}', | ||
| localeFallback: 'en', | ||
| locales: { | ||
| ${locale}, | ||
| ${locale !== 'en' ? 'en,' : ''} | ||
| }, | ||
| }); | ||
|
|
||
| export = faker; | ||
| `; | ||
|
|
||
| content = format(content, prettierTsOptions); | ||
| writeFileSync(resolve(pathLocale, locale + '.ts'), content); | ||
| } | ||
|
|
||
| // src/locales/index.ts | ||
|
|
||
| let indexContent = ` | ||
| ${autoGeneratedCommentHeader} | ||
|
|
||
| ${localeIndexImports} | ||
|
|
||
| ${localeIndexType}; | ||
|
|
||
| export type KnownLocales = Record<KnownLocale, LocaleDefinition>; | ||
|
|
||
| ${localeIndexLocales}}; | ||
|
|
||
| export default locales; | ||
| `; | ||
|
|
||
| indexContent = format(indexContent, prettierTsOptions); | ||
|
|
||
| writeFileSync(pathLocalesIndex, indexContent); | ||
|
|
||
| // docs/api/localization.md | ||
|
|
||
| localizationLocales = format(localizationLocales, prettierMdOptions); | ||
|
|
||
| let localizationContent = readFileSync(pathDocsApiLocalization, 'utf-8'); | ||
| localizationContent = localizationContent.replace( | ||
| /(^<!-- LOCALES-AUTO-GENERATED-START -->$).*(^<!-- LOCALES-AUTO-GENERATED-END -->$)/gms, | ||
| `$1\n\n<!-- Run '${scriptCommand}' to update. -->\n\n${localizationLocales}\n$2` | ||
| ); | ||
| writeFileSync(pathDocsApiLocalization, localizationContent); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.