# chinayangxiaowei/marktext-chinese-language-pack architecture

> Architecture diagram of the GitHub repository chinayangxiaowei/marktext-chinese-language-pack, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chinayangxiaowei/marktext-chinese-language-pack
- Repository: https://github.com/chinayangxiaowei/marktext-chinese-language-pack

## Overview

##### Project type and purpose
- This is an open-source language-pack project for MarkText (an Electron-Vue based Markdown editor).
- Its purpose is to inject Simplified Chinese translations into the MarkText build by replacing resource files and running an automated translation script at compile time.

##### File structure
- Top level:
- .electron-vue/build.js  → custom build script that overrides the official MarkText build step
- README.md               → project description and instructions
- marktext_asar_translate.ts → TypeScript translation script that processes text resources
- translate-resources/    → directory containing translation dictionaries and exclusion lists
- Patterns & clues:
- A replacement approach (“resource-swap”) rather than modifying core MarkText logic
- A standalone TS script invoked at build time to generate translated assets based on environment variable `lang`
- Dependency on the host MarkText codebase (you copy these files into the official source)

##### README for additional insights
- Describes how to integrate with MarkText’s source tree (drop-in replacement of build.js)
- Explains environment variable controls and build steps (yarn install, ts-node, yarn build)
- Mentions extensibility for other languages via additional `translate-resources/xxx_[lang].txt` files

##### Main components
a. MarkText Official Codebase
– Electron-Vue application
– Original build pipeline (npm/yarn → webpack/electron-builder)
b. Custom Build Override
– `.electron-vue/build.js` (replaces the original)
c. Translation Engine
– `marktext_asar_translate.ts` (reads dictionaries, performs translation passes)
d. Translation Resources
– `translate-resources/*` (dictionary files and exclude lists)
e. Build Environment
– Node.js / yarn / ts-node / typescript
– Environment variable `lang` specifying target locale
f. Output Artifacts
– Translated resource files embedded into the ASAR package
– Final Electron app installer/bundle

##### Relationships & interactions
- The Developer sets `lang=zh-cn` (or other) in the shell
- `yarn build` invokes the custom build.js
- build.js triggers marktext_asar_translate.ts via ts-node
- translate script loads all files in translate-resources, applies translations to MarkText’s JSON/label files
- The modified resource files are injected back into the Electron build pipeline
- Electron-builder packages the final localized MarkText app

##### Architectural patterns & design principles
- Plugin/override pattern: swapping out a single build script instead of forking the entire codebase
- Resource-replacement strategy: externalizing translations for easy updates and additions of new languages
- Environment-driven configuration: building for different locales by setting an env var
- Separation of concerns: build orchestration (build.js) vs. translation logic (TS script) vs. data (dict files)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Development Setup"
        Dev["Developer"]:::external
        Env["Env Var: lang"]:::external
        Node["Node.js / yarn / ts-node"]:::external
        Dev -->|sets| Env
        Env -->|used by| Node
    end

    subgraph "Build Time"
        BuildJS["Build Override Script"]:::custom
        TS["Translation Engine (TS script)"]:::custom
        ResourcesDir["Translation Resources"]:::data
        Exclude["Exclude List"]:::data
        MainDict["Main Dict (zh-cn)"]:::data
        LabelDict["Label Dict (zh-cn)"]:::data
        RendDict["Renderer Dict (zh-cn)"]:::data
        Patched["Patched Resource Files"]:::data
        ElectronBP["Electron Build Pipeline"]:::external
        Installer["Final App Bundle"]:::output

        Env -->|invokes with| BuildJS
        BuildJS -->|executes via ts-node| TS
        TS -->|reads| ResourcesDir
        ResourcesDir --> Exclude
        ResourcesDir --> MainDict
        ResourcesDir --> LabelDict
        ResourcesDir --> RendDict
        TS -->|outputs| Patched
        Patched -->|injected into| ElectronBP
        ElectronBP -->|produces| Installer
    end

    classDef external fill:#ffa500,color:#000;
    classDef custom fill:#00aaff,color:#000;
    classDef data fill:#00ff00,color:#000;
    classDef output fill:#bf00ff,color:#000;

    click BuildJS "https://github.com/chinayangxiaowei/marktext-chinese-language-pack/blob/main/.electron-vue/build.js"
    click TS "https://github.com/chinayangxiaowei/marktext-chinese-language-pack/blob/main/marktext_asar_translate.ts"
    click ResourcesDir "https://github.com/chinayangxiaowei/marktext-chinese-language-pack/tree/main/translate-resources/"
    click Exclude "https://github.com/chinayangxiaowei/marktext-chinese-language-pack/blob/main/translate-resources/exclude.txt"
    click MainDict "https://github.com/chinayangxiaowei/marktext-chinese-language-pack/blob/main/translate-resources/main_dict_zh-cn.txt"
    click LabelDict "https://github.com/chinayangxiaowei/marktext-chinese-language-pack/blob/main/translate-resources/main_label_dict_zh-cn.txt"
    click RendDict "https://github.com/chinayangxiaowei/marktext-chinese-language-pack/blob/main/translate-resources/renderer_dict_zh-cn.txt"
```
