What is the best way to localize a collection (IEnumerable)? From the BL I retrieve a collection of entities which still need to localized, I figured I write a method which extends the IEnumerable and returns the localized list.
本地化集合(IEnumerable)的最佳方法是什么?从BL我检索仍然需要本地化的实体集合,我想我写了一个扩展IEnumerable并返回本地化列表的方法。
How can i get the code underneath working? Any ideas? Maybe better options?
如何让代码在下面工作?有任何想法吗?也许更好的选择?
public static IEnumerable Localize(this IEnumerable items, CultureInfo cultureInfo)
{
foreach(string item in items)
{
/*Error underneath, cannot assign to item*/
item = ResourceHelper.GetString(item, cultureInfo);
}
return (items);
}
2 个解决方案
#1
2
have you tried something where you yield the item?
你尝试过产品的东西吗?
public static IEnumerable<string> Localize(this IEnumerable<string> items, CultureInfo culture)
{
foreach (string item in items)
{
yield return ResourceHelper.GetString(item,culture);
}
}
this won't change any other the items in the collection you are enumerating over, but it will return what you want it to.
这不会改变您要枚举的集合中的任何其他项目,但它会返回您想要的内容。
#2
1
Simple change to get it to return a new enumerable collection of localized values:
简单的更改,让它返回一个新的可枚举的本地化值集合:
public static IEnumerable<string> Localize(this IEnumerable<string> items, CultureInfo cultureInfo)
{
List<string> newItems = new List<string>();
foreach(string item in items)
{
newItems.Add( ResourceHelper.GetString(item, cultureInfo) );
}
return newItems;
}
#1
2
have you tried something where you yield the item?
你尝试过产品的东西吗?
public static IEnumerable<string> Localize(this IEnumerable<string> items, CultureInfo culture)
{
foreach (string item in items)
{
yield return ResourceHelper.GetString(item,culture);
}
}
this won't change any other the items in the collection you are enumerating over, but it will return what you want it to.
这不会改变您要枚举的集合中的任何其他项目,但它会返回您想要的内容。
#2
1
Simple change to get it to return a new enumerable collection of localized values:
简单的更改,让它返回一个新的可枚举的本地化值集合:
public static IEnumerable<string> Localize(this IEnumerable<string> items, CultureInfo cultureInfo)
{
List<string> newItems = new List<string>();
foreach(string item in items)
{
newItems.Add( ResourceHelper.GetString(item, cultureInfo) );
}
return newItems;
}