经典ASP-网站本地化 [英] Classic ASP - Website localization

查看:107
本文介绍了经典ASP-网站本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to add Languages support to an existing classic asp website

我发现的最佳"解决方案是将每个文本封装在一个函数中,创建一个数据库表,在其中为每个页面存储翻译,并使用字典对象检索正确的值.

The "best" solution I found is to encapsulate every text in a function, create a database table where store, for every page, the translations and use a dictionary object to retrieve the right value.

示例:

<div>Welcome to xy website</div>
<button class="btn green">Login</button>

成为

<div><%=TL("Welcome to xy website")%></div>
<button class="btn" ><%=TL("Login")%></button>

然后TL函数应该是这样的

then TL function should be like this

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

该函数尚未准备就绪,因为当前每次调用该函数都将访问数据库并加载页面的所有翻译并创建Dictionary:在这种情况下,最好避免使用字典,而直接向数据库查询所需的句子.

The function is not ready since at present every time it is called it access the DB and load all the translations of the page and create the Dictionary: in this situation would be better to avoid the Dictionary and directly Query the DB for the sentence required.

我需要唯一"来找到一种明智的方式来存储字典某处",以避免重建它
但是该选择哪一个呢?应用程序,会话,objVariable进入页面,???

I need "ONLY" to find a wise way to store the dictionary "somewhere" so to avoid to rebuild it
But which to choose? Application, Session, objVariable into the page, ???

仔细研究一下,我意识到应用程序不是一个明智的解决方案,原因有很多,

googling a little I realize that Application is not a wise solution for many reasons,

会话:我试图使会话保持非常细长:如果可以避免的话,我永远都不会保存带有30-50个键的对象....除非我在页面末尾将其删除(如果值得)?

Session: I try to keep session very slim: I would never save an object with some 30-50 Keys if I can avoid.... unless I remove it at the end of the page itself (if it worth)?

有人建议将翻译作为普通数组"加载到Application中,然后在每次需要时构建Dictionary,但是在将句子加载到Dictionary中时,我可以测试当前句子是否是目标句子并在不使用Dictionary的情况下提取翻译..因此这都不是一个明智的解决方案

Someone suggest to load translations into Application as "plain array" and then build Dictionary every time it is required, but while loading the sentences into the Dictionary I can test if current sentence is target sentence and extract the translation without using Dictionary.. therefore neither this is a wise solution

我也阅读了

Microsoft的查找组件

Lookup Component from Microsoft

但找不到任何文档

也许可以使用某些.NET组件,例如HashTable?

perhaps can use some .NET components, like HashTable?

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

Since I imagine that translations are a common issue, I expect there has to be a better solution, and that my approach is wrong:

请问可以提出更好的方法或提示吗?

Can pls suggest a better approach or some hints?

推荐答案

前一段时间,我从另一个开发人员那里继承了一个项目,到目前为止,在Classic ASP中,我还没有找到一种更有效的本地化方法.

A while ago I inherited a project from another developer and to date in Classic ASP I haven't found a more efficient way of handling localisation.

基本前提是

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

  1. Store key-value pairs in a database, the structure we used is a keys table containing the keys and a "section" (which denotes a particular grouping of keys). We then have our values table which contains language specific localisations associated to the key by a FK (1:M) relationship.

╔══════════════════════════════════════════════════════════════════╗
║     Keys Table                                                   ║
╠══════════════╦══════════════╦═════════════════╦══════════════════╣
║ id (int, PK) ║ key (string) ║ section (string)║  editpanel (bit) ║
╚══════════════╩══════════════╩═════════════════╩══════════════════╝

