3

Dear all, I´m working on an mvc application that needs to support two or more languages and covering UI for different countries. I would like to receive some guidance of the best approach in order to store user selection for language and portal. I´ve been reading and it seems that:

  • It could be stored in a cookie reading those values as permanent data and recreate that cookie when some value changes
  • Another case they prefer to set the information at the httpcontext in session.
  • Another approach use a base controller to change data taking into consideration the locale selection.

any idea, pros cons will be appreciated.Thanks in advance. brgds.

s_h
  • 1,476
  • 2
  • 27
  • 61

3 Answers3

2

I used this schema and I am happy with it.

It has the language in the route. This turns out to be convenient:

  • at some point you will have to email a link to a page with the language choosen
  • SEO
  • its easy to switch between languages in development and in the translation process
  • no problems with your load balancer
  • frontend tests are more stable if you can define the language in the url
Community
  • 1
  • 1
Mathias F
  • 15,906
  • 22
  • 89
  • 159
2

The approach i found for internationalization of Enums or getting text of Enums from respective Resource files is to create an attribute class by inheriting DescriptionAttribute class

public class EnumResourceAttribute : DescriptionAttribute
{

    public Type ResourceType { get; private set; }
    public string ResourceName { get; private set; }
    public int SortOrder { get; private set; }
    public EnumResourceAttribute(Type ResourceType,
                         string ResourceName,
                         int SortOrder)
    {

        this.ResourceType = ResourceType;
        this.ResourceName = ResourceName;
        this.SortOrder = SortOrder;
    }
}

Create another Static class that will provide extension methods for GetString and GetStrings.

public static class EnumHelper
{
    public static string GetString(this Enum value)
    {
        EnumResourceAttribute ea =
       (EnumResourceAttribute)value.GetType().GetField(value.ToString())
        .GetCustomAttributes(typeof(EnumResourceAttribute), false)
         .FirstOrDefault();
        if (ea != null)
        {
            PropertyInfo pi = ea.ResourceType
             .GetProperty(CommonConstants.ResourceManager);
            if (pi != null)
            {
                ResourceManager rm = (ResourceManager)pi
                .GetValue(null, null);
                return rm.GetString(ea.ResourceName);
            }

        }
        return string.Empty;
    }


    public static IList GetStrings(this Type enumType)
    {
        List<string> stringList = new List<string>();
        FieldInfo[] fiArray = enumType.GetFields();
        foreach (FieldInfo fi in fiArray)
        {
            EnumResourceAttribute ea =
                (EnumResourceAttribute)fi
                     .GetCustomAttributes(typeof(EnumResourceAttribute), false)
                     .FirstOrDefault();
            if (ea != null)
            {
                PropertyInfo pi = ea.ResourceType
                                    .GetProperty(CommonConstants.ResourceManager);
                if (pi != null)
                {
                    ResourceManager rm = (ResourceManager)pi
                                          .GetValue(null, null);
                    stringList.Add(rm.GetString(ea.ResourceName));
                }
            }
        }
        return stringList.ToList();
    }
}

And on the elements of your Enum you can write :

public enum Priority
{
         [EnumResourceAttribute(typeof(Resources.AdviceModule), Resources.ResourceNames.AdviceCreateAdviceExternaPriorityMemberHigh, 1)]
        High,
         [EnumResourceAttribute(typeof(Resources.AdviceModule), Resources.ResourceNames.AdviceCreateAdviceExternaPriorityMemberRoutine, 2)]
        Routine
 }

Where Resources.ResourceNames.AdviceCreateAdviceExternaPriorityMemberHigh & Resources.ResourceNames.AdviceCreateAdviceExternaPriorityMemberRoutine are constants in the resource file or you can say the strings whose values can be available in different cultures.

If you are implementing your web application in MVC architecture then create a property

private IList result;
public IList Result
        {
            get
            {
                result = typeof(Priority).GetStrings();
                return result;
            }
        }

and in your .cshtml file you can just bind the enum to your dropdownlist like :

 @Html.DropDownListFor(model => Model.vwClinicalInfo.Priority, new SelectList(Model.Result))
Spontifixus
  • 6,570
  • 9
  • 45
  • 63
Swati
  • 21
  • 1
1

Session/cookies are good for this purpose.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • hi darin, thank you so much. Any recommendation or tutorial in order to handle this approach?. I´m mostly concern of how I can persist user selection data in my application taking into consideration that they can choose not only language, but country either based on the fact that it will cover more than once. brgds! – s_h Apr 28 '11 at 20:26
  • @sebastian_h, did you follow the link in my answer? – Darin Dimitrov Apr 28 '11 at 20:27
  • uauu,no I read it like text.. sorry @Darin_Dimitrov :S . I read in the past his post , even hi posted a new one discouraging to use that approach and using this instead http://adamyan.blogspot.com/2010_07_01_archive.html focused on routes... but it seems to be more focused on url generation and seo issues. I will read it again. thank you – s_h Apr 28 '11 at 20:34