i18n: extract hardcoded Chinese strings from aiapi/common.php#1152
i18n: extract hardcoded Chinese strings from aiapi/common.php#1152
Conversation
Extract hardcoded Chinese strings in web/aiapi/common.php (lines 90-140) into lang/cn.php constants for internationalization support. - $MSG_AI_CODE_SUGGESTION_SOURCE_BROWSER - $MSG_AI_CODE_SUGGESTION - $MSG_AI_PROMPT_SYS (with sprintf placeholder) - $MSG_AI_INVALID_PARAM
There was a problem hiding this comment.
Pull request overview
This PR extracts several hardcoded Chinese AI prompt/response strings from web/aiapi/common.php into web/lang/cn.php message constants to support i18n and reuse.
Changes:
- Added 4 new
$MSG_AI_*message constants intrunk/web/lang/cn.php. - Updated
trunk/web/aiapi/common.phpto use$MSG_AI_*constants instead of inline Chinese strings. - Switched system prompt assembly to
sprintf($MSG_AI_PROMPT_SYS, $code_suggestion).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| trunk/web/lang/cn.php | Adds new AI-related $MSG_AI_* message constants (Chinese). |
| trunk/web/aiapi/common.php | Replaces hardcoded strings with $MSG_AI_* constants and uses sprintf for prompt composition. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if(isset($_SESSION[$OJ_NAME."_source_browser"])){ | ||
| $code_suggestion="分析我可能薄弱的知识点,问我一个提示性的相关问题。"; | ||
| $code_suggestion=$MSG_AI_CODE_SUGGESTION_SOURCE_BROWSER; | ||
| }else{ | ||
| $code_suggestion="不要直接给出完整代码,只给出问题原因,让我自己学习修改。分析我可能薄弱的知识点,问我一个提示性的相关问题,最后说一句鼓励或安慰的话,卖个萌。"; | ||
| $code_suggestion=$MSG_AI_CODE_SUGGESTION; | ||
| } | ||
| $prompt_sys="你是一个经验丰富的信息学奥赛编程高手,能帮我用简单清晰的中文,解释我看不懂的报错信息。如果对比中用户的输出为空,可能是没有考虑到多组输入的情况,应该使用循环处理。$code_suggestion 请尽量言简意赅,节省token消耗。"; | ||
| $prompt_sys=sprintf($MSG_AI_PROMPT_SYS,$code_suggestion); |
There was a problem hiding this comment.
These new $MSG_AI_* variables are currently only defined in lang/cn.php. When $OJ_LANG is set to other supported languages (e.g. en/fa/ko/th/ug), common.php will hit undefined-variable notices and build empty/incorrect prompts (and invalid-param responses). To keep i18n working, add corresponding $MSG_AI_CODE_SUGGESTION_SOURCE_BROWSER / $MSG_AI_CODE_SUGGESTION / $MSG_AI_PROMPT_SYS / $MSG_AI_INVALID_PARAM entries to the other language files (at least en.php and any languages exposed via init.php), or provide a safe fallback in common.php when the message is not set.
| if(!(isset($_SESSION[$OJ_NAME."_source_browser"])|| $user_id==$_SESSION[$OJ_NAME."_user_id"] )){ | ||
| echo "非法参数"; | ||
| echo $MSG_AI_INVALID_PARAM; | ||
| exit(); |
There was a problem hiding this comment.
$MSG_AI_INVALID_PARAM is newly used here, but it’s only defined in lang/cn.php. For non-Chinese UI languages this will be undefined and the endpoint may return an empty response / PHP notice. Add this message key to the other supported language packs (en/fa/ko/th/ug) or add a fallback value before echoing.
Add MSG_AI_CODE_SUGGESTION_SOURCE_BROWSER, MSG_AI_CODE_SUGGESTION, MSG_AI_PROMPT_SYS, MSG_AI_INVALID_PARAM, MSG_AI_PROMPT_USER_TITLE, MSG_AI_PROMPT_USER_SOURCE, MSG_AI_PROMPT_USER_ERROR to all lang files. - cn.php: already present (from PR #1152, #1153) - cnt.php: Traditional Chinese translation - ko.php: Korean translation - en/bo/fa/he/mn/ru/th/ug: English placeholder
Add MSG_AI_CODE_SUGGESTION_SOURCE_BROWSER, MSG_AI_CODE_SUGGESTION, MSG_AI_PROMPT_SYS, MSG_AI_INVALID_PARAM, MSG_AI_PROMPT_USER_TITLE, MSG_AI_PROMPT_USER_SOURCE, MSG_AI_PROMPT_USER_ERROR to all lang files. - cn.php: already present (from PR #1152, #1153) - cnt.php: Traditional Chinese translation - ko.php: Korean translation - en/bo/fa/he/mn/ru/th/ug: English placeholder
Summary
Extract hardcoded Chinese strings in
web/aiapi/common.php(lines 90-140) intolang/cn.phpconstants for i18n support.Changes
$MSG_AI_CODE_SUGGESTION_SOURCE_BROWSER$MSG_AI_CODE_SUGGESTION$MSG_AI_PROMPT_SYS(usessprintfplaceholder for dynamic$code_suggestion)$MSG_AI_INVALID_PARAMFiles changed
web/aiapi/common.php: replaced 3×echo "非法参数"and 3× Chinese prompt strings with lang constantsweb/lang/cn.php: added 4 new MSG_AI_* constants