none
C# - How to seprate this "bulky" class RRS feed

  • Question

  • I got a class called Translate() and it works perfectly fine.  The problems are

    1. As more languages are to be added, this class becomes very "bulky", and this makes it very hard to maintain.

    2. The "base language" is English.  Each language must have a base language which is actually duplicated.  How can it possible make a single base language (i.e. English) across all different translated languages?

    Here below is the coding:


        public class Translate
        {
            public enum Language
            {
                French,
                German,
                Spanish,
                Italian
            }

            public static string GetTranslate(string text, Language language)
            {          
                switch (language)
                {                  
                    case Language.French:
                        text = text.Replace("Black", "Noir");
                        text = text.Replace("Blue", "Bleu");
                        text = text.Replace("Brown", "Marron");
                        text = text.Replace("Green", "Vert");
                        text = text.Replace("Gold", "Or");
                        text = text.Replace("Pink", "Rose");
                        text = text.Replace("Red", "Rouge");
                        text = text.Replace("Silver", "Argent");
                        text = text.Replace("Yellow", "Jaune");
                        text = text.Replace("White", "Blanc");
                        break;

                    case Language.German:                   
                        text = text.Replace("Black", "Schwarz");
                        text = text.Replace("Blue", "Blau");
                        text = text.Replace("Brown", "Braun");
                        text = text.Replace("Green", "Grün");
                        text = text.Replace("Gold", "Gold");
                        text = text.Replace("Light", "Licht");
                        text = text.Replace("Pink", "Rosa");
                        text = text.Replace("Red", "rot");
                        text = text.Replace("Silver", "Silber");
                        text = text.Replace("Yellow", "Gelb");
                        text = text.Replace("White", "Weiß");
                        text = text.Replace("Assorted", "mischen");                  
                        break;
                       
                    case Language.Spanish:             
                        text = text.Replace("Black", "Negra");
                        text = text.Replace("Blue", "Azul");
                        text = text.Replace("Brown", "marrón");
                        text = text.Replace("Green", "Verde");
                        text = text.Replace("Gold", "Oro");
                        text = text.Replace("Pink", "Rosada");
                        text = text.Replace("Red", "Roja");
                        text = text.Replace("Silver", "Plata");
                        text = text.Replace("Yellow", "Amarilla");
                        text = text.Replace("White", "Blanca");
                        text = text.Replace("Assorted", "surtido");
                        break;

                    case Language.Italian:                   
                        text = text.Replace("Black", "Nero");
                        text = text.Replace("Blue", "Blu");
                        text = text.Replace("Brown", "Marrone");
                        text = text.Replace("Green", "verde");
                        text = text.Replace("Gold", "Oro");
                        text = text.Replace("Pink", "Rosa");
                        text = text.Replace("Red", "Rossa");
                        text = text.Replace("Silver", "Argento");
                        text = text.Replace("Yellow", "Gialla");
                        text = text.Replace("White", "Bianca");
                        text = text.Replace("Assorted", "assortito");
                        break;
                }
                return text;
            }
        }

    Thank you for your time, if anyone knows how to correct this, please let me know, thanks

    Saturday, March 28, 2020 11:54 AM

