Question

I'm building a multilingual website in ASP.

I have all the translations on DB.

Now the question is, how should I set the translations from DB on the page (I select only the needed translations for each page):

should I use :

sql = "select key,value from translations where lang='he' and page='index.asp'"
set rs = myconn.execute(mysql)
do until rs.eof
    eval(""&key&" = "&value&"") '// lang_hello = "Hello World"
rs.movenext
loop

and use it (for example):

<% =lang_hello %>

OR

choose the Dictionary Object?

What's the best practice for that case?

(Some pages have around 10 words to save, some 60)

Thanks!

Était-ce utile?

La solution

No don't read all your translations at once. Make a function:

private function ReadTranslations(byval lang, byval groupname)

    Dim d: Set d=Server.CreateObject("Scripting.Dictionary")
    dim mysql: mysql="SELECT * FROM translations WHERE lang = '" & lang & "' 
       AND groupname = '" & groupname & "'"
    set rs = myconn.execute(mysql)
    do until rs.eof
        d.Add groupname & "." & rs.key, rs.value
        rs.movenext
    loop
    rs.close
    ReadTranslations = d ' maybe you need to use set ReadTranslations = d not sure...
end function

Put this function in a asp file and include that file on every page. So replace the page by group (which is I think a more clearer concept than a page for grouping translations).

Now you can type:

 dim translations: translation = ReadTranslations("EN", "Orders")
 response.write translation.Item("...")
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top