Dictionary 经典ASP-网站本地化

Dictionary 经典ASP-网站本地化,dictionary,vbscript,asp-classic,Dictionary,Vbscript,Asp Classic,我需要添加语言支持现有的经典asp网站 我找到的“最佳”解决方案是将每个文本封装在一个函数中, 创建一个数据库表,其中存储每个页面的翻译,并使用dictionary对象检索正确的值 例如: 欢迎访问xy网站 登录 变成 那么TL函数应该是这样的 Function TL(strInput) Dim strTargetLanguage, strPageURL,objDict,strTmp1,strTmp2 if strInput<>"" then '

我需要添加语言支持现有的经典asp网站

我找到的“最佳”解决方案是将每个文本封装在一个函数中, 创建一个数据库表,其中存储每个页面的翻译,并使用dictionary对象检索正确的值

例如:

欢迎访问xy网站
登录
变成


那么TL函数应该是这样的

Function TL(strInput)
    Dim strTargetLanguage, strPageURL,objDict,strTmp1,strTmp2
    if strInput<>"" then
        ' First check if customer has set language.. else uses browser language
        if request.cookies("culture")="" then 
            strTargetLanguage=lcase(left(request.servervariables("HTTP_ACCEPT_LANGUAGE"),2))
        else
            strTargetLanguage=lcase(left(request.cookies("culture"),2))
        end if
        ' if User's Language is not supported....
        if instr(strAcceptedLanguages,strTargetLanguage)= 0 then        
            strTargetlanguage="en"
        end if

        strPageURL=Request.ServerVariables("URL")

        Set objDict=Server.CreateObject("Scripting.Dictionary")
        objDict.Add "strPageUrl",strPageUrl

        'Stored Procedure to load translation in the required language and for the target Page      
        cmd.CommandText="spDictionaryRead"
        cmd.CommandType=4
        cmd.Parameters("@LanguageID")=strTargetLanguage
        cmd.Parameters("@PageUrl")=strPageURL
        set rst=cmd.Execute()

        if not rst.eof then
            while not rst.eof
                objDict.Add rst("txt"),rst(strTargetLanguage) 
                rst.movenext()
            wend
        end if
        rst.close

        if objDict.Exists(strInput)=true then
            TL=objDict.Item(strInput)           
        else        
            ' Custom Function to translate using google
            TL=Translate(strInput,"en",strTargetLanguage)

            TL=Replace(TL,"'","''")
            strInput=replace(strInput,"'","''")
            'Add new Sentence to Dictionary
            cmd.CommandText="spDictionaryWrite"
            cmd.CommandType=4
            cmd.Parameters("@PageUrl")=strPageURL
            cmd.Parameters("@TXT")=strInput
            cmd.Parameters("@TargetLanguage")= strTargetLanguage
            cmd.Parameters("@TargetText")=TL
            cmd.Execute()

            set objDict=nothing
        end if          
    else
        TL=""
    end if
End Function
函数TL(strInput)
Dim strTargetLanguage、strPageURL、objDict、strTmp1、strTmp2
如果输入“”则
'首先检查客户是否设置了语言。。else使用浏览器语言
如果request.cookies(“文化”)=“”则
strTargetLanguage=lcase(左(request.servervariables(“HTTP\u ACCEPT\u LANGUAGE”),2))
其他的
strTargetLanguage=lcase(左(request.cookies(“文化”),2))
如果结束
'如果不支持用户的语言。。。。
如果instr(strAcceptedLanguages,strTargetLanguage)=0,则
strTargetlanguage=“en”
如果结束
strPageURL=Request.ServerVariables(“URL”)
设置objDict=Server.CreateObject(“Scripting.Dictionary”)
添加“strPageUrl”,strPageUrl
'存储过程以所需语言加载目标页的翻译
cmd.CommandText=“spDictionaryRead”
cmd.CommandType=4
cmd.Parameters(“@LanguageID”)=strTargetLanguage
cmd.Parameters(“@PageUrl”)=strPageURL
set rst=cmd.Execute()
如果不是rst.eof,则
而不是rst.eof
添加rst(“txt”),rst(strTargetLanguage)
rst.movenext()
温德
如果结束
rst.close
如果objDict.Exists(strInput)=true,则
TL=对象信息项(strInput)
其他的
'使用google进行翻译的自定义函数
TL=翻译(strInput,“en”,标准语言)
TL=替换(TL,“,”)
strInput=替换(strInput,“,”)
'在字典中添加新句子
cmd.CommandText=“spDictionaryWrite”
cmd.CommandType=4
cmd.Parameters(“@PageUrl”)=strPageURL
cmd.Parameters(“@TXT”)=strInput
cmd.Parameters(“@TargetLanguage”)=strTargetLanguage
cmd.Parameters(“@TargetText”)=TL
cmd.Execute()
设置objDict=nothing
如果结束
其他的
TL=“”
如果结束
端函数
该函数尚未准备就绪,因为目前每次调用该函数时,它都会访问数据库并加载页面的所有翻译并创建字典: 在这种情况下,最好避免使用字典,直接向DB查询所需的句子

