<% ' Translation class with a database as Backend ' ============================================ ' ' Copyright: GPL of corse ' Author: Markus Graf ' ' Database Layout for the table translation: ' ------------------------------------------ ' word varchar(255) ' language varchar(5) ' translation varchar(255) ' ' Database Layout for the table untranslated: ' ----------------------------------------- ' word varchar(255) ' language varchar(5) ' ' Using the untraslated table ' --------------------------- ' All untranslatable words will stored automaticaly in the table untranslated. ' Addding and renaming of a keyword deletes it from the table. ' ' Code Example: ' ------------- ' ' Create the object ' Set Tr = new Translation ' ' Set the language to use ' Tr.Language = "de" ' ' Set the database connection string ' Tr.Connection = "Provider=SQLOLEDB; Server=localhost;Database=MyDatabase;UID=sa;PWD=sa;MyDatabase" ' ' ' Add a new translation ' Tr.Add "Microsoft", "Winzigweich" ' Tr.Add "Redmond", "hell" ' ' Translate it ' Response.Write Tr.Translate("Microsoft") ' ' Translate a whole text. Translation keywords are encapsulated in [ and ] ' Response.Write Tr.MultiTranslate("Hi, My name is Bill Gates and i'm the CEO of the company [Microsoft]. Our headquarters are in [Redmond]") ' ' Rename the translation ' Tr.Add "Microsoft", "Ein grosser Konzern" ' ' Rename the word ' Tr.Add "Microsoft", "devilssoftwarecompany" ' ' And last but not least, delete the word ' Tr.Del "devilssoftwarecompany" Class Translation ' Language to use Dim Language ' Declare the database connection Dim Connection ' Translate a word Function Translate(Word) ' Get the translation from the database set RS_Trans = Server.CreateObject("ADODB.Recordset") RS_Trans.ActiveConnection = Connection RS_Trans.Source = "SELECT translation FROM translation WHERE word='" & Word & "' AND language='" & Language & "'" RS_Trans.open() IF RS_Trans.EOF THEN ' Add the keyword to the untranslated table AddUntranslated(Word) ' Return the input word with a ? if not exist Translate = Word & "*" ELSE ' Retorn otherwise the right word Translate = RS_Trans("translation") END IF RS_Trans.close() set RS_Trans = nothing END Function ' Add a word Function Add(Word, Translation) ' Check for existing word. Don't insert if a translation already exist. IF Translate(Word) = Word & "*" THEN DelUntranslated(Word) set RS_Trans = Server.CreateObject("ADODB.Recordset") RS_Trans.ActiveConnection = Connection RS_Trans.Source = "INSERT INTO translation (word,language,translation) VALUES ('" & Word & "','" & Language & "','" & Translation & "')" RS_Trans.open() END IF END Function ' Delete a word Function Del(Word) set RS_Trans = Server.CreateObject("ADODB.Recordset") RS_Trans.ActiveConnection = Connection RS_Trans.Source = "Delete translation WHERE word='" & Word & "' AND language='" & Language & "'" RS_Trans.open() END Function ' Rename a Translation Function Rename(Word, Translation) set RS_Trans = Server.CreateObject("ADODB.Recordset") RS_Trans.ActiveConnection = Connection RS_Trans.Source = "UPDATE translation SET translation='" & Translation & "' WHERE word='" & Word & "' AND language='" & Language & "'" RS_Trans.open() END Function ' Rename a word Function WRename(Word, NewWord) ' Check for existing word. Don't rename if a translation already exist. IF Translate(Word) = Word & "*" THEN DelUntranslated(Word) set RS_Trans = Server.CreateObject("ADODB.Recordset") RS_Trans.ActiveConnection = Connection RS_Trans.Source = "UPDATE translation SET word='" & NewWord & "' WHERE word='" & Word & "' AND language='" & Language & "'" RS_Trans.open() END IF END Function ' Translate a whole text. Keywords are encapsulated in [ and ] Function MultiTranslate(Text) Dim regEx, Match, Matches ' Create variable. Set regEx = New RegExp ' Create a regular expression. regEx.Pattern = "\[([^\]]+)\]" ' Set pattern. regEx.Global = True ' Set global applicability. Set Matches = regEx.Execute(Text) ' Execute search. For Each Match in Matches ' Iterate Matches collection. Match = Replace(Match,"[","") Match = Replace(Match,"]","") Text = replace(Text,"[" & Match & "]",Translate(Match)) Next MultiTranslate = Text END function ' Check if a keyword is still in the untranslated table Private Function IsUntnranslated(Word) set RS_Trans = Server.CreateObject("ADODB.Recordset") RS_Trans.ActiveConnection = Connection RS_Trans.Source = "SELECT * FROM untranslated WHERE word='" & Word & "' AND language='" & Language & "'" RS_Trans.open() IF RS_Trans.EOF THEN ' Return the input word with a ? if not exist IsUntnranslated = true ELSE ' Retorn otherwise the right word IsUntnranslated = false END IF RS_Trans.close() set RS_Trans = nothing END Function ' Add a word Private Function AddUntranslated(Word) ' Check for existing word. Don't insert if the word already exist. IF IsUntnranslated(Word) = true THEN set RS_Trans = Server.CreateObject("ADODB.Recordset") RS_Trans.ActiveConnection = Connection RS_Trans.Source = "INSERT INTO untranslated (word,language) VALUES ('" & Word & "','" & Language & "')" RS_Trans.open() END IF END Function ' Delete an untranslated word Private Function DelUntranslated(Word) set RS_Trans = Server.CreateObject("ADODB.Recordset") RS_Trans.ActiveConnection = Connection RS_Trans.Source = "Delete untranslated WHERE word='" & Word & "' AND language='" & Language & "'" RS_Trans.open() END Function END Class %>