Needs Improvement

Colin Coller's Blog
Posts: 62 | Comments: 156 | TrackBacks: ?

February 1, 2005

Mock-translating resx files

If you're developing an international application, one of the easiest ways you can test it for localizability is to mock-translate its resource files to a foreign alphabet or writing system and then go through it looking for English text. Here's some simple code based on one of our tools that mock-translates a resx file to hangul:

using System;

using System.Collections;

using System.Globalization;

using System.Resources;

using System.Text;

 

public class MockTranslation

{

 

    private static Random random = new Random();

 

    public static void MockTranslate( string inputFilename , string outputFilename )

    {

 

        ResXResourceReader reader;

        ResXResourceWriter writer;

        string key;

        string oldValue;

        StringBuilder newValue;

 

        using ( reader = new ResXResourceReader( inputFilename ) )

        {

            using ( writer = new ResXResourceWriter( outputFilename ) )

            {

                foreach ( DictionaryEntry entry in reader )

                {

                    key = (string)entry.Key;

                    oldValue = (string)entry.Value;

                    newValue = new StringBuilder( oldValue.Length );

                    newValue.Append( "[[[" );

                    foreach ( char oldCharacter in oldValue )

                    {

                        if ( Char.IsLetter( oldCharacter ) )

                        {

                            newValue.Append( (char)( 0xAC00 + random.Next( 0xD7A3 - 0xAC00 ) ) );

                        }

                        else

                        {

                            newValue.Append( oldCharacter );

                        }

                    }

                    newValue.Append( "]]]" );

                    writer.AddResource( key , newValue.ToString() );

                }

            }

        }

 

    }

 

}

We add square brackets to the values to help find embedded strings and string concatenation. We only convert letters so we don't break format strings. A more complicated implementation would support multiple alphabets or writing systems, support combined writing systems (hangul+hanja, hiragana+katakana+kanji, etc), change word length to simulate expansion or contraction of words in different languages, perform machine translation, etc. For many applications, though, this should be sufficient.

Colin

04:43 PM | Colin