Global MVC Globalization for DisplayName and DisplayFor

I am developing a multilingual (English, Arabic) application using MVC. This application should display both tags and data based on resource files.

Database

designed to store translated fields in the same tables, for example

  Gender[Id, Name,ArabicName]
  Person[Id,FirstName,FatherName,FamilyName,ArabicFirstName,
           ArabicFatherName,ArabicFamilyName,GenderId]

I managed to display everything dropdownlistbased on resources by switching between fields Nameand ArabicNameusing:

        ViewBag.Gender= new SelectList(mEntities.Genders.ToList(), "Id",
                                       Resources.Global.Name);
    // Name value in the Global.resx is Name and in Global.ar.resx is ArabicName

as well as display LabelForwith:

[Display(Name="FirstName", ResourceType =typeof(Resources.Global))]
public string FirstName{get;set;}

My question is: is it possible to switch between FirstName value and ArabicFirstNameusing DisplayForand how to achieve this in MVC?

For example, to display: FirstName = Antony for english , ArabicFirstName = انطوان for arabic

+4
source share
1 answer

,

  • , convension:

FirstName , FirstNameResource

, :

   FirstName => value = First Name (for english), الاسم الاول (for arabic)
   FirstNameResource = > value = FirstName ( for english) , ArabicFirstName (for arabic)
  1. HtmlHelper

     public static MvcHtmlString DisplayResourceFor<TModel, TValue>(this HtmlHelper<TModel> helper, 
        Expression<Func<TModel, TValue>> expression)
    {
        var rm = new ResourceManager("Resources.Global", 
            System.Reflection.Assembly.Load("App_GlobalResources"));
        var resource = rm.GetString(string.Format("{0}Resource", 
            ExpressionHelper.GetExpressionText(expression)));
        var value = helper.ViewContext.ViewData.GetViewDataInfo(resource).Value.ToString();
        return MvcHtmlString.Create(value);
    }
    
  2. :

    @Html.DisplayResourceFor( = > m.FirstName)

, , , .

+1

All Articles