Skip to content

Commit

Permalink
chore(global): fix translations
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed May 8, 2024
1 parent 4b55291 commit a4e2b2b
Show file tree
Hide file tree
Showing 44 changed files with 4,033 additions and 6,207 deletions.
2 changes: 2 additions & 0 deletions devtools/packages/helpers/constants.ts
@@ -1,10 +1,12 @@
import { DocsConfig } from './models';

export const SRC_PATH = `${process.cwd()}/src`;
export const PRESETS_PATH = `${process.cwd()}/src/options/presets.ts`;

export const DOCS_CONFIG: Record<string, DocsConfig> = {
['typedoc-plugin-markdown']: {
declarationsPath: `${process.cwd()}/src/options/declarations.ts`,
translatablePath: `${process.cwd()}/src/app/translatable.ts`,
optionsPath: '/docs',
docsPath: '/docs',
declarations: true,
Expand Down
1 change: 1 addition & 0 deletions devtools/packages/helpers/models.ts
@@ -1,5 +1,6 @@
export interface DocsConfig {
declarationsPath?: string;
translatablePath?: string;
presetsPath?: string;
optionsPath: string;
docsPath: string;
Expand Down
88 changes: 68 additions & 20 deletions devtools/packages/prebuild-options/tasks/generate-models.ts
@@ -1,4 +1,4 @@
import { DocsConfig } from '@devtools/helpers';
import { DocsConfig, SRC_PATH } from '@devtools/helpers';
import * as fs from 'fs';
import * as path from 'path';
import * as prettier from 'prettier';
Expand All @@ -11,37 +11,76 @@ import { DeclarationOption, ParameterType } from 'typedoc';
export async function generateOptionsModels(docsConfig: DocsConfig) {
const optionsConfig = await import(docsConfig.declarationsPath as string);

const mixedTypes = (Object.entries(optionsConfig) as any).filter(
([name, option]) =>
option.type === ParameterType.Mixed && option.defaultValue,
const sortedOptionsConfig = Object.fromEntries(
Object.entries(optionsConfig).sort((a, b) => a[0].localeCompare(b[0])),
);

const containsManuallyValidatedOptions = Object.values(optionsConfig).some(
await writeTypeDocDeclarations(docsConfig, sortedOptionsConfig);
await writeOptionsTypes(docsConfig, sortedOptionsConfig);
}

async function writeTypeDocDeclarations(
docsConfig: DocsConfig,
sortedOptionsConfig: any,
) {
const typedocDeclarationsFile = path.join(SRC_PATH, 'defs', 'typedoc.d.ts');

const containsManuallyValidatedOptions = Object.values(
sortedOptionsConfig,
).some(
(option) =>
(option as any).type === ParameterType.Mixed &&
(option as any).defaultValue,
);

const sortedOptionsConfig = Object.fromEntries(
Object.entries(optionsConfig).sort((a, b) => a[0].localeCompare(b[0])),
const out: string[] = [];

out.push(
`// THIS FILE IS AUTO GENERATED FROM THE OPTIONS CONFIG. DO NOT EDIT DIRECTLY.
import { TextContentMappings } from '../public-api';
${
containsManuallyValidatedOptions &&
`import { ManuallyValidatedOption } from 'typedoc'`
};
`,
);
out.push(`declare module 'typedoc' {`);
out.push(`export interface TypeDocOptionMap {
${(Object.entries(sortedOptionsConfig) as any)
.map(([name, option]) => `${name}: ${getType(name, option)};`)
.join('\n')}
}`);

if (docsConfig.translatablePath) {
const { translatable } = await import(docsConfig.translatablePath);
out.push(`
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Internationalization {
export interface TranslatableStrings ${getTranslations(translatable)}
}`);
}
out.push(`}`);
const formatted = await prettier.format(out.join('\n'), {
parser: 'typescript',
singleQuote: true,
trailingComma: 'all',
});

fs.writeFileSync(typedocDeclarationsFile, formatted);
}

async function writeOptionsTypes(
docsConfig: DocsConfig,
sortedOptionsConfig: any,
) {
const mixedTypes = (Object.entries(sortedOptionsConfig) as any).filter(
([name, option]) =>
option.type === ParameterType.Mixed && option.defaultValue,
);

const optionsOutput = `
// THIS FILE IS AUTO GENERATED FROM THE OPTIONS CONFIG. DO NOT EDIT DIRECTLY.
${
containsManuallyValidatedOptions &&
`import { ManuallyValidatedOption } from 'typedoc'`
};
declare module 'typedoc' {
export interface TypeDocOptionMap {
${(Object.entries(sortedOptionsConfig) as any)
.map(([name, option]) => `${name}: ${getType(name, option)};`)
.join('\n')}
}
}
/**
* Describes the options declared by the plugin.
*
Expand Down Expand Up @@ -94,6 +133,15 @@ ${name}: ${getType(name, option, true)};`,
fs.writeFileSync(optionsModelFile, formatted);
}

function getTranslations(inputObject: { [key: string]: string }) {
const output: { [key: string]: string[] } = {};
for (const [key, value] of Object.entries(inputObject)) {
const matches = value.match(/{\d+}/g) || [];
output[key] = matches.map(() => 'string');
}
return JSON.stringify(output).replace(/"/g, '');
}

function getComments(name: string) {
if (name === 'textContentMappings') {
return 'Describes the keys available to replace static text.';
Expand Down
38 changes: 2 additions & 36 deletions docs/pages/docs/options.mdx
Expand Up @@ -219,7 +219,7 @@ This option has no effect when [`readme`](https://typedoc.org/options/input/#rea
By default directories are split by scopes when generating file paths.

This option will remove reference to `@scope` in the path when generating files and directories. It does not effect the name of the package or module in the output.
This option will remove reference to `@scope` in the path when generating files and directories. It does not affect the name of the package or module in the output.

The following will be the directory structure for packages named `@scope/package-1` and `@scope/package-2`:

Expand Down Expand Up @@ -545,41 +545,7 @@ Only keys that require translation need to be added to the object.
"label.source": "Source",
"label.type": "Type",
"label.typeDeclaration": "Type declaration",
"label.value": "Value",
"kind.accessor.singular": "Accessor",
"kind.accessor.plural": "Accessors",
"kind.class.singular": "Class",
"kind.class.plural": "Classes",
"kind.constructor.singular": "Constructor",
"kind.constructor.plural": "Constructors",
"kind.enum.singular": "Enumeration",
"kind.enum.plural": "Enumerations",
"kind.enumMember.singular": "Enumeration Member",
"kind.enumMember.plural": "Enumeration Members",
"kind.event.singular": "Event",
"kind.event.plural": "Events",
"kind.function.singular": "Function",
"kind.function.plural": "Functions",
"kind.interface.singular": "Interface",
"kind.interface.plural": "Interfaces",
"kind.method.singular": "Method",
"kind.method.plural": "Methods",
"kind.module.singular": "Module",
"kind.module.plural": "Modules",
"kind.namespace.singular": "Namespace",
"kind.namespace.plural": "Namespaces",
"kind.variable.singular": "Variable",
"kind.variable.plural": "Variables",
"kind.parameter.singular": "Parameter",
"kind.parameter.plural": "Parameters",
"kind.property.singular": "Property",
"kind.property.plural": "Properties",
"kind.reference.singular": "Reference",
"kind.reference.plural": "References",
"kind.typeAlias.singular": "Type alias",
"kind.typeAlias.plural": "Type Aliases",
"kind.typeParameter.singular": "Type parameter",
"kind.typeParameter.plural": "Type parameters"
"label.value": "Value"
}
}
```
Expand Down

0 comments on commit a4e2b2b

Please sign in to comment.