Link to home
Start Free TrialLog in
Avatar of olka
olkaFlag for Germany

asked on

Import a text file into resource file

Hi,
i want to create an application which is language independent. I've created a resource file, with all phrases/caption texts ... of my forms in it.
But now i come to the next form, which is a reengineering of an existing one. I have already a text file where ~300 lines of textes are stored. Now i want to import this file to my existing resource file.
is there a way to do this, without switching between nodepad and resource file editor for doing this silly copy/paste??

hope, someone has a good answer...
olka
Avatar of xSinbad
xSinbad

I think you mean something like this

Open "Filename" For Input As #1
  strInput=input(lof(1),1)
Close #1
End Sub


Open "Filename" For Output As #1
  Write #1, "data here" & strInput
Close #1
Avatar of olka

ASKER

when it is possible to write the contents of the input file to the resource file (in this case the Output) then it would be ok. But i don't think that this is possible, because the VisualStudio resource file seems to be no text file.
olka
ASKER CERTIFIED SOLUTION
Avatar of Matti
Matti
Flag of Finland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of inthedark
I translate at runtime.


The neat thing about this method is that you can distribute your app untranslated and allow a user to translate.  You can alow your form to detect CTRL/ALT/T and pops up a list of labels and allows use to save the translated labels.

I keep the files in formname_languagecode.txt.

I basically use the following functions, bewarned xSinbad's solution may fail in a multi-user enviroment, you ned to open files "Shared".

Here is how to use translation class.

' in globals
Dim TransForm As New clsTranslate

' in app setup
TransForm.Language = "FR"
TransForm.ResourceFolder = App.Path + "\"
TransForm.TranslateMessage = TransForm.GetFromMaster(TransForm.TranslateMessage)
TransForm.TranslateTitle = TransForm.GetFromMaster(TransForm.TranslateTitle)

' in form load

TransForm.TranslateForm Me

Dim dateformat

' any where else
dateformat = TransForm.GetFromMaster("DD/MM/YY")


Here is the class:
Option Explicit

' Language Translation Class by Nick Young
' Sept 19th 2000
' please report bugs to: nyoung@vipintersoft.com
' use at will.

Public Language As String   '   e.g. FR,DE
Public TranslateMessage As String ' Message to prompt operator
Public TranslateTitle As String   ' title for translation module
Public ResourceFolder As String   ' place where translations are stored

Dim MasterData As String

Public Terminator As String
Public Sub TranslateForm(frm As Form)

Dim LangDataFile As String
Dim LD As String ' Language data
Dim SaveLD As String
Dim ok

LangDataFile = ResourceFolder + frm.Name + "_" + Language + ".txt"

LD = ReadFile(LangDataFile)
If LD = "ERROR" Then
    LD = ""
End If
SaveLD = LD
Dim ctrl As Control
For Each ctrl In frm.Controls
   If TypeOf ctrl Is VB.Label Then
      ctrl.Caption = Translate(LD, ctrl.Caption)
   ElseIf TypeOf ctrl Is VB.CommandButton Then
      ctrl.Caption = Translate(LD, ctrl.Caption)
   End If
Next

' has data changed
If LD <> SaveLD Then
   ok = WriteFileOK(LangDataFile, LD)
End If

End Sub
Public Function GetFromMaster(KeyName As String) As String

Dim LangDataFile As String
Dim LD As String ' Language data
Dim SaveLD As String
Dim ok
If Len(MasterData) = 0 Then
    LangDataFile = ResourceFolder + "MASTER" + "_" + Language + ".txt"
    MasterData = ReadFile(LangDataFile)
    If MasterData = "ERROR" Then
        MasterData = ""
    End If
End If
SaveLD = MasterData

GetFromMaster = Translate(LD, KeyName)

' has data changed
If MasterData <> SaveLD Then
   ok = WriteFileOK(LangDataFile, MasterData)
End If

End Function


Function Translate(LD As String, CaptionText As String) As String

Dim NewText As String

If Len(CaptionText) = 0 Then
    Exit Function
End If
NewText = ReturnParameter(LD, CaptionText)
If Len(NewText) = 0 Then
    NewText = InputBox(TranslateMessage, TranslateTitle, CaptionText)
    If Len(NewText) > 0 Then
        LD = LD + CaptionText + "=" + NewText + vbCrLf
    Else
        NewText = CaptionText
    End If
End If
Translate = NewText

End Function

Public Function ReadFile(FileName As String) As String

Dim wlfn As Long

wlfn = FreeFile
On Error Resume Next

If Len(Dir(FileName)) > 0 Then
    If Err.Number Then
        ReadFile = "Invalid resource file path: " + FileName
        Exit Function
    End If
   
    Open FileName For Input Shared As #wlfn
    ReadFile = Input(LOF(wlfn), wlfn)
    Close wlfn
Else
    ReadFile = "ERROR"
End If

End Function

Public Function WriteFileOK(FileName As String, DataString) As Boolean

Dim wlfn As Long

wlfn = FreeFile
On Error Resume Next
Err.Clear
WriteFileOK = False
Open FileName For Output As #wlfn
If Err.Number <> 0 Then
    Exit Function
End If
Print #wlfn, DataString;
Close wlfn
If Err.Number <> 0 Then
    Exit Function
End If
WriteFileOK = True

End Function

Function ReturnParameter(VariableList As String, VariableName As String, Optional DefaultValue As String = "", Optional FieldDelimiter As String = "") As String

' Returns the value of a variable from withing as list of stuff
' example:

' VList="Server=Fred;Title=Cute Routine"
' MyTitle=ReturnParameter(Vlist,"Title","Untitled")

Dim VarPos As Long
Dim sFieldDelimiter As String

If Len(FieldDelimiter) = 0 Then
    sFieldDelimiter = Terminator
Else
    sFieldDelimiter = FieldDelimiter
End If

VarPos = InStr(1, VariableList, VariableName, vbTextCompare)
If VarPos = 0 Then
    ReturnParameter = ""
Else
    ReturnParameter = Trim(LeftPart(RightPart(Mid(VariableList, _
        VarPos), "="), sFieldDelimiter))
End If
   
End Function

Public Function RightPart(WholeString, FindString) As String

Dim pos As Long

pos = InStr(1, WholeString, FindString, vbTextCompare)
If pos = 0 Then
    RightPart = ""
ElseIf pos = Len(WholeString) Then
    RightPart = ""
Else
    RightPart = Mid$(WholeString, pos + Len(FindString))
End If

End Function

Public Function LeftPart(WholeString, FindString) As String

Dim pos As Long

pos = InStr(1, WholeString, FindString, vbTextCompare)
If pos = 0 Then
    LeftPart = WholeString
ElseIf pos = 1 Then
    LeftPart = ""
Else
    LeftPart = Left$(WholeString, pos - 1)
End If

End Function


Private Sub Class_Initialize()
Terminator = ";"
TranslateMessage = "You need to translate the following: "
TranslateTitle = "Translation"
End Sub





Using the Translation class you don't need to give the operator an option to change if you don't want to.

Your app can run as is.

Also any where in your code you can translate e.g.

M$ = "Printer Error"
msgbox TransForm.GetFromMaster(M$)
p.s. On my PC it only takes .010 seconds to translate an average form at runtime.
Avatar of olka

ASKER

the link to the Excel2Resource Project was very good.
inthedark, i'm sorry, but this tool looks much more easier then your code, even i can now administrate my NLS-Textes in Excel.
If you already have done the resource file route then it's too late for you but using the translate class means that you can delivery your app. in any language without any effor on your part.