This article is reproduced: P
Certificate -------------------------------------------------------------------------------------------------------------------------------------------
<Preface>In the past few days, the new version of www. egametimes. com is still in preparation. The problem solved over the past two days is multi-language support. I did not expect a big trouble.
<Chapter 1 temple computing>In fact, the multi-language support already has some patterns. For example, ASP. NET basically uses resource files. However, sometimes I seem to be not very regular. When I saw the XML reading of JavaScript, I imagined a structure that supports multiple languages using JavaScript + XML.
1) use JavaScript to read XML files in various languages; 2) Replace the content of tags marked on the page and load the corresponding language content.
The simple steps, simple ideas, and discussion are closed. By the way, the reason for doing so is to reduce the server load, and once the download is complete, the local computing speed is very fast-What's more, is the XML file not big?
<Chapter 2 victoryAt noon yesterday, I was very excited because I completed the technical implementation of multi-language support! To remove the others, let's talk about the core points of the implementation of this multi-language:
- Use JavaScript to read XML.
In fact, this is relatively simple. The important code is here:VaR xmldoc;
Xmldoc = new activexobject ("msxml2.domdocument ");
Xmldoc. async = false;
Xmldoc. Load (xmlfile );
Note: HereThe declaration of the first line and the assignment of the second line must be written separately.! This is because I encountered such a problem during debugging-although there is no problem with the declaration and assignment on msdn, even though there was no problem during my previous test. This question is still bothering me. I haven't found any answers yet. Can someone tell me?
- The node defined in the XML file is like this:
<Tagname ZH-Cn = "Chinese" En-US = "English" otherlangcode = "what other languages">
To obtain the text of a specific language, you can use:
Document. All (tagname). innerhtml
= Xmldoc.doc umentelement. selectsinglenode (tagname). getattribute (langcode );
What do you need to understand here?
En-CN and CN-US are called language code, which can be searched directly in msdn ).
Go to msdn to check the usage of document. All. In fact, this is not the case in my code, because this tagname should be automatically obtained using a method, right? Let's take a look at the usage of document. all. In HTML, each tag can have two attributes: ID and name, which can be different.
For documentelement, selectsinglenode, and getattribute, check the domdocument item on msdn.
In fact, the core things are just like this. Is it very easy? However, you may encounter unexpected troubles during actual operations. Well, ELE. Me. After reading the book, we said that the multi-language support was "complete. I tried it during debugging. It seems quite good that both scripts and XML files are loaded locally and then processed. This frees the server from running locally, inter-language switching is quite smooth-you don't even need to refresh the page. At that time, it was a sense of accomplishment! In addition, the multi-language support of a website refers to the multi-language interface of a website. As for the content, you can see the website's own. Just like Bloglines, the interface can be in N languages, but if you want to blog, he can't give you any more languages, right? Therefore, I will only discuss the multi-language support for the interface. Well, I understand that people skip this section and ignore it.
<Chapter 3 rush down>Successful! After a lunch in the United States, I continued to modify the details in the afternoon. But the problem arises at this time: When I access my website from another machine, an error message is displayed! The content supported by multiple languages cannot be loaded! After checking it carefully, make sure that the problem lies in the xmldoc. Load () sentence-remote XML files cannot be loaded. You should say, "it's not necessary to run it on your machine ". Google spent half an afternoon and hardly found any instructions. This is the only way to do this? The hard work of more than one day is in vain? The situation is so sudden.
<Chapter 4 new hopes>Now, I think it is really awkward. I didn't refer to anything else. I directly considered that "Since remote XML files cannot be loaded, I can find a way to load them ". Looking at the FSO of JavaScript, it seems that this web page does not work. Then I searched all over the sky and found this reading a file. As a result, this readurl class is used to obtain filecontent, and XML content is loaded using the xmldoc. loadxml () method. The result turned out to be! If there are no such shortcomings ..... first, this is to read files through the applet, the user needs to have JRE support; second, the XML file is UTF-8 encoding storage, the readurl class is to read the file in plain text, after ...... aren't you really planning to switch between yourself? Third, it's not stable. When the time is not working, I have also completed the version...-_-B. Looking at this strange thing, I think of a strange word: DIY. It seems that this is my long term. I can combine the things I will use and find to accomplish some strange things ......
<The person who calls the last chapter is required to unbind the bell.>After two failed versions, we finally need to calm down and think about it-so let's start from scratch. At first, why did we think of the multi-language support for JavaScript + XML? In fact, when I analyzed the tree chart of the msdn website, I found several code snippets from various corners, including an HTC file, several CSS files, and several XML files. Of course, the most useful one is to encapsulate the HTC file of the script. The code for reading the XML file was also seen here. Then, I searched for some related articles on the Internet. Combined with msdn, the experiment started successfully. At this bottleneck, I remembered that the script also loaded a remote XML file in the HTC file, so I went back and looked at the HTC file carefully. Then the problem is solved. The key lies in this object:
VaR xmldoc;
Xmldoc = new activexobject ("msxml2.domdocument"); the HTC file uses the following objects:
VaR xmldoc;
Xmldoc = new activexobject ("Microsoft. xmldom "); this msxml2.domdocument and Microsoft. xmldom objects are almost identical in usage, but why is there such a difference in scope restriction? I haven't found a file that can be described in detail yet. Do you have any suggestions?<CONCLUSION>At this point, the multi-language support of this website has been solved. Because they are lazy people and have perfect requirements for details, they will not post more code-because it is difficult to organize it, haha. Basically, according to the information provided in these articles, I should have mentioned all my general ideas. If you have any questions, please wait for a while before the new version of www. egametimes. com is complete! = D
[Updating 2006-04-13]
The new version of egametimes decided to cancel this multi-language implementation method, so this study is here to be a toy.