简体   繁体   English

替换需要翻译和枚举的枚举

[英]Replacement of enum that requires translation and enumeration

We have some stuff that may be exported into various formats. 我们有些内容可能会导出为各种格式。 Currently we have these formats represented by an enum like this: 目前,我们有以下枚举表示的这些格式:

[Flags]
public enum ExportFormat
{
    None = 0x0,
    Csv = 0x1,
    Tsv = 0x2,
    Excel = 0x4,
    All = Excel | Csv | Tsv
}

Problem is that these must be enumerated and they also need a translation or description in the ui. 问题是这些必须枚举,并且还需要在ui中进行翻译或描述。 Currently I solved this by creating two extension methods. 目前,我通过创建两个扩展方法来解决此问题。 They work, but I don't really like them or the solution at all... they feel kind of smelly. 它们有效,但是我真的不喜欢它们或解决方案……它们感觉有点难闻。 Problem is I don't really know how I could do this better. 问题是我真的不知道该如何做得更好。 Does anyone have any good alternatives? 有人有什么好的选择吗? These are the two methods: 这是两种方法:

    public static IEnumerable<ExportFormat> Formats(this ExportFormat exportFormats)
    {
        foreach (ExportFormat e in Enum.GetValues(typeof (ExportFormat)))
        {
            if (e == ExportFormat.None || e == ExportFormat.All)
                continue;

            if ((exportFormats & e) == e)
                yield return e;
        }
    }

    public static string Describe(this ExportFormat e)
    {
        var r = new List<string>();

        if ((e & ExportFormat.Csv) == ExportFormat.Csv)
            r.Add("Comma Separated Values");

        if ((e & ExportFormat.Tsv) == ExportFormat.Tsv)
            r.Add("Tab Separated Values");

        if ((e & ExportFormat.Excel) == ExportFormat.Excel)
            r.Add("Microsoft Excel 2007");

        return r.Join(", ");
    }

Maybe this is the way to do this, but I have a feeling there must be better ways to do it. 也许这是做到这一点的方法,但是我觉得必须有更好的方法来做到这一点。 How could I refactor this? 我该如何重构?

You could use the Formats method inside Describe to avoid doing all the bit operations at multiple places, like this: 您可以使用Describe中的Formats方法来避免在多个位置执行所有位操作,如下所示:

private static Dictionary<ExportFormat, string> FormatDescriptions =
    new Dictionary<ExportFormat,string>()
{
    { ExportFormat.Csv, "Comma Separated Values" },
    { ExportFormat.Tsv, "Tab Separated Values" },
    { ExportFormat.Excel, "Microsoft Excel 2007" },            
};

public static string Describe(this ExportFormat e)
{
    var formats = e.Formats();
    var descriptions = formats.Select(fmt => FormatDescriptions[fmt]);

    return string.Join(", ", descriptions.ToArray());
}

This way, it is easy to incorporate the string descriptions from an external source or localization, as hinted above. 这样,很容易合并来自外部源或本地化的字符串描述,如上所述。

The only other way comes to my mind is the usage of the System.Attribute class. 我想到的唯一其他方法是System.Attribute类的用法。

public class FormatDescription : Attribute
{
    public string Description { get; private set; }

    public FormatDescription(string description)
    {
        Description = description;
    }
}

And then use Reflection with in your Describe function. 然后在您的Describe函数中使用Reflection with。 The only benefit of this method would be to have definition and the description at one place. 这种方法的唯一好处是将定义和描述放在一个地方。

Dupe: How do I have an enum bound combobox with custom string formatting for enum values? Dupe: 我如何使用枚举值自定义字符串格式的枚举绑定组合框?

You could write an TypeConverter that reads specified attributes to look them up in your resources. 您可以编写一个TypeConverter来读取指定的属性,以在您的资源中查找它们。 Thus you would get multi-language support for display names without much hastle. 因此,您将获得对显示名称的多语言支持,而不会产生太多麻烦。

Look into the TypeConverter's ConvertFrom/ConvertTo methods, and use reflection to read attributes on your enum fields. 查看TypeConverter的ConvertFrom / ConvertTo方法,并使用反射读取枚举字段上的属性。

Addition: 加成:

Scroll down in the linked post for a implementation of a TypeConverter that does part of what is required for full support. 在链接的帖子中向下滚动以获取TypeConverter的实现,该实现完成了完整支持所需的部分工作。

This would support an application where you have several languages at the same time, not only code name -> english name. 这将支持您同时具有多种语言的应用程序,而不仅仅是代码名称->英文名称。

Remember that this is only the display name, never the stored value. 请记住,这只是显示名称,而不是存储值。 You should always store the code name, or the integer value, to support users with different locales using the same data. 您应该始终存储代码名称或整数值,以支持使用相同数据的不同语言环境的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM