c# - MVC Globalization for DisplayName and DisplayFor -
i developing multilingual (english, arabic) application using mvc. application should display both labels , data based on resources files.
database designed store translated fields in same tables example
gender[id, name,arabicname] person[id,firstname,fathername,familyname,arabicfirstname, arabicfathername,arabicfamilyname,genderid]
i managed display dropdownlist
based on resources switching between name
, arabicname
fields using:
viewbag.gender= new selectlist(mentities.genders.tolist(), "id", resources.global.name); // name value in global.resx name , in global.ar.resx arabicname
and displaying labelfor
using:
[display(name="firstname", resourcetype =typeof(resources.global))] public string firstname{get;set;}
my question, possible switch between firstname
value , arabicfirstname
using displayfor
, how achieve in mvc?
for example display: firstname = antony english , arabicfirstname = انطوان arabic
i made following workaround make things work
- to distinguish between labels , data values in resource file used convension:
firstname
stands labels , firstnameresource
stands data
example resource file like:
firstname => value = first name (for english), الاسم الاول (for arabic) firstnameresource = > value = firstname ( english) , arabicfirstname (for arabic)
i coded following 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); }
you can use extension following:
@html.displayresourcefor(m=>m.firstname)
this workaround helped me solve problem, maybe there straight forward solution.
Comments
Post a Comment