Solved

Import a text file into resource file

Posted on 2002-05-06
8
401 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:olka
8 Comments
 
LVL 6

Expert Comment

by:xSinbad
Comment Utility
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
0
 

Author Comment

by:olka
Comment Utility
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
0
 
LVL 14

Accepted Solution

by:
Matti earned 150 total points
Comment Utility
Hi!

The res file which is directly supported by VB is an cmplier procuct from .RC and .H files

Seems that you have the VisualStudio then in VC++ you can save easy in this .res format.

If you look in your VB setupkit folder there is a RC and .H and RES files for that Setup1 VBproject.

See the SETUP1.RC file is the one targeted for translations and Setup1.res the output there are editors to support directly res file like the VB adini, but the own text editor project is much more simple to make if you do write these both files at same time.

In RC file it has the string
resCALCSPACE "An error occurred while calculating required disk space."

In .H file it has the ID num:
#define resCALCSPACE 400

And common in both is:
resCALCSPACE


If you can make both of these files like this The VC++ will load it and make the res file. Also the commandline RES compiler will do the same.

I think it's easier to copy +300 strings once.
But the "Open "Filename" For Input As #1" and
simple string calc  Name & Str(Num) will solve it

Don't have this editor made, but seen these codes, maybe someone else has a link?

May be this is very close one, Excel to Resource:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=8556&lngWId=1

Matti
0
 
LVL 17

Expert Comment

by:inthedark
Comment Utility
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





0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 17

Expert Comment

by:inthedark
Comment Utility
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$)
0
 
LVL 17

Expert Comment

by:inthedark
Comment Utility
p.s. On my PC it only takes .010 seconds to translate an average form at runtime.
0
 

Author Comment

by:olka
Comment Utility
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.
0
 
LVL 17

Expert Comment

by:inthedark
Comment Utility
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.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now