Answers

  • Here is one idea

    public class Translate
    {
        public enum Language
        {
            French,
            German,
            Spanish,
            Italian
        }
        public static string GetTranslate(string text, Language language)
        {
            switch (language)
            {
                case Language.French:
                    return French.GetText(text);
                case Language.German:
                    return German.GetText(text);
                case Language.Spanish:
                    return Spanish.GetText(text);
                case Language.Italian:
                    return Italian.GetText(text);
                default:
                    return "";
            }
        }
    }

    Then one class per language e.g.

    public class French
    {
        public static string GetText(string text)
        {
            text = text.Replace("Black", "Noir");
            text = text.Replace("Blue", "Bleu");
            text = text.Replace("Brown", "Marron");
            text = text.Replace("Green", "Vert");
            text = text.Replace("Gold", "Or");
            text = text.Replace("Pink", "Rose");
            text = text.Replace("Red", "Rouge");
            text = text.Replace("Silver", "Argent");
            text = text.Replace("Yellow", "Jaune");
            text = text.Replace("White", "Blanc");
    
            return text;
        }
    }

    Does that work for you?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, March 28, 2020 12:25 PM
    Moderator
  • Hi iHandler,

    Here is my idea :)

    public class Languages
        {
            public List<string> English { get; set; }
            public List<string> French { get; set; }
            public List<string> German { get; set; }
            public List<string> Spanish { get; set; }
            public List<string> Italian { get; set; }
    
    
    
            public Languages()
            {
    
                English = new List<string>()
                {
                   "Black","Blue","Brown","Green","Gold","Pink","Red","Silver","Yellow","White"
    
                };
    
                French = new List<string>()
                {
                "Noir", "Bleu", "Marron", "Vert", "Or", "Rose", "Rouge", "Argent", "Jaune", "Blanc"
    
                 };
    
    
                German = new List<string>()
                {
    
                 "Schwarz", "Blau", "Braun", "Grün", "Gold", "Rosa", "Rot", "Silber", "Gelb", "Weiß"
    
                };
    
    
                Spanish = new List<string>()
                {
    
                 "Negro", "Azul", "Marrón", "Verde", "Dorado", "Rosa", "Rojo", "Plata", "Amarillo", "Blanco"
    
                };
    
    
                Italian = new List<string>()
                 {
    
                "Silver", "Nero", "Blu", "Brown", "Verde", "Gold", "Rosa", "Rosso", "Giallo", "bianco"
    
                 };
    
    
            }
    
    
        }

     public class Translate
        {
            public enum Language
            {
                French,
                German,
                Spanish,
                Italian
            }
    
            public  string GetTranslate(string text, Language language)
            {
                string varresult="";
    
    
                Languages varlanguages = new Languages();
    
    
                switch (language)
                {
                    case Language.French:
    
                        for (int i = 0; i < varlanguages.English.Count(); i++)
                        {
                            if (varlanguages.English[i].Contains(text))
                            {
                                varresult = varlanguages.French[i];
                            }
                           
                        }
    
                     break;
    
                    case Language.German:
    
    
                        for (int i = 0; i < varlanguages.English.Count(); i++)
                        {
                            if (varlanguages.English[i].Contains(text))
                            {
                                varresult = varlanguages.German[i];
                            }
    
                        }
                        break;
    
                    case Language.Spanish:
    
                        for (int i = 0; i < varlanguages.English.Count(); i++)
                        {
                            if (varlanguages.English[i].Contains(text))
                            {
                                varresult = varlanguages.Spanish[i];
                            }
    
                        }
                        break;
                    case Language.Italian:
    
                        for (int i = 0; i < varlanguages.English.Count(); i++)
                        {
                            if (varlanguages.English[i].Contains(text))
                            {
                                varresult = varlanguages.Italian[i];
                            }
    
                        }
                        break;
                    default:
                        return "";
                      
                }
    
                return varresult;
            }
        }

     static void Main(string[] args)
            {
                 
                Translate TranslateObj = new Translate();
    
                 
                Console.WriteLine(TranslateObj.GetTranslate("Red", Language.French));
                Console.WriteLine(TranslateObj.GetTranslate("Red", Language.German));
                Console.WriteLine(TranslateObj.GetTranslate("Red", Language.Spanish));
                Console.WriteLine(TranslateObj.GetTranslate("Red", Language.Italian));
    
    
                Console.ReadKey();
    
            }

    The result 


    Please remember to mark the replies as answers if they helped you :) ~

    • Marked as answer by iHandler Sunday, March 29, 2020 2:19 PM
    Saturday, March 28, 2020 2:02 PM
  • Had some time to kill so here is another example done in VS2017, compatible with VS2019. 

    https://1drv.ms/u/s!AtGAgKKpqdWjkH5hTjFgRpX-2swz?e=112B8x

    using System;
    using System.Windows.Forms;
    using TranslateDemo.Classes;
    
    namespace TranslateDemo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void TranslationButton_Click(object sender, EventArgs e)
            {
    
                var text = "The Black hat is on this head with a Gold rim";
                var result = GetTranslation.Text(Language.French, text);
                Console.WriteLine(result);
    
                text = "The Black hat is on this head with a Silver rim";
                result = GetTranslation.Text(Language.German, text);
                Console.WriteLine(result);
    
    
                text = "The Black hat is on this head with a Silver rim";
                result = GetTranslation.Text(Language.Spanish, text);
                Console.WriteLine(result);
    
                text = "The Black hat is on this head with a Silver rim";
                result = GetTranslation.Text(Language.Italian, text);
                Console.WriteLine(result);
    
            }
        }
    }
    

    1.

    using TranslateDemo.LanguageClasses;
    
    namespace TranslateDemo.Classes
    {
        public class Translator
        {
            public string French(string text) => 
                FrenchLanguage.GetText(text);
            public string German(string text) => 
                GermanLanguage.GetText(text);
            public string Spanish(string text) => 
                SpanishLanguage.GetText(text);
            public string Italian(string text) => 
                ItalianLanguage.GetText(text);
        }
    }

    2.

    namespace TranslateDemo.Classes
    {
        public class GetTranslation
        {
            public static string Text(Language selectedLanguage, string text)
            {
                var translator = new Translator();
                return (string)typeof(Translator)
                    .GetMethod(selectedLanguage.ToString())
                    .Invoke(translator, parameters: new object[] { text });
            }
        }
    }

    3.

    namespace TranslateDemo.Classes
    {
        public enum Language
        {
            French,
            German,
            Spanish,
            Italian
        }
    }


    French

    namespace TranslateDemo.LanguageClasses
    {
        public class FrenchLanguage
        {
            public static string GetText(string text) => text
                .Replace("Black", "Noir")
                .Replace("Blue", "Bleu")
                .Replace("Brown", "Marron")
                .Replace("Green", "Vert")
                .Replace("Gold", "Or")
                .Replace("Pink", "Rose")
                .Replace("Red", "Rouge")
                .Replace("Silver", "Argent")
                .Replace("Yellow", "Jaune")
                .Replace("White", "Blanc");
        }
    }

    German

    namespace TranslateDemo.LanguageClasses
    {
        public class GermanLanguage
        {
            public static string GetText(string text) => text
                .Replace("Black", "Schwarz")
                .Replace("Blue", "Blau")
                .Replace("Brown", "Braun")
                .Replace("Green", "Grün")
                .Replace("Gold", "Gold")
                .Replace("Light", "Licht")
                .Replace("Pink", "Rosa")
                .Replace("Red", "rot")
                .Replace("Silver", "Silber")
                .Replace("Yellow", "Gelb")
                .Replace("White", "Weiß")
                .Replace("Assorted", "mischen");
        }
    }

    Italian

    namespace TranslateDemo.LanguageClasses
    {
        public class ItalianLanguage
        {
            public static string GetText(string text) => text
                .Replace("Black", "Nero")
                .Replace("Blue", "Blu")
                .Replace("Brown", "Marrone")
                .Replace("Green", "verde")
                .Replace("Gold", "Oro")
                .Replace("Pink", "Rosa")
                .Replace("Red", "Rossa")
                .Replace("Silver", "Argento")
                .Replace("Yellow", "Gialla")
                .Replace("White", "Blanca")
                .Replace("Assorted", "assortito");
        }
    }

    Spanish

    namespace TranslateDemo.LanguageClasses
    {
        public class SpanishLanguage
        {
            public static string GetText(string text) => text
                .Replace("Black", "Negra")
                .Replace("Blue", "Azul")
                .Replace("Brown", "marrón")
                .Replace("Green", "Verde")
                .Replace("Gold", "Oro")
                .Replace("Pink", "Rosada")
                .Replace("Red", "Roja")
                .Replace("Silver", "Plata")
                .Replace("Yellow", "Amarilla")
                .Replace("White", "Blanca")
                .Replace("Assorted", "surtido");
        }
    }

    Now the above could be more streamline by placing the text in a well formed file e.g. comma delimited or json thus no hard coded translations, they would be dynamic, that is your choice.

    Note how things are organized.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by iHandler Sunday, March 29, 2020 2:19 PM
    Saturday, March 28, 2020 3:24 PM
    Moderator

