Skip to content

Conversation

@jovicdev97
Copy link

fixes issue #297

Problem:
Running the application on systems with non-Chinese locales causes a UnicodeEncodeError during import. The application crashes immediately on startup before any functionality can be used. The strftime() function uses the system's locale encoding to process the format string. On systems without Chinese locale support (e.g., cp1252 on German Windows), the characters 年, 月, 日 cannot be encoded.

Error Message:
File "...\phone_agent\config\prompts_zh.py", line 8, in <module> formatted_date = today.strftime("%Y年%m月%d日") + " " + weekday ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: encoding error

Fix:

Replaced strftime() with f-string formatting to avoid locale-dependent encoding

Changes:

old:
formatted_date = today.strftime("%Y年%m月%d日") + " " + weekday
.
.
.
new:
formatted_date = f"{today.year}年{today.month:02d}月{today.day:02d}日 {weekday}"

@jovicdev97
Copy link
Author

修复 issue #297

问题:
在非中文语言环境的系统上运行应用程序时,导入过程中会出现 UnicodeEncodeError,应用程序在启动时立即崩溃。strftime() 函数使用系统的区域编码来处理格式字符串。在不支持中文区域设置的系统上(例如德语 Windows 上的 cp1252),字符 年、月、日 无法被编码。

错误信息:
File "...\phone_agent\config\prompts_zh.py", line 8, in <module> formatted_date = today.strftime("%Y年%m月%d日") + " " + weekday ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: encoding error

修复方案:
使用 f-string 格式化替代 strftime(),避免依赖于区域设置的编码。

修改内容:

修改前:
formatted_date = today.strftime("%Y年%m月%d日") + " " + weekday
修改后:
formatted_date = f"{today.year}年{today.month:02d}月{today.day:02d}日 {weekday}"

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.

1 participant