dictionary - Classic ASP - Website localization -
i need add languages support existing classic asp website
the "best" solution found encapsulate every text in function, create database table store, every page, translations , use dictionary object retrieve right value.
example:
<div>welcome xy website</div> <button class="btn green">login</button>
becomes
<div><%=tl("welcome xy website")%></div> <button class="btn" ><%=tl("login")%></button>
then tl function should this
function tl(strinput) dim strtargetlanguage, strpageurl,objdict,strtmp1,strtmp2 if strinput<>"" ' first check if customer has set language.. else uses browser language if request.cookies("culture")="" strtargetlanguage=lcase(left(request.servervariables("http_accept_language"),2)) else strtargetlanguage=lcase(left(request.cookies("culture"),2)) end if ' if user's language not supported.... if instr(stracceptedlanguages,strtargetlanguage)= 0 strtargetlanguage="en" end if strpageurl=request.servervariables("url") set objdict=server.createobject("scripting.dictionary") objdict.add "strpageurl",strpageurl 'stored procedure load translation in required language , target page cmd.commandtext="spdictionaryread" cmd.commandtype=4 cmd.parameters("@languageid")=strtargetlanguage cmd.parameters("@pageurl")=strpageurl set rst=cmd.execute() if not rst.eof while not rst.eof objdict.add rst("txt"),rst(strtargetlanguage) rst.movenext() wend end if rst.close if objdict.exists(strinput)=true tl=objdict.item(strinput) else ' custom function translate using google tl=translate(strinput,"en",strtargetlanguage) tl=replace(tl,"'","''") strinput=replace(strinput,"'","''") 'add new sentence 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
the function not ready since @ present every time called access db , load translations of page , create dictionary: in situation better avoid dictionary , directly query db sentence required.
i need "only" find wise way store dictionary "somewhere" avoid rebuild it
choose? application, session, objvariable page, ???
googling little realize application not wise solution many reasons,
session: try keep session slim: never save object 30-50 keys if can avoid.... unless remove @ end of page (if worth)?
someone suggest load translations application "plain array" , build dictionary every time required, while loading sentences dictionary can test if current sentence target sentence , extract translation without using dictionary.. therefore neither wise solution
i read
lookup component microsoft
but couldn't find docs
perhaps can use .net components, hashtable?
since imagine translations common issue, expect there has better solution, , approach wrong:
can pls suggest better approach or hints?
a while ago inherited project developer , date in classic asp haven't found more efficient way of handling localisation.
the basic premise is
store key-value pairs in database, structure used
keys
table containing keys , "section" (which denotes particular grouping of keys). have ourvalues
table contains language specific localisations associated key 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) ║ ╚══════════════╩═════════════════╩═════════════════╩══════════════════╝
build asp application create xml key-value pairs in database. application 2 pages
iterates through supported languages , sections (defined in
keys
table) in nested loop, how decide on logical separation of cache files. within each iteration pass work page viawinhttprequest
object. page returns xml structure has built looking in database , pulling out key-value pairs related specific section being iterated.as mentioned page built called
winhttprequest
object after querying database specific section key-value pairs returns them in bespoke xml structure.
store file structure like
\packs\ ┐ ├ \de\ ├ \es\ ├ \fr\ ... etc
that includes sub directory each language supported in database. directories need accessible web application identity (whether that's
iusr
or pre-configured account) @ leastmodify
permission allow creation , modification of cached xml files.the bespoke xml files 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 little time!\nplease patient.\n]]> </error> </error_messages> <info> <langcode>gb</langcode> <langname>english</langname> <strings>194</strings> <time>0</time> </info> </language_pack>
xml file truncated keep things simple, actual files contain lot more values
the main localisation powered #include
file get's added each page needs support localisation (it becomes part of build process when working in localised web applications).
the #include
call locale.asp
similar describe in question. made of various functions of main ones include;
init_langpack(langcode, admin, section)
- handles initialisation of xml cache file.langcode
string representation of language want load (collates directory names,de
,es
etc).admin
determines whether in "admin mode" or not , behaves differently if are, setting page headers etc. ,section
xml file (which separate sections) want load xmldom object. should called before attempting accessml_string()
loads xml.ml_string(id, showcontrol)
- used asp pages want output localisationid
key xml / keys table (why both?, explain in bit), whileshowcontrol
boolean used decide when page displayed in "admin mode" can show localisation in editable field or not. might not want due how localisations placed in page (allowing handle them differently, display panel underneath etc).
Comments
Post a Comment