更换需要翻译和枚举的枚举 [英] Replacement of enum that requires translation and enumeration

查看:105
本文介绍了更换需要翻译和枚举的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些可能导出为各种格式的东西。目前我们有这样一个枚举的格式:

  [Flags] 
public enum ExportFormat
{
无= 0x0,
Csv = 0x1,
Tsv = 0x2,
Excel = 0x4,
全部= Excel | Csv | Tsv
}

问题是这些必须被枚举,他们还需要翻译或描述在呃目前我通过创建两个扩展方法来解决这个问题他们的工作,但我真的不喜欢他们或解决方案...他们感到有点臭。问题是我真的不知道我该怎么做得更好。有人有什么好的选择吗?这些是两种方法:

  public static IEnumerable< ExportFormat>格式(此ExportFormat导出格式)
{
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描述(此ExportFormat e)
{
var r = new List< string>();

if((e& ExportFormat.Csv)== ExportFormat.Csv)
r.Add(逗号分隔值);

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(,);
}

也许这是这样做的方式,但我有一种感觉做更好的方法。如何重构?

解决方案

您可以使用Describe中的Formats方法避免在多个地方进行所有位操作,如下所示:

 私有静态字典< ExportFormat,string> FormatDescriptions = 
new Dictionary< ExportFormat,string>()
{
{ExportFormat.Csv,逗号分隔值},
{ExportFormat.Tsv,Tab Separated Values },
{ExportFormat.Excel,Microsoft Excel 2007},
};

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

return string.Join(,,descript.ToArray());
}

这样,很容易将来自外部来源的字符串描述本地化,如上所述。


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. 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:

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.

这篇关于更换需要翻译和枚举的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