In the past, I wrote a small experimental web project when I was working at a foreign software company. Part of the requirement was to support multiple languages (Simplified Chinese, Traditional Chinese, English, Korean, Japanese) for the user interface. At that time, template technology was used to implement it. This requirement is met and the user's settings are saved using cookies. However, the user still needs to select the language when logging in for the first time (or after deleting the cookies). So can it be more convenient for users? Can a program be used to automatically help with selection?
The answer is yes, everyone is using Google. If you use the Chinese system to open the Google homepage, the Chinese homepage will naturally open, not other languages. Because Google will automatically determine what the preferred language is for the user's system.
How can we do it like Google? It’s actually very simple.
The HTTP Headers Information sent by the browser to the web server contains such information as Accept-Language. This information is the language under Tools->Internet Options->General in the browser. It is used to set browsing of language preferences acceptable to the server, which can be a prioritized list of multiple acceptable languages.
Let me take ASP as an example to briefly introduce "how to automatically determine the user language":
<%
Function RegExpTest(patrn, strng)
Dim regEx,Match,Matches' creates variables.
Set regEx = New RegExp' Create a regular expression.
regEx.Pattern = patrn' sets the pattern.
regEx.IgnoreCase = True' sets whether characters are case-sensitive.
regEx.Global = True'Sets global availability.
Set Matches = regEx.Execute(strng)'Execute search.
For Each Match in Matches' iterates through the match collection.
RetStr = RetStr&Match.Value
Next
RegExpTest = RetStr
End Function
AL = Request.ServerVariables("HTTP_ACCEPT_LANGUAGE")' Get the acceptable language parameters of the user's browser
LG = RegExpTest("^[az-]+",AL)' Get the user browser’s acceptable language preferences
select case LG
case "zh-cn"
response.write "中文(中国)"
case "zh-tw"
response.write "Chinese (Taiwan)"
case "zh-mo"
response.write "Chinese (Macau SAR)"
case "zh-hk"
response.write "Chinese (Hong Kong SAR)"
case "zh-sg"
response.write "Chinese (Singapore)"
case "en-us"
response.write "English (United States)"
case "ja"
response.write "Japanese"
case "ko"
response.write "Korean"
end select
%>