# weedymallick-nwu/learn_chinese architecture

> Architecture diagram of the GitHub repository weedymallick-nwu/learn_chinese, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/weedymallick-nwu/learn_chinese
- Repository: https://github.com/weedymallick-nwu/learn_chinese

## Overview

#### Purpose and entry points
This is a Next.js 15 App Router PWA providing a terminal-style English-to-Simplified-Chinese translator. The browser entry route is `src/app/page.tsx`; shared document shell and global styling are defined in `src/app/layout.tsx` and `src/app/globals.css`. Translation requests enter through the server route `src/app/api/translate/route.ts`.

#### Application shell and visual system
The root layout establishes application-wide metadata, fonts, and shared client behavior. The main page renders the translator experience rather than a conventional multi-page web application. Styling is primarily global/Tailwind-driven, with the black terminal canvas and accent design applied through:
- `src/app/layout.tsx`
- `src/app/globals.css`
- `src/app/page.tsx`

#### Terminal interaction engine
`Terminal` is the central interactive client component. It owns the command-line UX: input at the `~/translate` prompt, Enter submission, animated translation output, sentence history via arrow keys, screen clearing via Ctrl+L, and local commands such as `/help`, `/clear`, and `/about`.
- `src/components/Terminal.tsx`
- `src/app/page.tsx`

Its boundary is the translation API: commands and UI-only state remain client-side, while actual language conversion is delegated to the server endpoint.

#### Translation API and provider fallback
The API handler accepts browser POSTs and returns Chinese translation data to the terminal. Its provider sequence is Google’s public translation endpoint first, followed by MyMemory when Google is unavailable. This avoids application-managed API keys but makes translation quality/availability dependent on public external services.
- `src/app/api/translate/route.ts`

The server also computes pinyin offline using `pinyin-pro`, producing romanization independently of the remote translation provider. The response supports both full-sentence pinyin and word-level Chinese/pinyin/meaning display.

#### Primary request and rendering flow
1. User submits English text in `src/components/Terminal.tsx`.
2. The client POSTs text to `/api/translate`.
3. `src/app/api/translate/route.ts` calls Google, or MyMemory on failure, and enriches the result with offline pinyin.
4. The terminal receives the result and progressively types/render translations, sentence pinyin, and word breakdowns.
5. Submitted inputs remain in client-side history for keyboard recall.

#### PWA and offline boundary
The app is installable through a web manifest and service worker registration. Offline support is principally for the UI/app shell; live translations still require network access because the translation providers are remote.
- `src/app/manifest.ts`
- `src/components/PwaRegister.tsx`
- `public/sw.js`

#### Runtime and deployment model
The project runs as a Node.js/Next.js application with TypeScript and Tailwind CSS. The App Router serves both the rendered UI and same-origin API endpoint, making deployment suitable for platforms such as Vercel. The service worker and manifest enable mobile installation without a native runtime.
- `package.json`
- `next.config.mjs`
- `src/app/api/translate/route.ts`

