Menu

#1 Make translation loading simpler and less repetitive

open
None
5
2009-01-16
2009-01-16
wj32
No

Instead of manually setting the text for controls, OSE should dynamically set the Text property of controls. For example, I could have a translation file with the following text:

buttonDoSomething: Do Something
labelSomethingElse: Something Else

The code would do something like this (in C#):

foreach (var line in File.ReadAllText(fileName))
{
string[] splitLine = line.Split(new char[] { ':' }, 2);
string controlName = splitLine[0];
string controlText = splitLine[1];
FieldInfo controlField = this.GetType().GetField(controlName);
PropertyInfo textProperty = controlField.GetType().GetProperty("Text");

textProperty.SetValue(controlField.GetValue(this), controlText, null);
}

Discussion

  • Lefteris Aslanoglou

    I'm trying to implement the method you're saying like this:

    langFileReader = My.Computer.FileSystem.OpenTextFileReader(My.Computer.Registry.GetValue(AppRegKey, "LanguageFile", Nothing))

    While Not langFileReader.EndOfStream
    Dim line As String = langFileReader.ReadLine
    If Not (line = "" Or Microsoft.VisualBasic.Left(line, 1) = ("!"c)) Then
    Dim splitLine As String() = line.Split(New Char() {","c}, 2)
    Dim controlName As String = splitLine(0)
    Dim controlText As String = LTrim(splitLine(1))
    Dim controlField As FieldInfo = Me.GetType.GetField(controlName)
    Dim textProperty As PropertyInfo = controlField.GetType.GetProperty("Text")

    textProperty.SetValue(controlField.GetValue(Me), controlText, Nothing)
    End If

    End While

    However, the controlFiels is returned as nothing even though the control name is correct in the Language file I'm tesing. The line is as follows:

    SubtitleToolStripMenuItem, Subtitle

     
  • Lefteris Aslanoglou

    • assigned_to: nobody --> leftos
     
  • wj32

    wj32 - 2009-01-16

    Replace GetField(controlName) with GetField(controlName, BindingFlags.NonPublic)

     
  • Lefteris Aslanoglou

    I've tried it quite a lot of ways, trying other examples (of not the same use though) from the Internet, but I can't get anything to return to controlField. It's always Nothing, no matter what way I've tried. If you have the time to play with it on your own and find a way that works, I'd be grateful.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.