Beispiel #1
0
        private static void TranslateSingleFile(string fileName, string fileSaveName, bool includeBlankResources)
        {
            // Open the input file.
            ResXResourceReader reader = new ResXResourceReader(fileName);

            try
            {
                // Get the enumerator.  If this throws an ArguementException
                // it means the file is not a .RESX file.
                IDictionaryEnumerator enumerator = reader.GetEnumerator();
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("WARNING: could not parse " + fileName);
                Console.WriteLine("         " + ex.Message);
                return;
            }

            // Allocate the list for this instance.
            SortedList textResourcesList = new SortedList();

            // Run through the file looking for only true text related
            // properties and only those with values set.
            foreach (DictionaryEntry dic in reader)
            {
                // Only consider this entry if the value is something.
                if (null != dic.Value)
                {
                    // Is this a System.String.
                    if ("System.String" == dic.Value.GetType().ToString())
                    {
                        String KeyString = dic.Key.ToString();

                        // Make sure the key name does not start with the
                        // "$" or ">>" meta characters and is not an empty
                        // string (or we're explicitly including empty strings).
                        if ((false == KeyString.StartsWith(">>")) &&
                            (false == KeyString.StartsWith("$")) &&
                            (includeBlankResources || "" != dic.Value.ToString()))
                        {
                            // We've got a winner.
                            textResourcesList.Add(dic.Key, dic.Value);
                        }

                        // Special case the Windows Form "$this.Text" or
                        // I don't get the form titles.
                        if (0 == String.Compare(KeyString, "$this.Text"))
                        {
                            textResourcesList.Add(dic.Key, dic.Value);
                        }
                    }
                }
            }

            // It's entirely possible that there are no text strings in the
            // .ResX file.
            if (textResourcesList.Count > 0)
            {
                if (null != fileSaveName)
                {
                    if (File.Exists(fileSaveName))
                    {
                        File.Delete(fileSaveName);
                    }

                    // Create the new file.
                    ResXResourceWriter writer =
                        new ResXResourceWriter(fileSaveName);

                    foreach (DictionaryEntry textdic in textResourcesList)
                    {
                        writer.AddResource(textdic.Key.ToString(), Psuedoizer.ConvertToFakeInternationalized(textdic.Value.ToString()));
                    }

                    writer.Generate();
                    writer.Close();
                    Console.WriteLine(fileName + ": converted " + textResourcesList.Count + " text resource(s).");
                }
            }
            else
            {
                Console.WriteLine("WARNING: No text resources found in " + fileName);
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Psuedoizer: Adapted from MSDN BugSlayer 2004-Apr i18n Article.");
            if (args.Length < 2)
            {
                Console.WriteLine("Purpose: Takes an English resource file (resx) and creates an artificial");
                Console.WriteLine("         but still readable Euro-like language to exercise your i18n code");
                Console.WriteLine("         without a formal translation.");
                Console.WriteLine(String.Empty);
                Console.WriteLine("Psuedoizer.exe infile outfile");
                Console.WriteLine("    Example:");
                Console.WriteLine("    Psuedoizer.exe strings.en.resx strings.ja-JP.resx");
                System.Environment.Exit(1);
            }

            string fileName     = args[0];
            string fileSaveName = args[1];

            // Open the input file.
            ResXResourceReader reader = new ResXResourceReader(fileName);

            try
            {
                // Get the enumerator.  If this throws an ArguementException
                // it means the file is not a .RESX file.
                IDictionaryEnumerator enumerator = reader.GetEnumerator( );

                // Allocate the list for this instance.
                SortedList textResourcesList = new SortedList( );

                // Run through the file looking for only true text related
                // properties and only those with values set.
                foreach (DictionaryEntry dic in reader)
                {
                    // Only consider this entry if the value is something.
                    if (null != dic.Value)
                    {
                        // Is this a System.String.
                        if ("System.String" == dic.Value.GetType().ToString())
                        {
                            String KeyString = dic.Key.ToString( );

                            // Make sure the key name does not start with the
                            // "$" or ">>" meta characters and is not an empty
                            // string.
                            if ((false == KeyString.StartsWith(">>")) &&
                                (false == KeyString.StartsWith("$")) &&
                                ("" != dic.Value.ToString( )))
                            {
                                // We've got a winner.
                                textResourcesList.Add(dic.Key, dic.Value);
                            }

                            // Special case the Windows Form "$this.Text" or
                            // I don't get the form titles.
                            if (0 == String.Compare(KeyString, "$this.Text"))
                            {
                                textResourcesList.Add(dic.Key, dic.Value);
                            }
                        }
                    }
                }

                // It's entirely possible that there are no text strings in the
                // .ResX file.
                if (textResourcesList.Count > 0)
                {
                    if (null != fileSaveName)
                    {
                        // Create the new file.
                        ResXResourceWriter writer =
                            new ResXResourceWriter(fileSaveName);

                        foreach (DictionaryEntry textdic in textResourcesList)
                        {
                            writer.AddResource(textdic.Key.ToString(), Psuedoizer.ConvertToFakeInternationalized(textdic.Value.ToString()));
                        }

                        writer.Generate( );
                        writer.Close( );
                        Console.WriteLine("Converted " + textResourcesList.Count + " text resource(s).");
                    }
                }
                else
                {
                    Console.Write("WARNING: No text resources found in " + fileName);
                    System.Environment.Exit(2);
                }
            }
            catch (Exception e)
            {
                Console.Write(e.ToString());
                System.Environment.Exit(1);
            }
        }