Main entry points: [`src/app/api/translate/route.ts`](https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/api/translate/route.ts) (Translate endpoint), [`src/app/layout.tsx`](https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/layout.tsx) (Root layout), [`package.json`](https://github.com/weedymallick-nwu/learn_chinese/blob/main/package.json) (Next.js Node runtime), [`src/components/Terminal.tsx`](https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/components/Terminal.tsx) (Terminal)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Next.js web app"]
  node_layout["Root layout<br/>App Router shell<br/>[layout.tsx]"]
  node_page["Translator page<br/>App Router page<br/>[page.tsx]"]
  node_styles["Terminal styling<br/>global CSS<br/>[globals.css]"]
  node_terminal["Terminal<br/>client component<br/>[Terminal.tsx]"]
end

subgraph group_api["Translation service"]
  node_translate_api{{"Translate endpoint<br/>Next.js route handler<br/>[route.ts]"}}
  node_google(("Google Translate<br/>public translation provider"))
  node_mymemory(("MyMemory<br/>fallback translation provider"))
  node_pinyin["pinyin-pro<br/>offline romanization library"]
end

subgraph group_pwa["PWA offline shell"]
  node_manifest["Web manifest<br/>PWA manifest<br/>[manifest.ts]"]
  node_pwa_register["PWA registration<br/>client component<br/>[PwaRegister.tsx]"]
  node_service_worker["Service worker<br/>offline app shell cache<br/>[sw.js]"]
end

node_browser(("Browser user<br/>client runtime"))
node_next_runtime{{"Next.js Node runtime<br/>application runtime<br/>[package.json]"}}
node_next_config["Next configuration<br/>runtime configuration<br/>[next.config.mjs]"]

node_browser -->|"loads app"| node_layout
node_layout -->|"renders"| node_page
node_layout -->|"applies"| node_styles
node_page -->|"hosts"| node_terminal
node_terminal -->|"POST text"| node_translate_api
node_translate_api -->|"primary translation"| node_google
node_translate_api -.->|"fallback on failure"| node_mymemory
node_translate_api -->|"computes pinyin"| node_pinyin
node_translate_api -->|"translation result"| node_terminal
node_layout -->|"shared client behavior"| node_pwa_register
node_pwa_register -->|"registers"| node_service_worker
node_service_worker -->|"supports installation"| node_manifest
node_next_runtime -->|"serves UI"| node_layout
node_next_runtime -->|"serves API"| node_translate_api
node_next_config -->|"configures"| node_next_runtime

click node_layout "https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/layout.tsx"
click node_page "https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/page.tsx"
click node_styles "https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/globals.css"
click node_terminal "https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/components/Terminal.tsx"
click node_translate_api "https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/api/translate/route.ts"
click node_manifest "https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/manifest.ts"
click node_pwa_register "https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/components/PwaRegister.tsx"
click node_service_worker "https://github.com/weedymallick-nwu/learn_chinese/blob/main/public/sw.js"
click node_next_runtime "https://github.com/weedymallick-nwu/learn_chinese/blob/main/package.json"
click node_next_config "https://github.com/weedymallick-nwu/learn_chinese/blob/main/next.config.mjs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_layout,node_page,node_styles,node_terminal toneBlue
class node_translate_api,node_google,node_mymemory,node_pinyin toneAmber
class node_manifest,node_pwa_register,node_service_worker toneMint
class node_browser,node_next_runtime,node_next_config toneNeutral
```

## Components

### Next.js web app

- [Root layout](https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/layout.tsx): App Router shell · Sets metadata, fonts, and shared client behavior. · `src/app/layout.tsx`
- [Translator page](https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/page.tsx): App Router page · Hosts the terminal translator experience. · `src/app/page.tsx`
- [Terminal styling](https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/globals.css): global CSS · Defines the terminal canvas and accent visual system. · `src/app/globals.css`
- [Terminal](https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/components/Terminal.tsx): client component · Owns commands, input history, submission, and progressive output. · `src/components/Terminal.tsx`

### Translation service

- [Translate endpoint](https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/api/translate/route.ts): Next.js route handler · Returns translated text, pinyin, and word-level data. · `src/app/api/translate/route.ts`
- **Google Translate**: public translation provider
- **MyMemory**: fallback translation provider
- **pinyin-pro**: offline romanization library · Computes pinyin independently of remote translation.

### PWA offline shell

- [Web manifest](https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/app/manifest.ts): PWA manifest · `src/app/manifest.ts`
- [PWA registration](https://github.com/weedymallick-nwu/learn_chinese/blob/main/src/components/PwaRegister.tsx): client component · Registers the service worker. · `src/components/PwaRegister.tsx`
- [Service worker](https://github.com/weedymallick-nwu/learn_chinese/blob/main/public/sw.js): offline app shell cache · Supports installable UI; live translation remains network-bound. · `public/sw.js`

### Other components

- **Browser user**: client runtime
- [Next.js Node runtime](https://github.com/weedymallick-nwu/learn_chinese/blob/main/package.json): application runtime · Serves the App Router UI and same-origin API. · `package.json`
- [Next configuration](https://github.com/weedymallick-nwu/learn_chinese/blob/main/next.config.mjs): runtime configuration · `next.config.mjs`

## Connections

- Browser user → Root layout: loads app
- Root layout → Translator page: renders
- Root layout → Terminal styling: applies
- Translator page → Terminal: hosts
- Terminal → Translate endpoint: POST text
- Translate endpoint → Google Translate: primary translation
- Translate endpoint → MyMemory: fallback on failure
- Translate endpoint → pinyin-pro: computes pinyin
- Translate endpoint → Terminal: translation result
- Root layout → PWA registration: shared client behavior
- PWA registration → Service worker: registers
- Service worker → Web manifest: supports installation
- Next.js Node runtime → Root layout: serves UI
- Next.js Node runtime → Translate endpoint: serves API
- Next configuration → Next.js Node runtime: configures