All replies

  • Here is one idea

    public class Translate
    {
        public enum Language
        {
            French,
            German,
            Spanish,
            Italian
        }
        public static string GetTranslate(string text, Language language)
        {
            switch (language)
            {
                case Language.French:
                    return French.GetText(text);
                case Language.German:
                    return German.GetText(text);
                case Language.Spanish:
                    return Spanish.GetText(text);
                case Language.Italian:
                    return Italian.GetText(text);
                default:
                    return "";
            }
        }
    }

    Then one class per language e.g.

    public class French
    {
        public static string GetText(string text)
        {
            text = text.Replace("Black", "Noir");
            text = text.Replace("Blue", "Bleu");
            text = text.Replace("Brown", "Marron");
            text = text.Replace("Green", "Vert");
            text = text.Replace("Gold", "Or");
            text = text.Replace("Pink", "Rose");
            text = text.Replace("Red", "Rouge");
            text = text.Replace("Silver", "Argent");
            text = text.Replace("Yellow", "Jaune");
            text = text.Replace("White", "Blanc");
    
            return text;
        }
    }

    Does that work for you?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, March 28, 2020 12:25 PM
    Moderator
  • Hi iHandler,

    Here is my idea :)

    public class Languages
        {
            public List<string> English { get; set; }
            public List<string> French { get; set; }
            public List<string> German { get; set; }
            public List<string> Spanish { get; set; }
            public List<string> Italian { get; set; }
    
    
    
            public Languages()
            {
    
                English = new List<string>()
                {
                   "Black","Blue","Brown","Green","Gold","Pink","Red","Silver","Yellow","White"
    
                };
    
                French = new List<string>()
                {
                "Noir", "Bleu", "Marron", "Vert", "Or", "Rose", "Rouge", "Argent", "Jaune", "Blanc"
    
                 };
    
    
                German = new List<string>()
                {
    
                 "Schwarz", "Blau", "Braun", "Grün", "Gold", "Rosa", "Rot", "Silber", "Gelb", "Weiß"
    
                };
    
    
                Spanish = new List<string>()
                {
    
                 "Negro", "Azul", "Marrón", "Verde", "Dorado", "Rosa", "Rojo", "Plata", "Amarillo", "Blanco"
    
                };
    
    
                Italian = new List<string>()
                 {
    
                "Silver", "Nero", "Blu", "Brown", "Verde", "Gold", "Rosa", "Rosso", "Giallo", "bianco"
    
                 };
    
    
            }
    
    
        }

     public class Translate
        {
            public enum Language
            {
                French,
                German,
                Spanish,
                Italian
            }
    
            public  string GetTranslate(string text, Language language)
            {
                string varresult="";
    
    
                Languages varlanguages = new Languages();
    
    
                switch (language)
                {
                    case Language.French:
    
                        for (int i = 0; i < varlanguages.English.Count(); i++)
                        {
                            if (varlanguages.English[i].Contains(text))
                            {
                                varresult = varlanguages.French[i];
                            }
                           
                        }
    
                     break;
    
                    case Language.German:
    
    
                        for (int i = 0; i < varlanguages.English.Count(); i++)
                        {
                            if (varlanguages.English[i].Contains(text))
                            {
                                varresult = varlanguages.German[i];
                            }
    
                        }
                        break;
    
                    case Language.Spanish:
    
                        for (int i = 0; i < varlanguages.English.Count(); i++)
                        {
                            if (varlanguages.English[i].Contains(text))
                            {
                                varresult = varlanguages.Spanish[i];
                            }
    
                        }
                        break;
                    case Language.Italian:
    
                        for (int i = 0; i < varlanguages.English.Count(); i++)
                        {
                            if (varlanguages.English[i].Contains(text))
                            {
                                varresult = varlanguages.Italian[i];
                            }
    
                        }
                        break;
                    default:
                        return "";
                      
                }
    
                return varresult;
            }
        }

     static void Main(string[] args)
            {
                 
                Translate TranslateObj = new Translate();
    
                 
                Console.WriteLine(TranslateObj.GetTranslate("Red", Language.French));
                Console.WriteLine(TranslateObj.GetTranslate("Red", Language.German));
                Console.WriteLine(TranslateObj.GetTranslate("Red", Language.Spanish));
                Console.WriteLine(TranslateObj.GetTranslate("Red", Language.Italian));
    
    
                Console.ReadKey();
    
            }

    The result 


    Please remember to mark the replies as answers if they helped you :) ~

    • Marked as answer by iHandler Sunday, March 29, 2020 2:19 PM
    Saturday, March 28, 2020 2:02 PM
  • Had some time to kill so here is another example done in VS2017, compatible with VS2019. 

    https://1drv.ms/u/s!AtGAgKKpqdWjkH5hTjFgRpX-2swz?e=112B8x

    using System;
    using System.Windows.Forms;
    using TranslateDemo.Classes;
    
    namespace TranslateDemo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void TranslationButton_Click(object sender, EventArgs e)
            {
    
                var text = "The Black hat is on this head with a Gold rim";
                var result = GetTranslation.Text(Language.French, text);
                Console.WriteLine(result);
    
                text = "The Black hat is on this head with a Silver rim";
                result = GetTranslation.Text(Language.German, text);
                Console.WriteLine(result);
    
    
                text = "The Black hat is on this head with a Silver rim";
                result = GetTranslation.Text(Language.Spanish, text);
                Console.WriteLine(result);
    
                text = "The Black hat is on this head with a Silver rim";
                result = GetTranslation.Text(Language.Italian, text);
                Console.WriteLine(result);
    
            }
        }
    }
    

    1.

    using TranslateDemo.LanguageClasses;
    
    namespace TranslateDemo.Classes
    {
        public class Translator
        {
            public string French(string text) => 
                FrenchLanguage.GetText(text);
            public string German(string text) => 
                GermanLanguage.GetText(text);
            public string Spanish(string text) => 
                SpanishLanguage.GetText(text);
            public string Italian(string text) => 
                ItalianLanguage.GetText(text);
        }
    }

    2.

    namespace TranslateDemo.Classes
    {
        public class GetTranslation
        {
            public static string Text(Language selectedLanguage, string text)
            {
                var translator = new Translator();
                return (string)typeof(Translator)
                    .GetMethod(selectedLanguage.ToString())
                    .Invoke(translator, parameters: new object[] { text });
            }
        }
    }

    3.

    namespace TranslateDemo.Classes
    {
        public enum Language
        {
            French,
            German,
            Spanish,
            Italian
        }
    }


    French

    namespace TranslateDemo.LanguageClasses
    {
        public class FrenchLanguage
        {
            public static string GetText(string text) => text
                .Replace("Black", "Noir")
                .Replace("Blue", "Bleu")
                .Replace("Brown", "Marron")
                .Replace("Green", "Vert")
                .Replace("Gold", "Or")
                .Replace("Pink", "Rose")
                .Replace("Red", "Rouge")
                .Replace("Silver", "Argent")
                .Replace("Yellow", "Jaune")
                .Replace("White", "Blanc");
        }
    }

    German

    namespace TranslateDemo.LanguageClasses
    {
        public class GermanLanguage
        {
            public static string GetText(string text) => text
                .Replace("Black", "Schwarz")
                .Replace("Blue", "Blau")
                .Replace("Brown", "Braun")
                .Replace("Green", "Grün")
                .Replace("Gold", "Gold")
                .Replace("Light", "Licht")
                .Replace("Pink", "Rosa")
                .Replace("Red", "rot")
                .Replace("Silver", "Silber")
                .Replace("Yellow", "Gelb")
                .Replace("White", "Weiß")
                .Replace("Assorted", "mischen");
        }
    }

    Italian

    namespace TranslateDemo.LanguageClasses
    {
        public class ItalianLanguage
        {
            public static string GetText(string text) => text
                .Replace("Black", "Nero")
                .Replace("Blue", "Blu")
                .Replace("Brown", "Marrone")
                .Replace("Green", "verde")
                .Replace("Gold", "Oro")
                .Replace("Pink", "Rosa")
                .Replace("Red", "Rossa")
                .Replace("Silver", "Argento")
                .Replace("Yellow", "Gialla")
                .Replace("White", "Blanca")
                .Replace("Assorted", "assortito");
        }
    }

    Spanish

    namespace TranslateDemo.LanguageClasses
    {
        public class SpanishLanguage
        {
            public static string GetText(string text) => text
                .Replace("Black", "Negra")
                .Replace("Blue", "Azul")
                .Replace("Brown", "marrón")
                .Replace("Green", "Verde")
                .Replace("Gold", "Oro")
                .Replace("Pink", "Rosada")
                .Replace("Red", "Roja")
                .Replace("Silver", "Plata")
                .Replace("Yellow", "Amarilla")
                .Replace("White", "Blanca")
                .Replace("Assorted", "surtido");
        }
    }

    Now the above could be more streamline by placing the text in a well formed file e.g. comma delimited or json thus no hard coded translations, they would be dynamic, that is your choice.

    Note how things are organized.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by iHandler Sunday, March 29, 2020 2:19 PM
    Saturday, March 28, 2020 3:24 PM
    Moderator
  • Thanks all !

    All are well designed, just a matter of choice !

    Thanks again :-)

    Sunday, March 29, 2020 2:21 PM