ASP中使用英文和汉字时的编码问题

发布于 2024-11-14 09:43:27 字数 1321 浏览 2 评论 0原文

我在 ASP 站点中编码中文时遇到问题。文件格式为:

  • Translations.txt - UTF-8(用于存储我的翻译)
  • test.asp - UTF-8 -(用于呈现页面)

test.asp 正在读取包含以下数据的 Translations.txt:

Help|ZH|帮助 
Home|ZH|首页

test.asp在管道分隔符上分割,如果用户包含带有 ZH 的 cookie,它将显示此翻译,否则它将恢复为 Key 值。

现在,我尝试了以下方法,但没有成功:

  1. 添加元标记

  2. 设置Response.CharSet = "UTF-8"

  3. 设置 Response.ContentType = "text/html"
  4. 将 Session.CodePage(和 Response)设置为 65001 (UTF-8) )
  5. 我已经确认 translations.txt 中的文本肯定是 UTF-8 格式并且没有字节顺序标记
  6. 浏览器发现该页面是 Unicode UTF-8 ,但是页面正在显示冗长的书。
  7. 无论 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:

  1. Add a meta tag

    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>

  2. Set the Response.CharSet = "UTF-8"

  3. Set the Response.ContentType = "text/html"
  4. Set the Session.CodePage (and Response) to both 65001 (UTF-8)
  5. I have confirmed that the text in translations.txt is definitely in UTF-8 and has no byte order mark
  6. The browser is picking up that the page is Unicode UTF-8, but the page is displaying gobbledegook.
  7. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

同展鸳鸯锦 2024-11-21 09:43:27

为了读取 UTF-8 文件,您可能需要使用 ADODB.Stream 对象。我并不声称自己是字符编码方面的专家,但这个测试对我有用:

test.txt(保存为没有BOM的UTF-8):

首页
帮助

测试.vbs

Option Explicit

Const adTypeText = 2
Const adReadLine = -2

Dim stream : Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = adTypeText
stream.Charset = "UTF-8"
stream.LoadFromFile "test.txt"

Do Until stream.EOS
    WScript.Echo stream.ReadText(adReadLine)
Loop

stream.Close

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

Option Explicit

Const adTypeText = 2
Const adReadLine = -2

Dim stream : Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = adTypeText
stream.Charset = "UTF-8"
stream.LoadFromFile "test.txt"

Do Until stream.EOS
    WScript.Echo stream.ReadText(adReadLine)
Loop

stream.Close
长不大的小祸害 2024-11-21 09:43:27

无论该过程的哪个部分正在读取 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.

冷默言语 2024-11-21 09:43:27

Scripting.OpenTextFile 根本不理解 UTF-8。它只能读取当前的 OEM 编码或 Unicode。从某些字符集使用的字节数可以看出,UTF-8 的效率相当低。对于此类数据,我建议使用 Unicode。

您应该将文件另存为 Unicode(Windows 术语),然后使用以下命令打开:

Dim stream : Set stream = Scripting.OpenTextFile(yourFilePath, 1, false, -1)

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:

Dim stream : Set stream = Scripting.OpenTextFile(yourFilePath, 1, false, -1)
请持续率性 2024-11-21 09:43:27

只需使用页面顶部的以下脚本即可

Response.CodePage=65001
Response.CharSet="UTF-8"

Just use the script below at the top of your page

Response.CodePage=65001
Response.CharSet="UTF-8"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文