Chinese Text Safe
Overview
Keep Chinese text readable end to end. Emit Chinese characters directly whenever the target format supports them, and treat encoding as part of the implementation.
Core Rules
- Write Chinese literals directly in code, config, templates, fixtures, and test data.
- Do not replace Chinese with
???, mojibake,\u4e2d\u6587, HTML entities, percent-encoding, or Base64 unless the target format strictly requires it. - Prefer UTF-8 for files. Match an existing repository convention only when it is already deliberate and safe for Chinese text.
- Disable serializer settings that escape non-ASCII by default. Example: Python JSON should use
ensure_ascii=False. - Prefer
.xlsxover.csvfor Excel delivery. If CSV is required for Microsoft Excel on Windows, write UTF-8 with BOM such asutf-8-sigunless the project already uses another proven Excel-safe path. - If a terminal renders Chinese incorrectly but the saved file bytes are correct, keep the readable UTF-8 file and note the console limitation instead of downgrading the text to escapes.
Workflow
- Detect whether the task contains Chinese labels, messages, UI copy, sample data, or exported content.
- Keep the literal Chinese text readable in source files whenever the format allows it.
- Set reader, writer, serializer, response, and export encodings explicitly instead of relying on platform defaults.
- Add a focused verification step when generating CSV, Excel, or download code. Use a sample such as
错误:学号重复. - If the surrounding toolchain cannot safely preserve raw Chinese, explain the constraint and choose the narrowest necessary escaping.
Common Fixes
- Python JSON: use
json.dumps(data, ensure_ascii=False). - Python text files: pass
encoding="utf-8"; for Excel-facing CSV on Windows useencoding="utf-8-sig". - Node.js text output: write with
utf8; for Excel-facing CSV prepend BOM when needed. - Java or Kotlin: use
StandardCharsets.UTF_8explicitly for file I/O and HTTP responses. - C#: use
new UTF8Encoding(true)when BOM is needed for Excel-facing CSV. - Browser downloads: set
charset=utf-8; if exporting CSV for Excel, include BOM or switch to.xlsx. - Spreadsheet libraries: prefer native
.xlsxwriters because worksheet text is Unicode-safe by design.
Before Finishing
- Confirm the final file actually contains readable Chinese characters.
- Confirm sample exports open correctly in the target app.
- Leave Chinese literals readable in code unless a strict protocol forbids it.