Sayfalar

30 Mayıs 2013 Perşembe

ASP.NET MVC DERSLERİ ( 6.7 ) DATAANNOTATIONS LOCALIZATION

Dataannotations kullanırken, kullanıcıya gösterilen mesajların, farklı dillerde olmasını nasıl sağlarız.
Öncelikle projemize bir resourse dosyası ekliyoruz.
enter image description here
resource dosyasına cevirisini yapacagımız alanları giriyoruz.
enter image description here
Resource dosyasının properties kısmına gelip aşağıdaki ayarları ekliyoruz.
enter image description here
Yani Build Action kısmına Embedded Resource ve Custom Tool kısmına isePublicResXFileCodeGenerator yazıyoruz.
Daha sonra kodlarımız:
model
[MetadataType(typeof(KullaniciMetaData))]
public class Kullanici
{
    public int Id { get; set; }
    public string KullaniciAdi { get; set; }
}

public class KullaniciMetaData
{
    public int Id { get; set; }

    [Required(ErrorMessageResourceType = typeof(App_GlobalResources.ModelValidation), ErrorMessageResourceName = "KullaniciAdiRequired")]
    [Display(Name = "KullaniciAdi", ResourceType = typeof(App_GlobalResources.ModelValidation))]
    public string KullaniciAdi { get; set; }
}
view
@model MvcMultiLanguageProject.Models.Kullanici
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.KullaniciAdi)
        @Html.TextBoxFor(x => x.KullaniciAdi)
        @Html.ValidationMessageFor(x => x.KullaniciAdi)
    </div>
    <div>
        <input type="submit" value="Gönder">
    </div>
}
Ve sonuç:
enter image description here

Hiç yorum yok:

Yorum Gönder