rspress-plugin-translate

Rspress plugin for translating files using LLM, currently only supports GPT.

Installation

npm
yarn
pnpm
bun
npm add rspress-plugin-translate -D

Usage

import { defineConfig } from 'rspress/config';
import path from 'path';
import { pluginTranslate } from 'rspress-plugin-translate';
import { config } from 'dotenv';

config();

export default defineConfig({
  plugins: [
    pluginTranslate({
      modelConfig: {
        clientOptions: {
          apiKey: process.env.OPENAI_API_KEY,
          httpAgent: new HttpsProxyAgent('http://127.0.0.1:7890'),
        },
      },
    }),
  ],
  root: path.join(__dirname, 'docs'),
  lang: 'en',
  locales: [
    {
      lang: 'en',
      label: 'English',
      title: 'Modern.js',
      description: 'Modern.js Documentation Framework',
    },
    {
      lang: 'zh',
      label: '简体中文',
      title: 'Modern.js',
      description: 'Rspress',
    },
  ],
});

When you start the project, the plugin will automatically translate the files in the source language folder under your document root directory into the target language and save them in the target language folder.

For files in the source language folder that do not need to be translated, they will also be automatically copied to the target language folder every time you start the project.

Note that the plugin will do a hash cache on the files in the source language folder every time you start the project. If it finds updates compared to the previous cache, it will re-translate the files, otherwise it will skip the files.

Parameter Description

modelConfig

  • Type: Object

modelConfig.clientOptions

  • Type: ClientOptions {}

Client settings for calling OpenAI, including API-KEY, Agent, etc. For details, you can go to Type Declarations to see all supported parameters.

modelConfig.model

  • Type: string
  • Default: gpt-3.5-turbo

The name of the model used to call OpenAI. You can go to the Model List to see all supported model names.

match

  • Type: Object

Customize the configuration for matching files that need to be translated.

match.exclude

  • Type: string[]
  • Default: []

Exclude certain files from translation. For example:

rspress.config.ts
import { defineConfig } from 'rspress/config';
import { pluginTranslate } from 'rspress-plugin-translate';

export default defineConfig({
  plugins: [
    pluginTranslate({
      match: {
        exclude: ['custom.tsx', 'component/**/*'],
      },
    }),
  ],
});

Note: The strings in the array support glob patterns.

match.extensions

  • Type: string[]
  • Default: []

The file extensions of files included in the translation process. By default, Rspress includes all 'md', 'mdx' files in the routes. If you want to customize the extensions, you can use this option. For example:

rspress.config.ts
import { defineConfig } from 'rspress/config';
import { pluginTranslate } from 'rspress-plugin-translate';

export default defineConfig({
  plugins: [
    pluginTranslate({
      match: {
        extensions: ['.jsx', '.md', '.mdx'],
      },
    }),
  ],
});

rateLimitPerMinute

  • Type: Number
  • Default: 3

Rate limit for the large model, default is 3 requests per minute.

getPrompt

  • Type: Function

Prompt function for the large model, which takes three parameters: content (content of the source language file), from (name of the source language), and to (name of the target language). The return value of this function will be used as the prompt parameter passed to the large model. For example:

rspress.config.ts
import { defineConfig } from 'rspress/config';
import { pluginTranslate } from 'rspress-plugin-translate';

export default defineConfig({
  plugins: [
    pluginTranslate({
      getPrompt: (
        content: string,
        from: string,
        to: string,
      ) => `A ${from} markdown file content is provided: ${content}
      The masterful ${from} translator flawlessly translates the phrase into ${to} and keep the markdown formatting:`,
    }),
  ],
});