Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

internationalization mvc3 - which is the best approach? session, cookie?

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.

like image 355
s_h Avatar asked Jan 28 '26 16:01

s_h


2 Answers

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
like image 190
Mathias F Avatar answered Jan 30 '26 05:01

Mathias F


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))
like image 32
Swati Avatar answered Jan 30 '26 06:01

Swati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!