╔═════════════════════════════════════════════════════════════════════╗
║     Values Table                                                    ║
╠══════════════╦═════════════════╦═════════════════╦══════════════════╣
║ id (int, PK) ║ key_id (int, FK)║ lang (string)   ║  value (string)  ║
╚══════════════╩═════════════════╩═════════════════╩══════════════════╝

  • 构建一个ASP应用程序,以从数据库中的键值对创建XML.该应用程序基本上是两页

  • Build a ASP application to create the XML from the key-value pairs in the database. The application is basically two pages

    1. 在嵌套循环中遍历受支持的语言和(在 keys 表中定义)部分,这是我们决定缓存文件逻辑分离的方式.在每次迭代中,我们通过 WinHttpRequest 对象将工作传递到另一个页面.该页面返回一个XML结构,该结构是通过在数据库中查找并拉出与要迭代的特定部分相关的所有键值对而构建的.

    1. Iterates through the supported languages and sections (defined in the keys table) in a nested loop, this is how we decide on logical separation of cache files. Within each iteration we pass the work to a another page via a WinHttpRequest object. The page returns an XML structure that is has built by looking in the database and pulling out all the key-value pairs that are related to the specific section being iterated.

    正如已经提到的,专门为 WinHttpRequest 对象调用而创建的页面,在查询数据库中的特定节键值对之后,该页面将以定制的XML结构返回它们./p>

  • As already a mentioned a page specifically built to be called by the WinHttpRequest object that after querying the database for the specific section key-value pairs returns them in a bespoke XML structure.

  • 存储类似的文件结构

  • Store a file structure something like

    \packs\ ┐
            ├ \de\
            ├ \es\
            ├ \fr\
            ... etc
    

    包括数据库中支持的每种语言的子目录.必须通过Web应用程序身份(无论是 IUSR 还是预先配置的帐户)来访问目录,并且至少具有 Modify 权限以允许创建和修改缓存的XML文件.

    that includes a sub directory for each language supported in the database. The directories need to be accessible by Web Application Identity (whether that's a IUSR or a pre-configured account) with at least Modify permission to allow the creation and modification of the cached XML files.

    定制的XML文件如下所示

    The bespoke XML files look something like this

    <?xml version="1.0" encoding="utf-8" ?>
    <language_pack>
      <strings>
        <string id="391" key="connect" editpanel="0" section="email">
          <![CDATA[Connect]]>
        </string>
        <string id="9" key="uploadimage" editpanel="0" section="common">
          <![CDATA[Upload Photo]]>
        </string>
        <string id="12" key="notes" editpanel="0" section="common">
          <![CDATA[Notes]]>
        </string>
      </strings>
      <error_messages>
        <error id="1" key="pleasebepatient">
          <![CDATA[\nThis action may take a little time!\nPlease be patient.\n]]>
        </error>
      </error_messages>
      <info>
        <langcode>gb</langcode>
        <langname>English</langname>
        <strings>194</strings>
        <time>0</time>
      </info>
    </language_pack>
    

    XML文件被截断以保持简单,实际文件包含更多值

    然后主要的本地化由 #include 文件提供动力,该文件已添加到需要支持本地化的每个页面中(在本地化的Web应用程序中工作时,它只是构建过程的一部分).

    The main localisation is then powered by an #include file that get's added to each page that needs to support localisation (it just becomes part of the build process when working in localised web applications).

    我们称为 locale.asp #include 所做的事情与您在问题中描述的类似.它由各种功能组成,其中主要包括;

    The #include we call locale.asp does something similar to what you describe in the question. It made up of various functions of which the main ones include;

    • init_langpack(langcode,admin,section)-处理XML缓存文件的所有初始化. langcode 只是您要加载的语言的字符串表示形式(整理到目录名称, de es 等). admin 确定我们是否处于管理员模式",如果行为,则稍有不同,设置页面标题等.并且 section 是XML文件(其中我们分成几部分),我们希望将其加载到XMLDOM对象中.在尝试访问 ml_string()时应始终调用它,因为它加载XML.

    • init_langpack(langcode, admin, section) - Handles any initialisation of the XML cache file. The langcode is just the string representation of the language you want to load (collates to the directory names, de, es etc). admin determines whether we are in "Admin Mode" or not and behaves slightly differently if we are, setting page headers etc. and section is the XML file (which we separate into sections) we want to load into an XMLDOM object. It should always be called before attempting to access ml_string() as it loads the XML.

    ml_string(id,showcontrol)-使用了我们要输出本地化的ASP页面, id 是XML/Keys表中的键(为什么两者都将解释一下) ,而 showcontrol 布尔值用于确定页面何时以管理员模式"显示,因此我们可以在是否可以编辑字段.

    ml_string(id, showcontrol) - Used the ASP pages where we want to output a localisation the id is a key from the XML / Keys table (why both?, will explain in a bit), while the showcontrol Boolean is used to decide when the page is displayed in "Admin Mode" so we can show the localisation in an editable field or not. You might not always want to do this due to how localisations are placed in the page (allowing you to handle them differently, display a panel underneath etc).

    这篇关于经典ASP-网站本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