-1

(I didn't really know how to properly title this post)

Do you guys have a better alternative to this code? I feel like I could accomplish more with a dictionary combined with my JsonSerialized class.

WillyTC config = new WillyTC();

switch (GetCurrentLanguage().ToLower())
{
    case "french":
        PerimetreExterne = GetSolidworksProp(config.PerimetreExterne_Fr);
        PerimetreInterne = GetSolidworksProp(config.PerimetreInterne_Fr);
        break;

    case "english":
        PerimetreExterne = GetSolidworksProp(config.PerimetreExterne_En);
        PerimetreInterne = GetSolidworksProp(config.PerimetreInterne_En);
        break;

    default:
        throw new Exception("Non-Handled");
}

My Json serializable class - here's the definition of the class used for JSONserialization:

    public class WillyTC 
    {
        public WillyProperties WillyCustomProperties;

        public WillyTC()
        {
            WillyCustomProperties = new WillyProperties();
        }

        public class WillyProperties
        {
            public readonly string PerimetreExterne_Fr = "Longueur à découper extérieure";
            public readonly string PerimetreExterne_En = "Cutting Length-Outer";
            public readonly string PerimetreInterne_Fr = "Longueur à découper des boucles intérieures";
            public readonly string PerimetreInterne_En = "Cutting Length-Inner";
            public readonly string NbDecoupeInterne_Fr = "Découpes";
            public readonly string NbDecoupeInterne_En = "Cut Outs";
            public readonly string AireBrut_Fr = "Surface du flanc de tôle";
            public readonly string AireBrut_En = "Bounding Box Area";
            public readonly string AirePiece_Fr = "Surface du flanc de tôle brut";
            public readonly string AirePiece_En = "Bounding Box Area-Blank";
            public readonly string Pliages_Fr = "Plis";
            public readonly string Pliages_En = "Bends";
            public readonly string Epaisseur_Fr = "Epaisseur de tôlerie";
            public readonly string Epaisseur_En = "Sheet Metal Thickness";
            public readonly string LongueurRect_Fr = "Longueur du flanc de tôle";
            public readonly string LongueurRect_En = "Bounding Box Length";
            public readonly string LargeurRect_Fr = "Largeur du flanc de tôle";
            public readonly string LargeurRect_En = "Bounding Box Width";
            public readonly string NumeroMateriel = "NumeroMateriel";
            public readonly string RPliage = "RPliage";
            public readonly string VPliage = "VPliage";
        }
}

Thanks!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

3

I think localization would solve this. It seems you're wanting to load language based strings, and there is already a mechanism for this. A very rough example:

public class WillyTC
{
    public string PerimetreExterne { get; }
    public string PerimetreInterne { get; }
    public string NbDecoupeInterne { get; }
    public string AireBrut { get; }
    public string AirePiece { get; }
    public string Pliages { get; }
    public string Epaisseur { get; }
    public string LongueurRect { get; }
    public string LargeurRect { get; }
    public string NumeroMateriel { get; }
    public string RPliage { get; }
    public string VPliage { get; }

    public WillyTC() : this("fr") { }

    public WillyTC(string cultureName)
    {

        var culture = CultureInfo.CreateSpecificCulture(cultureName);
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        PerimetreExterne = Resources.strings.PerimetreExterne;
        PerimetreInterne = Resources.strings.PerimetreInterne;
        NbDecoupeInterne = Resources.strings.NbDecoupeInterne;
        AireBrut = Resources.strings.AireBrut;
        AirePiece = Resources.strings.AirePiece;
        Pliages = Resources.strings.Pliages;
        Epaisseur = Resources.strings.Epaisseur;
        LongueurRect = Resources.strings.LongueurRect;
        LargeurRect = Resources.strings.LargeurRect;
        NumeroMateriel = Resources.strings.NumeroMateriel;
        RPliage = Resources.strings.RPliage;
        VPliage = Resources.strings.VPliage;
    }
}

You could then instantiate the class with the appropriate culture:

class Program
{
    static void Main(string[] args)
    {
        var inFrench = new WillyTC();
        var inEnglish = new WillyTC("en-US");
    }
}

And you set up the resource files with the localized text:

enter image description here

enter image description here

Parrish Husband
  • 3,148
  • 18
  • 40
  • I'm sure you could also use reflection in conjunction with the resource manager to set the properties too, which would cut out needing to type them all out by hand. – Parrish Husband May 23 '19 at 20:10
  • I like the effort you put in explaining to me how localization. I think this could a B plan. I want to regroup everything as much as possible in the same file. WillyTC is part of a much bigger class called MyConfiguration which is loaded at the beginning of the application through JSonDeserialization on a config file. This is why I wanted to include some sort of localization inside my configuration file. But at the same time, I'm questionning myself if this practice is the best. I like Serialization as it works fairly well with Classes and specially list of classes. – Michaël Corriveau-Côté May 24 '19 at 11:18
  • The downside with loading a ConfigFile only when the application starts is that you need to restart the application if you modify the ConfigFile in order to load the new values. On way of getting through this would be to load the file, save the "LastModifiedDate" and reload the file at a specific point only if the "LastModifiedDate" has changed. – Michaël Corriveau-Côté May 24 '19 at 11:21
1

You could store the property names in a dictionary lookup, or a resource dictionary, or a SQL table, and then try to get that value and use it. It also has the benefit of allowing you to change the languages easily outside your code.

Dictionary<string,string> languageConfigLookup = 
    new Dictionary<string,string>() {
        {"french",JSonConfigFile.FrenchPropName},
        {"english",JSonConfigFile.EnglishPropName}
    };

if (languageConfigLookup.TryGetValue(GetCurrentLanguage().ToLower(), out string propertyName))
{
   Value2Get = GetSolidworksProp(propertyName);
}
else 
{ 
   throw new Exception("Not-Handled");
}
nixkuroi
  • 2,259
  • 1
  • 19
  • 25
0

Should I just do that?

Willy Class

public class WillyTC : Willy
{
    public Dictionary<string, Dictionary<string, string>> WillyCustomProperties = new Dictionary<string, Dictionary<string, string>>();


    public WillyTC() : base()
    {
        WillyCustomProperties.Add("PerimetreExterne", new Dictionary<string, string> { { "french", "Longueur à découper extérieure" } , { "english", "Cutting Length-Outer" } });
        WillyCustomProperties.Add("PerimetreInterne", new Dictionary<string, string> { { "french", "Longueur à découper des boucles intérieures" } , { "english", "Cutting Length-Inner" } });
        WillyCustomProperties.Add("NbDecoupeInterne", new Dictionary<string, string> { { "french", "Découpes" } , { "english", "Cut Outs" } });
        WillyCustomProperties.Add("AireBrut", new Dictionary<string, string> { { "french", "Surface du flanc de tôle" } , { "english", "Bounding Box Area" } });
        WillyCustomProperties.Add("AirePiece", new Dictionary<string, string> { { "french", "Surface du flanc de tôle brut" } , { "english", "Bounding Box Area-Blank" } });
        WillyCustomProperties.Add("Pliages", new Dictionary<string, string> { { "french", "Plis" } , { "english", "Bends" } });
        WillyCustomProperties.Add("Epaisseur", new Dictionary<string, string> { { "french", "Epaisseur de tôlerie" } , { "english", "Sheet Metal Thickness" } });
        WillyCustomProperties.Add("LongueurRect", new Dictionary<string, string> { { "french", "Longueur du flanc de tôle" } , { "english", "Bounding Box Length" } });
        WillyCustomProperties.Add("LargeurRect", new Dictionary<string, string> { { "french", "Largeur du flanc de tôle" } , { "english", "Bounding Box Width" } });
        WillyCustomProperties.Add("NumeroMateriel", new Dictionary<string, string> { { "french", "NumeroMateriel" } , { "english", "NumeroMateriel" } });
        WillyCustomProperties.Add("RPliage", new Dictionary<string, string> { { "french", "RPliagee" } , { "english", "RPliage" } });
        WillyCustomProperties.Add("VPliage", new Dictionary<string, string> { { "french", "VPliage" } , { "english", "VPliage" } });

    }
}

Using it

        string lang = parentType.solidworksController.swApp.GetCurrentLanguage();
        PerimetreInterne = GetCustomPropDouble(customPropertyManager, ConfigController.config.TC.willy.WillyCustomProperties[nameof(PerimetreInterne)][lang]);
        PerimetreExterne = GetCustomPropDouble(customPropertyManager, ConfigController.config.TC.willy.WillyCustomProperties[nameof(PerimetreExterne)][lang]);
        NbDecoupeInterne = GetCustomPropInt(customPropertyManager, ConfigController.config.TC.willy.WillyCustomProperties[nameof(NbDecoupeInterne)][lang]);
        AireBrut = GetCustomPropDouble(customPropertyManager, ConfigController.config.TC.willy.WillyCustomProperties[nameof(AireBrut)][lang]);
        AirePiece = GetCustomPropDouble(customPropertyManager, ConfigController.config.TC.willy.WillyCustomProperties[nameof(AirePiece)][lang]);
        Pliages = GetCustomPropInt(customPropertyManager, ConfigController.config.TC.willy.WillyCustomProperties[nameof(Pliages)][lang]);
        Epaisseur = GetCustomPropDouble(customPropertyManager, ConfigController.config.TC.willy.WillyCustomProperties[nameof(Epaisseur)][lang]);
        LongueurRect = GetCustomPropDouble(customPropertyManager, ConfigController.config.TC.willy.WillyCustomProperties[nameof(LongueurRect)][lang]);
        LargeurRect = GetCustomPropDouble(customPropertyManager, ConfigController.config.TC.willy.WillyCustomProperties[nameof(LargeurRect)][lang]);