Skip to content

Commit

Permalink
✨ feat: Add includeMatter config to lobe i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Mar 24, 2024
1 parent a2c761d commit 3181ed9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/lobe-i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ $ lobe-i18n
| outputExtensions | | `function` | `(locale) => '.{locale}.md'` | Output file extension generation |
| mode | | `string`,`mdast`,`function` | `string` | Translation mode selection, explained below |
| translateCode | | `boolean` | `false` | Whether to translate code blocks under `mdast`, other modes are invalid |
| includeMatter | | `boolean` | `false` | Whether to include front matter in the translation |

#### `outputExtensions`

Expand Down
1 change: 1 addition & 0 deletions packages/lobe-i18n/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ $ lobe-i18n
| outputExtensions | | `function` | `(locale) => '.{locale}.md'` | 输出文件的扩展名生成 |
| mode | | `string`,`mdast`,`function` | `string` | 翻译的模式选择,解释见下文 |
| translateCode | | `boolean` | `false` |`mdast` 下是否翻译代码块,其他模式无效 |
| includeMatter | | `boolean` | `false` | 是否翻译 `front matter` |

#### `outputExtensions`

Expand Down
1 change: 1 addition & 0 deletions packages/lobe-i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"fast-deep-equal": "^3",
"glob": "^10",
"gpt-tokenizer": "^2",
"gray-matter": "^4",
"ink": "^4.2",
"json-stable-stringify": "^1",
"just-diff": "^6",
Expand Down
12 changes: 10 additions & 2 deletions packages/lobe-i18n/src/commands/TranslateMarkdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { alert, render } from '@lobehub/cli-ui';
import chalk from 'chalk';
import { consola } from 'consola';
import { globSync } from 'glob';
import matter from 'gray-matter';
import { isString } from 'lodash-es';
import { existsSync } from 'node:fs';
import { relative, resolve } from 'node:path';
Expand Down Expand Up @@ -94,7 +95,12 @@ class TranslateMarkdown {
clear();
const outputPath = relative('.', item.filename);
if (data?.result && Object.keys(data.result).length > 0) {
writeMarkdown(item.filename, data.result);
let mdResut = data.result;
if (!this.markdownConfig.includeMatter) {
mdResut = matter.stringify(data.result, item.matter);
}

writeMarkdown(item.filename, mdResut);
totalTokenUsage += data.tokenUsage;
consola.success(chalk.yellow(outputPath), chalk.gray(`[Token usage: ${data.tokenUsage}]`));
} else {
Expand All @@ -120,11 +126,13 @@ class TranslateMarkdown {
const targetFilename = this.getTargetFilename(file, targetExtension);
if (existsSync(targetFilename)) continue;
const mode = this.getMode(file, md);
const { data, content } = matter(md);
consola.info(`📄 To ${locale}: ${chalk.yellow(file)}`);
this.query.push({
filename: targetFilename,
from: config.entryLocale,
md,
matter: data,
md: this.markdownConfig.includeMatter ? md : content,
mode,
to: locale,
});
Expand Down
1 change: 1 addition & 0 deletions packages/lobe-i18n/src/core/I18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface I18nTranslateOptions {

export interface I18nMarkdownTranslateOptions
extends Pick<I18nTranslateOptions, 'from' | 'to' | 'onProgress'> {
matter?: any;
md: string;
mode: MarkdownModeType;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/lobe-i18n/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export interface MarkdownConfig {
* @description The markdown that will ignore, support glob
*/
exclude?: string[];
/**
* @description Whether to include matter in the translation
*/
includeMatter?: boolean;
/**
* @description Markdown translate mode
*/
Expand Down

0 comments on commit 3181ed9

Please sign in to comment.