我需要“仅”找到一种明智的方法将字典存储在“某处”,以避免重建它
但选择哪一个呢?应用程序、会话、objVariable进入页面

通过谷歌搜索,我意识到应用程序并不是一个明智的解决方案,原因有很多

会话:我尽量保持会话非常苗条:如果可以避免,我将永远不会保存具有30-50个键的对象。。。。除非我在页面末尾删除它(如果值得的话)

有人建议将翻译作为“普通数组”加载到应用程序中,然后在每次需要时构建字典,但在将句子加载到字典中时,我可以测试当前句子是否为目标句子,并在不使用字典的情况下提取翻译。。 因此,这两者都不是明智的解决方案

我还读到了

来自Microsoft的查找组件

但是找不到任何文档

也许可以使用一些.NET组件,比如HashTable

因为我认为翻译是一个常见的问题,所以我希望有更好的解决方案,我的方法是错误的:


请推荐一个更好的方法或一些提示?

我使用
应用程序
在经典ASP中缓存一些对象,通常作为一个数组,包含从数据库中通过
GetRows()检索到的值。

会话
不适合,因为它只对一个用户可用,而不是像
应用程序
那样的所有用户

对于您可能需要缓存大量数据的情况,我有一个建议

从数据库检索值时,可以使用包含VBScript代码的文件系统对象创建ASP脚本,以创建字典并填充值。然后,您可以将生成的ASP页面包含在所有文件中

例如,要生成缓存文件

<%
datestamp = Year(Now()) & Month(Now()) & Day(Now()) & Hour(Now()) & Minute(Now()) & Second(Now())

set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(Server.MapPath("\cache\language_" & datestamp))
tfile.WriteLine("<%")
tfile.WriteLine("Set objDict=Server.CreateObject(""Scripting.Dictionary"")")

'...your database code here....
        while not rst.eof
            tfile.WriteLine("objDict.Add " & rst("txt") & ",rst(strTargetLanguage)")
            rst.movenext()
        wend
'...etc etc...

tfile.WriteLine("%>")
tfile.close
set tfile=nothing
set fs=nothing

Application("languagecache") = datestamp
%>

这只是一个例子。您还应该添加代码,以确保如果在第一次构建缓存文件之前访问了页面,则该页面将从始终存在的include文件中获取内容。您还可以添加一些代码来检查上次生成缓存文件的时间,并在设置的时间之后生成新的缓存文件。您可以这样做,因为这是一项计划任务,这样一些较差的用户就不必在构建缓存文件时等待(或者异步启动)。

不久前,我从另一个开发人员那里继承了一个项目,到目前为止,在经典ASP中,我还没有找到一种更有效的方法来处理本地化

基本前提是

  • 在数据库中存储键值对,我们使用的结构是一个
    keys
    表,其中包含键和一个“部分”(表示一组特定的键)。然后,我们有了
    表,该表包含通过FK(1:M)关系与键关联的特定于语言的本地化

    ╔══════════════════════════════════════════════════════════════════╗
    ║     键表║
    ╠══════════════╦══════════════╦═
    
    Server.Execute("\cache\language_" & Application("languagecache"))