What is the best way to localize IEnumerable?

What is the best way to localize a collection (IEnumerable)? From BL I retrieve a set of objects that still need localization, I decided to write a method that extends IEnumerable and returns a localized list.

How can I get the code at work? Any ideas? Maybe the best 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);
}
+3
source share
2 answers

Have you tried something where you yield item?

public static IEnumerable<string> Localize(this IEnumerable<string> items, CultureInfo culture)
{
    foreach (string item in items)
    {
        yield return ResourceHelper.GetString(item,culture);
    }
}

this will not change any other elements in the collection you list, but will return what you want.

+2
source

A simple change to make it return a new enumerated 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

Source: https://habr.com/ru/post/1698495/


All Articles