There is a customer who wants to make a Russian version of his website. I use UTF-8 encoding for the front and backends, but the static Russian information generated through the database is garbled. If displayed using GB2312, the static Russian information generated through the database is displayed correctly, but the Russian text in the template file is garbled.
The specific characteristics are as follows:
1. Realize normal Russian language through templates.
2. The static Russian information generated through the backend database is displayed normally in the backend but garbled in the frontend.
3. English is normal.
Similar or different expressions related to the topic
FSO writes UTF-8 encoded files
How can FSO generate utf-8 encoded files?
What is the default encoding format of files generated by FSO?
How to convert to UTF-8 encoding
Problem with FSO generating static web pages
The code for using FSO to generate files in ASP is as follows:
Copy the code code as follows:
function createfile(sfilename,scontent)
set fso=server.CreateObject("scripting.filesystemobject")
set f1=fso.opentextfile(sfilename,2,true,-1)
f1.write(scontent)
f1.close
set fso=nothing
end function
fso.opentextfile(sfilename,2,true,-1) The last parameter -1 specifies the encoding format as Unicode. When the encoding format is Unicode, the web page displays UTF-16 little-endian.
OpenTextFile method
Creates the specified file and returns a TextStream object, which can be used to read or write the created file.
object.OpenTextFile(filename[, iomode[, create[, format]]])
Parameter object, required. Should be the name of a FileSystemObjec or Folder object. This is fso.
filename, required. A string expression specifying the file to create. Here is sfilename.
iomode, optional parameter, indicates whether the file is used for reading, writing, or appending. The default value is 1 for reading, 2 for writing, and 8 for appending.
create, optional parameter, whether to create the file if it does not exist, the default is false, if you want to create it, set it to true.
format, the parameter can be one of the following settings, the default is ASCII format, and there is no utf-8 we want:
TristateUseDefault -2 opens the file in the system default format.
TristateTrue -1 opens the file in Unicode format.
TristateFalse 0 Open the file in ASCII format.
There are only three encoding attributes of FSO. The system defaults are Unicode and ASCII, and there is no utf-8 we want. Therefore, files generated by using FSO components on Chinese systems are generally in the gb2312 encoding format.
Solution one
GB2312 encoding contains Russian letters. If the page is set to Simplified Chinese GB2312, then Russian can be displayed normally. However, since Chinese defaults to "Song Ti" display, Russian will be displayed as full-width characters in "Song Ti", which is ugly, so foreign fonts must be used. , such as "New Rome" or "Arial" etc. Chinese customers can communicate there, but when real Russians browse the web, they need to download and install Chinese, which is not ideal and may lead to giving up browsing the web.
Solution 2
Because ASP is an old language, some of its features have very poor support for UTF-8. FSO does not support directly generating files in UTF-8 format because it cannot specify the required file format. For example, if you want to generate a file in UTF-8 format, you cannot use the commonly used Scripting.FileSystemObject object. We must change our thinking and use Adodb.Stream. This is how some current blog programs are implemented, such as zblog.
The function of Scripting.FileSystemObject object to create files is as follows:
FileSystemObject.CreateTextFile(filename[,overwrite[,unicode]])
The unicode attribute is described like this:
Optional. Boolean value indicating whether to create the file in Unicode or ASCII file format. This value is True if the file is created in Unicode file format; False if the file is created in ASCII file format. If this section is omitted, an ASCII file is assumed to be created.
Unfortunately, you cannot use this function to create UTF-8 format files.
Use the ADODB.Stream object, see below for usage:
Copy the code code as follows:
Set objStream = Server.CreateObject("ADODB.Stream")
With objStream
.Open
.Charset = "utf-8"
.Position = objStream.Size
.WriteText=str
.SaveToFile server.mappath("/sitemap.xml"),2
.Close
End With
Set objStream = Nothing
Appendix: Introduction to ASCII, Unicode, and UTF-8
ASCII is a character set, including uppercase and lowercase English letters, numbers, control characters, etc. It is represented by one byte and ranges from 0-127.
Since the characters represented by ASCII are very limited, each country or region has proposed its own character set based on this. For example, GB2312, which is widely used in China, provides encoding for Chinese characters and is represented by two bytes.
These character sets are incompatible with each other, and the same numbers may represent different characters, causing trouble for information exchange.
Unicode is a character set that maps all characters in the world to a unique number (code point), such as the number 0x0041 corresponding to the letter a. Unicode is still in development, and it contains more and more characters.
When storing characters represented by Unicode, a certain encoding method is required, such as UCS-2, which uses two bytes to represent Unicode-encoded characters. UTF-8 is another encoding method of the Unicode character set. It is of variable length, up to 6 bytes, and characters less than 127 are represented by one byte. It has the same result as the ASCII character set, so it has very good Compatibility, English text in ASCII encoding can be processed as UTF-8 encoding without modification, and is widely used.