å§ããåã«ãMVCããŒã ã«æè¬ããããšæããŸãããã°ãããä»äºãããŠãããçããããã®ãã¬ãŒã ã¯ãŒã¯ã䜿çšãããšãã«Webã¢ããªã±ãŒã·ã§ã³ãäœæããããã»ã¹ã楜ããã§ããŸãã Ruby on Railsã§å°ãçµéšããåŸããã®ã¿ã€ãã®ãã¬ãŒã ã¯ãŒã¯ãæ¢ããŠããŸããã
次ã®åé¡ãæ€èšããŸãã
- æ€èšŒã衚瀺
- ã·ã³ãã«ãªäœç©åãæ¿ãã¡ã«ããºã
- ã¢ãã«æ€èšŒã¡ãã»ãŒãžã®ããŒã«ã©ã€ãº
- DisplayNameå±æ§ã®ããŒã«ã©ã€ãº
- ãã£ãã·ã¥ãšããŒã«ãªãŒãŒã·ã§ã³
ããŒã«ã©ã€ãºã衚瀺
ãã¡ãããããŒã«ã©ã€ãºãããããã¹ãã®å ŽåããªãœãŒã¹ãã¡ã€ã«ã䜿çšãããŸãããã¹ãã¬ãŒãžã«ã¯æšæºã®asp.netãã©ã«ããŒã¯äœ¿çšãããŸããã
次ã®ãã£ã¬ã¯ããªæ§é ããå§ãããŸãã
ãã¥ãŒ -aspxããŒãžã®ãªãœãŒã¹ãã¡ã€ã«ã ã¢ãã« -ãã¥ãŒã¢ãã«ãããŒã«ã©ã€ãºããããã®ãªãœãŒã¹ãã¡ã€ã«ã
Viewsãã£ã¬ã¯ããªã«ã¯ããªãœãŒã¹ãã¡ã€ã«ãé 眮ãããŠããåã³ã³ãããŒã©ãŒã®ãµããã©ã«ããŒãå«ãŸããŠããŸãïŒãã¡ã€ã«ã®æ°ã¯ããµããŒããããŠããèšèªã®ã»ããã«ãã£ãŠç°ãªããŸãïŒã
ã¢ãã«ã«ã¯ããã¥ãŒã¢ãã«ã®åã°ã«ãŒãã®ãµããã£ã¬ã¯ããªãå«ãŸããŸãã çæãããã¢ã«ãŠã³ãã¢ãã«ïŒLogOnãRegisterãChangePasswordïŒã«ã¯ãåèšèªã«ã«ãã£ã®Accountãã©ã«ããŒãšãªãœãŒã¹ãã¡ã€ã«ããããŸãã
ãªãœãŒã¹ãã¡ã€ã«
ãªãœãŒã¹ãã¡ã€ã«ã®åœåèŠåã«é¢ããããã€ãã®èšèã ãªãœãŒã¹ãã¡ã€ã«ã®ååã®åœ¢åŒã¯æ¬¡ã®ãšããã§ãã
[ãªãœãŒã¹å]ã[æå] .resx
ãªãœãŒã¹å-ãã¡ã€ã«åã 絶察ã«äœã§ãæ§ããŸããã åããªãœãŒã¹åãæã€è€æ°ã®ãªãœãŒã¹ãã¡ã€ã«ãããå Žåã«ã°ã«ãŒãåããããã«äœ¿çšãããŸã-ãããã¯CULTUREã§æå®ãããç°ãªãã«ã«ãã£ãæã€åäžã®ãªãœãŒã¹ãæ§æããŸãã
CULTURE-ãªãœãŒã¹ãã¡ã€ã«ã«ã«ãã£ã®ã€ã³ãžã±ãŒã¿ã æåã«ã¯ã äžç«ãšæ£ç¢ºã® 2ã€ã®ã¿ã€ãããããŸãã ãã¥ãŒãã©ã«ã«ã«ãã£ã¯ãèšèªã³ãŒãïŒ en ã ru ã deãªã©ïŒã®ã¿ã§æ§æãããŸãã æ£ç¢ºãªã«ã«ãã£ã¯ãèšèªã³ãŒããšå°åã³ãŒãã§æ§æãããŸãïŒ en-US ã en-UK ïŒ
ãŸããã«ã«ãã£ãå®çŸ©ãããŠããªããªãœãŒã¹ãã¡ã€ã«ã«ã¯ç¹å¥ãªæå³ãããããããã©ã«ãããŸãã¯ããã©ãŒã«ããã¯ãã«ã«ãã£ãšåŒã°ããŸãã ååããæšæž¬ã§ããããã«ãç¹å®ã®ã«ã«ãã£ãªãœãŒã¹ãã¡ã€ã«ã§ããã¹ããèŠã€ãããªãã£ãå ŽåããŸãã¯ç¹å®ã®ã«ã«ãã£ã®ãã¡ã€ã«ããªãå ŽåããªãœãŒã¹ãã¡ã€ã«ã«äœ¿çšãããŸãã ç¹ã«ãŠãŒã¶ãŒããµããŒããããŠããªãã«ã«ãã£ãäœããã®æ¹æ³ã§ã€ã³ã¹ããŒã«ã§ããå Žåã¯ãåºæ¬çãªãªãœãŒã¹ãã¡ã€ã«ã䜿çšããããšã匷ããå§ãããŸãã
ãªãœãŒã¹ãã¡ã€ã«ã®ããã€ãã®äŸïŒ
MyStrings.en-US.resx-è±èªã¢ã¡ãªã«
MyStrings.en-UK.resx-ã€ã®ãªã¹è±èª
MyStrings.en.resx-ãã¥ãŒãã©ã«è±èªïŒè±èªã®ããã©ã«ãïŒ
MyStrings.ru.resx-äžç«ãã·ã¢èª
MyStrings.resxã¯ããŒã¹ãªãœãŒã¹ãã¡ã€ã«ã§ãã
ããã§ãäœããããŒã«ã©ã€ãºããŠããããã©ã®ããã«æ©èœãããã確èªããæºåãã§ããŸããã äœæãããWebã¢ããªã±ãŒã·ã§ã³ã§ã®ããããŒã®ããŒã«ã©ã€ãºã®å°ããªäŸã瀺ããŸãã ãã®äŸã§ã¯ãè±èªïŒããã©ã«ãïŒãšäžç«ãã·ã¢èªã®2ã€ã®èšèªã䜿çšããŸãããä»ã®èšèªã䜿çšã§ããŸãã
ãŸãããã©ã«ããŒæ§é ãäœæããŸããäžèšã§èª¬æããããã«ãSite.Masterã®èŠªããŒãžã®ãªãœãŒã¹ãã¡ã€ã«ãå¿ èŠã«ãªããŸãã Resources \ Viewsã« å ±æãã£ã¬ã¯ããªãäœæãã2ã€ã®ãªãœãŒã¹ãã¡ã€ã«ãè¿œå ããŸãã
SharedStrings.resx-è±èªã®ããŒã¿ãå«ãåºæ¬çãªãªãœãŒã¹ãã¡ã€ã«ã
SharedStrings.ru.resx-ãã·ã¢èªã®ããŒã¿ãå«ãåºæ¬çãªãªãœãŒã¹ãã¡ã€ã«ã
äž¡æ¹ã®ãã¡ã€ã«ã«ãã¿ã€ãã«ãããããã£ãè¿œå ããŠå ¥åããŸãã
éèŠïŒ åãªãœãŒã¹ãã¡ã€ã«ã®ã¢ã¯ã»ã¹ä¿®é£Ÿåãpublicã«èšå®ãããŠããããšã確èªããŸãã ãŸãããªãœãŒã¹ããŒã«ã«PublicResXFileCodeGeneratorã®ã«ã¹ã¿ã ããŒã«ããããã£å€ãããããšã確èªããŸãã ããããªããšããªãœãŒã¹ãã¡ã€ã«ã¯ã³ã³ãã€ã«ããããå©çšã§ããŸããã
ãªãœãŒã¹ãã¡ã€ã«ã®åå空éã«ã€ããŠå°ã説æããŸãã ãã®æ¹æ³ã§äœæããããã¡ã€ã«ã«ã¯ã次ã®åœ¢åŒã®åå空éããããŸãã
[ãããžã§ã¯ãå] .Resources.Views.Shared
èªã¿ããããšåœåèŠåãžã®æºæ ãæ¹åããããã«ããªãœãŒã¹ãã¡ã€ã«ã®ã«ã¹ã¿ã ããŒã«åå空éããããã£ãViewRes ïŒãã¥ãŒãªãœãŒã¹ãã¡ã€ã«çšïŒã«å€æŽããŸããã
Site.MasterããŒãžã«å€æŽãå ãããšããæ¥ãŸããã
ã«çœ®ãæãã< div id ="title" >
< h1 > My MVC Application </ h1 >
</ div >
< div id ="title" >
< h1 > <% =ViewRes.SharedStrings.Title %> </ h1 >
</ div >
ã¢ããªã±ãŒã·ã§ã³ãå®è¡ããŠããã¹ãŠãæ£åžžã«æ©èœããããããŒãæ£ããå Žæã«è¡šç€ºãããããšã確èªããŸãïŒãªãœãŒã¹ãã¡ã€ã«ããèªã¿åãå¿ èŠããããŸãïŒã ãã¹ãŠãããŸããã£ãããæåãå€ããæ¹æ³ã瀺ããŸãããã
ã«ã«ãã£ãå€æŽããã«ã¯ããªã¯ãšã¹ãããšã« CurrentThreadãªããžã§ã¯ãã®CurrentCultureããã³CurrentUICultureããããã£ãå€æŽããå¿ èŠããããŸã!!! ãããè¡ãã«ã¯ãã«ã«ãã£ãå€æŽããã³ãŒããApplication_AcquireRequestState Global.asaxã¡ãœããã«é 眮ããŸãïŒãã®ã¡ãœããã¯ã€ãã³ããã³ãã©ãŒã§ããããªã¯ãšã¹ãããšã«åŒã³åºãããŸãïŒ
Global.asax.csãã¡ã€ã«ã«æ¬¡ã®ã³ãŒããè¿œå ããŸãã
protected void Application_AcquireRequestState( object sender, EventArgs e)
{
//Create culture info object
CultureInfo ci = new CultureInfo( "en" );
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}
ã¢ããªã±ãŒã·ã§ã³ãå床起åããæ©èœããããšã確èªããŸãã 次ã«ãCulturInfoã³ã³ã¹ãã©ã¯ã¿ãŒã®æååãã©ã¡ãŒã¿ãŒãå€æŽãïŒç§ã®å Žåã¯ãruãã«ãªããŸãïŒããããžã§ã¯ããåèµ·åããŸãã 次ã®ãã®ãå¿ èŠã§ãã
以äžã§ãã Site.MasterããããŒãããŒã«ã©ã€ãºãããããä»»æã®ããã¹ãã§ãããè¡ãããšãã§ããŸãã
ã·ã³ãã«ãªäœç©åãæ¿ãã¡ã«ããºã
åã®ç« ã§ã¯ãã¢ããªã±ãŒã·ã§ã³ã¿ã€ãã«ã®ããŒã«ã©ã€ãºã«æåããŸãããããªã¢ã«ã¿ã€ã ã§ã«ã«ãã£ãå€æŽããæ¹æ³ã¯ãããŸããã§ããã 次ã«ãæåçç°å¢ããªã¢ã«ã¿ã€ã ã§å¶åŸ¡ããã®ã«åœ¹ç«ã€äœããã®ã¡ã«ããºã ãäœæããŸãã
ãŠãŒã¶ãŒãéžæããã«ã«ãã£ã®ãªããžããªãã»ãã·ã§ã³ãªããžã§ã¯ãã䜿çšããŸãã æåãå€ããããã«ãåèšèªã®ãªã³ã¯ããã¹ã¿ãŒããŒãžã«é 眮ããŸãã ãªã³ã¯ãã¯ãªãã¯ãããšãã¢ã«ãŠã³ãã³ã³ãããŒã©ãŒã§ã¢ã¯ã·ã§ã³ãçºçããã»ãã·ã§ã³å€ãé©åãªã«ã«ãã£ã«å€æŽãããŸãã
次ã®ã³ãŒããAccountControllerã¯ã©ã¹ã«è¿œå ããŸãã
public ActionResult ChangeCulture( string lang, string returnUrl)
{
Session[ "Culture" ] = new CultureInfo(lang);
return Redirect(returnUrl);
}
ããã§ã2ã€ã®ãã©ã¡ãŒã¿ãŒãæã€ã¢ã¯ã·ã§ã³ã¡ãœãããäœæãããŸããã1ã€ç®ã¯ã«ã«ãã£ã³ãŒãã2ã€ç®ã¯ãŠãŒã¶ãŒãå ã®ããŒãžã«ãªãã€ã¬ã¯ãããããã®ã¢ãã¬ã¹ã§ãã ã»ãã·ã§ã³ã³ã¬ã¯ã·ã§ã³ã«æ°ããã«ã«ãã£ãèšå®ããå¿ èŠããããŸããããµããŒããããŠããªãã«ã«ãã£ã®ã€ã³ã¹ããŒã«ãé²ãããã«ãåä¿¡ãããŠãŒã¶ãŒããŒã¿ã®ãã§ãã¯ãè¿œå ããããšãå¿ããªãã§ãã ããã
次ã«ãæåçãªåç §ãå«ãåçŽãªãŠãŒã¶ãŒã³ã³ãããŒã«ãäœæããŸãã Views \ Sharedãã£ã¬ã¯ããªã«æ°ããéšåãã¥ãŒãè¿œå ãããã¡ã€ã«ã«CultureChooserUserControl.ascxãšããååãä»ããŠã次ã®ãã¡ã€ã«ã貌ãä»ããŸãã
<% = Html.ActionLink( "English" , "ChangeCulture" , "Account" ,
new { lang = "en" , returnUrl = this .Request.RawUrl }, null ) %>
<% = Html.ActionLink( "" , "ChangeCulture" , "Account" ,
new { lang = "ru" , returnUrl = this .Request.RawUrl }, null ) %>
ããã§ã2ã€ã®ãªã³ã¯ãäœæããŸããã1ã€ã¯è±èªçšã§ã2ã€ç®ã¯ãã·ã¢èªçšã§ãã Site.MaterããŒãžã«ãã®ã³ã³ãããŒã«ãé 眮ãããšããæ¥ãŸããã ãã°ã€ã³ãã©ãŒã ã®æšªã«è¿œå ããŸãã
<div id = "logindisplay">ã³ãŒããèŠã€ããŠæ¬¡ã®ã³ãŒãã«çœ®ãæããŸãã
< div id ="logindisplay" >
<% Html.RenderPartial( "LogOnUserControl" ); %>
<% Html.RenderPartial( "CultureChooserUserControl" ); %>
</ div >
ä»ã«äœãããå¿ èŠããããŸããïŒ ãããŠãæãéèŠãªããšã¯æ®ã£ãŠããŸããç§ãã¡ã¯æåã«é¢ããæ å ±ã®ãªããžã§ã¯ããã»ãã·ã§ã³ã«é 眮ããŸããããããã©ãã«ã䜿çšããŸããã Global.asax.csãã¡ã€ã«ã¡ãœããApplication_AcquireRequestStateãå床å€æŽããŸãã
protected void Application_AcquireRequestState( object sender, EventArgs e)
{
//
if ( HttpContext .Current.Session != null )
{
CultureInfo ci = (CultureInfo) this .Session[ "Culture" ];
// ,
//
//
if (ci == null )
{
// -
string langName = "en" ;
// HTTP
if ( HttpContext .Current.Request.UserLanguages != null && HttpContext .Current.Request.UserLanguages.Length != 0)
{
//
langName = HttpContext .Current.Request.UserLanguages[0].Substring(0, 2);
}
ci = new CultureInfo(langName);
this .Session[ "Culture" ] = ci;
}
//
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}
}
ã¢ããªã±ãŒã·ã§ã³ãèµ·åãããšã次ã®ããŒãžã衚瀺ãããŸãããªã³ã¯ãã¯ãªãã¯ãããšãéžæããã«ã«ãã£ã§ããŒãžããªãŒããŒããŒããããŸãã
ã¢ãã«æ€èšŒã¡ãã»ãŒãžã®ããŒã«ã©ã€ãº
Phil Haackãçºè¡ãããã®åé¡ã«å¯Ÿããåªãã解決çãèŠã€ããŸãããããã®èšäºã¯å®å šãªã¬ã€ãã§ããããããã®åé¡ã«è§ŠããããåŸããæ確ã«ãããç¹å®ã®ææ§ãããããŸãã ãããããŸã第äžã«ã Phil Haackã®æçš¿ãèªãããšããå§ãããŸã
ã¢ã«ãŠã³ãã¢ãã«ãç¹ã«RegistrationModelã®æ€èšŒã¡ãã»ãŒãžãããŒã«ã©ã€ãºããæ¹æ³ã説æããŸãã ãŸããAccountControllerã³ãŒãã«çŽæ¥èšè¿°ãããã¡ã³ããŒã·ããæ€èšŒã¡ãã»ãŒãžãããŒã«ã©ã€ãºããæ¹æ³ã«ã€ããŠã説æããŸãã
ããã§ã Resources \ Models \ Accountãã©ã«ããŒã«ValidationStrings.resxãšValidationStrings.ru.resxãäœæããŸãããïŒã¢ã¯ã»ã¹ä¿®é£Ÿåããããªãã¯ã§ããããšã確èªããŠãã ããïŒã ãæ³åã®ãšããããããã®ãã¡ã€ã«ã«æ€èšŒã¡ãã»ãŒãžãä¿åããŸãã
äž¡æ¹ã®ãã¡ã€ã«ã«æ¬¡ã®ããããã£ãäœæããŸããïŒè±èªã®äŸïŒïŒ
ã¢ãã«ã次ã®ããã«å€æŽããå¿ èŠããããŸãïŒRegisterModelã®äŸïŒïŒ
[PropertiesMustMatch( "Password" , "ConfirmPassword" ,
ErrorMessageResourceName = "PasswordsMustMatch" ,
ErrorMessageResourceType = typeof (ValidationStrings))]
public class RegisterModel
{
[Required(ErrorMessageResourceName = "Required" , ErrorMessageResourceType = typeof (ValidationStrings))]
[DisplayName( "Username" )]
public string UserName { get ; set ; }
[Required(ErrorMessageResourceName = "Required" , ErrorMessageResourceType = typeof (ValidationStrings))]
[DataType(DataType.EmailAddress)]
[DisplayName( "Email" )]
public string Email { get ; set ; }
[Required(ErrorMessageResourceName = "Required" , ErrorMessageResourceType = typeof (ValidationStrings))]
[ValidatePasswordLength(ErrorMessageResourceName = "PasswordMinLength" ,ErrorMessageResourceType = typeof (ValidationStrings))]
[DataType(DataType.Password)]
[DisplayName( "Password" )]
public string Password { get ; set ; }
[Required(ErrorMessageResourceName = "Required" , ErrorMessageResourceType = typeof (ValidationStrings))]
[DataType(DataType.Password)]
[DisplayName( "Confirm password" )]
public string ConfirmPassword { get ; set ; }
}
ErrorMessageResourceNameããããã£ãšErrorMessageResourceTypeããããã£ãRequired ã PropertiesMustMatchã ValidatePasswordLengthå±æ§ã«è¿œå ããŸããããã§ã ErrorMessageResourceTypeã¯ã¡ãã»ãŒãžãä¿åããããªãœãŒã¹ã¯ã©ã¹ã®ã¿ã€ãã§ã ErrorMessageResourceNameã¯ããããã£ã®ååã§ãã æ®å¿µãªãããããããã£ãèªã¿åãããã®å³å¯ã«åæå®ãããããŒãžã§ã³ã¯ãããŸããããããã£ãŠããããã®éæ³ã®è¡ãæ£ããå€ãæã£ãŠããããšã確èªããŠãã ããã
ç§ãã¡ã¯ã»ãšãã©ããã«ããŸãã詳现ã¯ãŸã æ®ã£ãŠããŸãã ç¬èªã®æ€èšŒå±æ§PropertiesMustMatchAttributeããã³ValidatePasswordLenghtAttributeã2ã€ããã FormatErrorMessageã¡ãœããã®CultureInfo.CurrentUICultureãCultureInfo.CurrentCultureã«å€æŽããå¿ èŠããããŸããå€æŽããªãå Žåãäœãæ©èœããŸããã
ã¢ããªã±ãŒã·ã§ã³ãèµ·åããç»é²ããŒãžã«ç§»åããã«ã«ãã£ãå€æŽããããã®èšèªãéžæãã空ã®ãã©ãŒã ãéä¿¡ãããšãã«ããã以äžãååŸããŸãã
ããããæ°ã¥ãã®ããã«ããã¬ãŒã³ããŒã·ã§ã³ã¢ãã«ã®ããããã£åãããŒã«ã©ã€ãºããã®ãå¿ããŠãå°ãæ··ä¹±ããŸããã ãããè¡ãã«ã¯ãDisplayNameå±æ§ã®å€ãããŒã«ã©ã€ãºããå¿ èŠããããŸãããããã¯äžèŠããã»ã©ç°¡åã§ã¯ãããŸããã ããã«ã€ããŠã¯æ¬¡ã®ç« ã§èª¬æããŸãããæ®ãã®è©³çŽ°ã¯çµäºããŸãããã ããã¯ãMembership APIã®æ€èšŒã¡ãã»ãŒãžã®ããŒã«ã©ã€ãºã§ãã
AccountControllerãéããŠãã¡ã€ã«ã®æ«å°Ÿã«ç§»åãããšãErrorCodeToStringã¡ãœãããååšããã¯ãã§ãããã®ã¡ãœããã¯ããŠãŒã¶ãŒç»é²ã倱æããå Žåã«ãšã©ãŒã¡ãã»ãŒãžãäœæããŸãã ãã¹ãŠã®ã¡ãã»ãŒãžã¯ã³ãŒãã«ããŒãã³ãŒãã£ã³ã°ãããŠããŸãã å¿ èŠãªã®ã¯ãæ¢ã«äœæãããValidationStringsãªãœãŒã¹ãã¡ã€ã«å ã®åã¡ãã»ãŒãžã«é©åãªããããã£ãäœæããErrorCodeToStringã¡ãœããå ã®ããã¹ãã®ä»£ããã«ããããé 眮ããããšã ãã§ãã
æ€èšŒã¢ãã«ã¯ããã§ãã¹ãŠã§ãã DisplayNameã®æéã§ãïŒ
DisplayNameå±æ§ã®ããŒã«ã©ã€ãº
åã®ç« ã§è¿°ã¹ãããã«ãDisplayNameå€ã¯ããã©ãŒãããã«ãã©ã¡ãŒã¿ãŒã䜿çšããæ€èšŒã¡ãã»ãŒãžã«å«ãŸããŸãã DisplayNameå±æ§ã«ã€ããŠèãããã1ã€ã®çç±ã¯ãHTMLãã©ãŒã ã®ããã¹ããã£ãŒã«ãã©ãã«ã§ãããããã¯DisplayNameå€ã®åå ã«ãã£ãŠäœæãããŸãã
æ¬åœã®åé¡ã¯ãDisplayNameãããŒã«ã©ã€ãºããµããŒãããŠããªãããšã§ããããã¯ãå€ãååŸããå ŽæãããªãœãŒã¹ãã¡ã€ã«ã«é¢é£ä»ããæ¹æ³ããããŸããã
ããã¯ãDisplayNameAttributeãæ¡åŒµããDisplayNameããããã£ãæžãæããŠãããŒã«ã©ã€ãºãããååãè¿ãå¿ èŠãããããšãæå³ããŸãã ç¶æ¿ã¯ã©ã¹ãäœæããLocalizedDisplayNameãšåŒã³ãŸããã
public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
private PropertyInfo _nameProperty;
private Type _resourceType;
public LocalizedDisplayNameAttribute( string displayNameKey)
: base (displayNameKey)
{
}
public Type NameResourceType
{
get
{
return _resourceType;
}
set
{
_resourceType = value ;
// nameProperty, set'
_nameProperty = _resourceType.GetProperty( base .DisplayName, BindingFlags.Static | BindingFlags.Public);
}
}
public override string DisplayName
{
get
{
//,nameProperty null
if (_nameProperty == null )
{
return base .DisplayName;
}
return ( string )_nameProperty.GetValue(_nameProperty.DeclaringType, null );
}
}
}
éèŠãªè©³çŽ°ã¯ãèŠæ±ããããã³ã«ããããã£ã®å€ãèªã¿åãå¿ èŠããããšããç解ã§ãããããã£ãŠãGetValueã¡ãœããã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒã§ã¯ãªãDisplayNameããããã£ã®get'erã§åŒã³åºãããŸãã
衚瀺åãä¿åããããã«ã Resources \ Models \ Accountãã©ã«ããŒã«Names.resxããã³Names.ru.resxãªãœãŒã¹ãã¡ã€ã«ãäœæãã次ã®ããããã£ãäœæããŸããã
次ã«ãDisplayNameå±æ§ãLocalizedDisplayNameã«çœ®ãæããŠããªãœãŒã¹ã¯ã©ã¹ã®ã¿ã€ããæå®ããå¿ èŠããããŸãã å€æŽãããRegisterModelã³ãŒãã¯æ¬¡ã®ããã«ãªããŸãã
[PropertiesMustMatch( "Password" , "ConfirmPassword" ,
ErrorMessageResourceName = "PasswordsMustMatch" ,
ErrorMessageResourceType = typeof (ValidationStrings))]
public class RegisterModel
{
[Required(ErrorMessageResourceName = "Required" , ErrorMessageResourceType = typeof (ValidationStrings))]
[LocalizedDisplayName( "RegUsername" , NameResourceType = typeof (Names))]
public string UserName { get ; set ; }
[Required(ErrorMessageResourceName = "Required" ,ErrorMessageResourceType = typeof (ValidationStrings))]
[DataType(DataType.EmailAddress)]
[LocalizedDisplayName( "RegEmail" , NameResourceType = typeof (Names))]
public string Email { get ; set ; }
[Required(ErrorMessageResourceName = "Required" , ErrorMessageResourceType = typeof (ValidationStrings))]
[ValidatePasswordLength(ErrorMessageResourceName = "PasswordMinLength" ,
ErrorMessageResourceType = typeof (ValidationStrings))]
[DataType(DataType.Password)]
[LocalizedDisplayName( "RegPassword" , NameResourceType = typeof (Names))]
public string Password { get ; set ; }
[Required(ErrorMessageResourceName = "Required" , ErrorMessageResourceType = typeof (ValidationStrings))]
[DataType(DataType.Password)]
[LocalizedDisplayName( "RegConfirmPassword" , NameResourceType = typeof (Names))]
public string ConfirmPassword { get ; set ; }
}
* This source code was highlighted with Source Code Highlighter .
ã¢ããªã±ãŒã·ã§ã³ãå®è¡ããåäœããŠããããšã確èªããŠãã ããã次ã®ããã«ãªããŸãã
ãã£ãã·ã¥ãšããŒã«ãªãŒãŒã·ã§ã³
å¥åŠãªç« ã§ãããïŒ ãã£ãã·ã³ã°ãšããŒã«ãªãŒãŒã·ã§ã³ãçµã¿åãããæ¹æ³ãèããŠããŸããïŒ OKã次ã®ã·ããªãªãæ³åããŠã¿ãŸããããHomeControllerãéããOutputCacheå±æ§ãIndexã¢ã¯ã·ã§ã³ã¡ãœããã«è¿œå ããŸãã
[OutputCache(Duration=3600, VaryByParam= "none" )]
public ActionResult Index()
{
ViewData[ "Message" ] = "Welcome to ASP.NET MVC!" ;
return View();
}
ã¢ããªã±ãŒã·ã§ã³ãèµ·åãã[ã€ã³ããã¯ã¹]ããŒãžã§èšèªãå€æŽããŠãããããŒããŸã ããŒã«ã©ã€ãºãããŠããããšã確èªããŸãã
ãªããŠãã£ãïŒ ãã£ãã·ã³ã°ãšããŒã«ãªãŒãŒã·ã§ã³ãäžç·ã«äœ¿çšããããšã¯ã§ããªããšãèãã§ããïŒ å¿é ããªãã§ãã ããããã®åé¡ã®è§£æ±ºçãååšããŸã:)
OutputCacheã«ã€ããŠäœãç¥ã£ãŠããŸãããVaryByCustomããããã£ã«ã€ããŠäœãç¥ã£ãŠããŸããïŒ ããã䜿çšããæéã§ãã
ã€ã³ããã¯ã¹ããŒãžãåããŠèŠæ±ãããšãOutputCacheãããŒãžããã£ãã·ã¥ããŸãã 2çªç®ã®ãªã¯ãšã¹ãïŒèšèªéžæãªã³ã¯ãã¯ãªãã¯ãããšãïŒã§ãOutputCacheã¯äœãå€æŽãããŠããªããšå€æãããã£ãã·ã¥ããçµæãè¿ããããããŒãžã¯åäœæãããŸããã ãã®ãããèšèªã®éžæãæ©èœããŸããã§ããã ãã®åé¡ã解決ããã«ã¯ãããŒãžã®ããŒãžã§ã³ãå€æŽãããããšãäœããã®æ¹æ³ã§OutputCacheã«äŒããå¿ èŠããããŸãïŒã¢ã¯ã·ã§ã³ãç¹å®ã®ãã©ã¡ãŒã¿ãŒãåãåããVaryByParamããããã£ã«æž¡ãå Žåãªã©ïŒã
VaryByCustomã¯ãã®åé¡ã®è§£æ±ºçã®çæ³çãªåè£ã§ãããGlobal.asax.csãã¡ã€ã«ã®System.Web.HttpApplicationã¯ã©ã¹ã«ç¹å¥ãªã¡ãœããããããŸãã ãã®ã¡ãœããã®æšæºå®è£ ãæžãçŽããŸãã
public override string GetVaryByCustomString( HttpContext context, string value )
{
if ( value .Equals( "lang" ))
{
return Thread.CurrentThread.CurrentUICulture.Name;
}
return base .GetVaryByCustomString(context, value );
}
ãŸãããã®ã¡ãœããã¯ãã©ã¡ãŒã¿ãŒå€ããlangãïŒç¹å¥ãªå€ãªããVaryByCustomã®å€ãšããŠäœ¿çšãããåçŽãªæååïŒã«äžèŽãããã©ããã確èªããäžèŽããå Žåã¯çŸåšã®ã«ã«ãã£ã®ååãè¿ããŸãã ãã以å€ã®å Žåã¯ãæšæºå®è£ ã®å€ãè¿ããŸãã
次ã«ãããŒã«ã©ã€ãºã䜿çšããåOutputCacheå±æ§ã«å€ãlangããæã€VaryByCustomããããã£ãè¿œå ããŸããããããã¹ãŠã§ãã æŽæ°ãããã€ã³ããã¯ã¹ã¢ã¯ã·ã§ã³ã¡ãœããã¯æ¬¡ã®ãšããã§ãã
[OutputCache(Duration=3600,VaryByParam= "none" , VaryByCustom= "lang" )]
public ActionResult Index()
{
ViewData[ "Message" ] = "Welcome to ASP.NET MVC!" ;
return View();
}
ããäžåºŠã¢ããªã±ãŒã·ã§ã³ãå®è¡ããŠã¿ãŠãäœæ¥ã®åãæ¿ãã楜ããã§ãã ããã
æåŸã®ç« ã¯çµãããŸããããäœãèŠéããŠããªãããšãé¡ã£ãŠããŸãã