ASP中使用英文和汉字时的编码问题
我在 ASP 站点中编码中文时遇到问题。文件格式为:
- Translations.txt - UTF-8(用于存储我的翻译)
- test.asp - UTF-8 -(用于呈现页面)
test.asp 正在读取包含以下数据的 Translations.txt:
Help|ZH|帮助
Home|ZH|首页
test.asp在管道分隔符上分割,如果用户包含带有 ZH 的 cookie,它将显示此翻译,否则它将恢复为 Key 值。
现在,我尝试了以下方法,但没有成功:
添加元标记
设置
Response.CharSet = "UTF-8"
- 设置
Response.ContentType = "text/html"
- 将 Session.CodePage(和 Response)设置为 65001 (UTF-8) )
- 我已经确认
translations.txt
中的文本肯定是 UTF-8 格式并且没有字节顺序标记 - 浏览器发现该页面是 Unicode UTF-8 ,但是页面正在显示冗长的书。
- 无论 Encoding 参数如何,
Scripting.OpenTextFile(
方法都会返回相同的错误文本。, , , )
这是我想要在中国(ZH)显示的示例:
- 首页
- 帮助
但是显示以下内容:
- 霖页
- 帮助
所有测试的浏览器都会出现这种情况 - Google Chrome、IE 7/8、和Firefox 4。该字体肯定有中文分支的字形。另外,我确实安装了东方语言。
--
我尝试将原始值粘贴到 HTML 中,这确实有效(但请注意这是一个硬编码值)。
- 首页
- é�–é¡
然而,这很奇怪。
首页 --(十六进制)-- > E9 A6 96 E9 A1 --(作为字符)--> é...–é¡
我有什么想法吗?
I am having problems with encoding Chinese in an ASP site. The file formats are:
- translations.txt - UTF-8 (to store my translations)
- test.asp - UTF-8 - (to render the page)
test.asp is reading translations.txt that contains the following data:
Help|ZH|帮助
Home|ZH|首页
The test.asp splits on the pipe delimiter and if the user contains a cookie with ZH, it will display this translation, else it will just revert back to the Key value.
Now, I have tried the following things, which have not worked:
Add a meta tag
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
Set the
Response.CharSet = "UTF-8"
- Set the
Response.ContentType = "text/html"
- Set the Session.CodePage (and Response) to both 65001 (UTF-8)
- I have confirmed that the text in
translations.txt
is definitely in UTF-8 and has no byte order mark - The browser is picking up that the page is Unicode UTF-8, but the page is displaying gobbledegook.
- The
Scripting.OpenTextFile(<file>,<create>,<iomode>,<encoding>)
method returns the same incorrect text regardless of the Encoding parameter.
Here is a sample of what I want to be displayed in China (ZH):
- 首页
- 帮助
But the following is displayed:
- 首页
- 帮助
This occurs all tested browsers - Google Chrome, IE 7/8, and Firefox 4. The font definitely has a Chinese branch of glyphs. Also, I do have Eastern languages installed.
--
I have tried pasting in the original value into the HTML, which did work (but note this is a hard coded value).
- 首页
- 首页
However, this is odd.
首页 --(in hex)--> E9 A6 96 E9 A1 --(as chars)--> 首页
Any ideas what I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了读取 UTF-8 文件,您可能需要使用
ADODB.Stream
对象。我并不声称自己是字符编码方面的专家,但这个测试对我有用:test.txt(保存为没有BOM的UTF-8):
测试.vbs
In order to read the UTF-8 file, you'll probably need to use the
ADODB.Stream
object. I don't claim to be an expert on character encoding, but this test worked for me:test.txt (saved as UTF-8 without BOM):
test.vbs
无论该过程的哪个部分正在读取
translations.txt
文件,似乎都不了解该文件采用 UTF-8 格式。看起来它正在将其作为其他编码读取。您应该在打开和读取该文件的任何进程中指定编码。这将与您网页的编码不同。在该文件的开头插入字节顺序标记也可能是一种解决方案。
Whatever part of the process is reading the
translations.txt
file does not seem to understand that the file is in UTF-8. It looks like it is reading it in as some other encoding. You should specify encoding in whatever process is opening and reading that file. This will be different from the encoding of your web page.Inserting the byte order mark at the beginning of that file may also be a solution.
Scripting.OpenTextFile
根本不理解 UTF-8。它只能读取当前的 OEM 编码或 Unicode。从某些字符集使用的字节数可以看出,UTF-8 的效率相当低。对于此类数据,我建议使用 Unicode。您应该将文件另存为 Unicode(Windows 术语),然后使用以下命令打开:
Scripting.OpenTextFile
does not understand UTF-8 at all. It can only read the current OEM encoding or Unicode. As you can see from the number of bytes being used for some character sets UTF-8 is quite inefficient. I would recommend Unicode for this sort of data.You should save the file as Unicode (in Windows parlance) and then open with:
只需使用页面顶部的以下脚本即可
Just use the script below at the top of your page