Skip to content

i18n: extract hardcoded Chinese strings from aiapi/common.php#1152

Merged
zhblue merged 1 commit intomasterfrom
i18n/aiapi-chinese-strings
Apr 3, 2026
Merged

i18n: extract hardcoded Chinese strings from aiapi/common.php#1152
zhblue merged 1 commit intomasterfrom
i18n/aiapi-chinese-strings

Conversation

@zhblue
Copy link
Copy Markdown
Owner

@zhblue zhblue commented Apr 3, 2026

Summary

Extract hardcoded Chinese strings in web/aiapi/common.php (lines 90-140) into lang/cn.php constants for i18n support.

Changes

  • $MSG_AI_CODE_SUGGESTION_SOURCE_BROWSER
  • $MSG_AI_CODE_SUGGESTION
  • $MSG_AI_PROMPT_SYS (uses sprintf placeholder for dynamic $code_suggestion)
  • $MSG_AI_INVALID_PARAM

Files changed

  • web/aiapi/common.php: replaced 3× echo "非法参数" and 3× Chinese prompt strings with lang constants
  • web/lang/cn.php: added 4 new MSG_AI_* constants

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
Copilot AI review requested due to automatic review settings April 3, 2026 01:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in trunk/web/lang/cn.php.
  • Updated trunk/web/aiapi/common.php to 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.

Comment on lines 97 to +102
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);
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 109 to 111
if(!(isset($_SESSION[$OJ_NAME."_source_browser"])|| $user_id==$_SESSION[$OJ_NAME."_user_id"] )){
echo "非法参数";
echo $MSG_AI_INVALID_PARAM;
exit();
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$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.

Copilot uses AI. Check for mistakes.
@zhblue zhblue merged commit 8cc9354 into master Apr 3, 2026
12 checks passed
zhblue added a commit that referenced this pull request Apr 3, 2026
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
zhblue added a commit that referenced this pull request Apr 3, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants