2011-12-19 22 views
8

Chúng tôi có các chuỗi cục bộ trong cơ sở dữ liệu và muốn biết nếu extending the ASP.NET Resource Provider Model sẽ hoạt động với công cụ xem ASP.NET MVC 3 Razor.ASP.NET MVC 3: - Sử dụng cơ sở dữ liệu thay vì tệp tài nguyên làm cửa hàng bản địa hóa

Vui lòng cho tôi biết nếu công cụ xem ASP.NET MVC 3 Razor hỗ trợ truy xuất chuỗi được bản địa hóa từ cơ sở dữ liệu khi chúng tôi đã mở rộng mô hình Nhà cung cấp tài nguyên ASP.NET. Hoặc nó chỉ làm việc với Classic ASP.NET và không phải với ASP.NET MVC.

Cảm ơn bạn

Satyaprakash J

+0

bản sao có thể của [ASP.NET MVC 2 Localization/Globalizatio n được lưu trữ trong cơ sở dữ liệu?] (http://stackoverflow.com/questions/2568129/asp-net-mvc-2-localization-globalization-stored-in-the-database) – jrummell

Trả lời

9

Giải pháp sạch nhất tôi đã tìm thấy cho đến nay là: http://www.codeproject.com/Tips/514321/A-Simple-and-Effective-Way-to-Localize-ASP-Net-MVC.

Nhận xét/phản hồi được hoan nghênh.

Chỉnh sửa 1: Dựa trên nhận xét, tôi đã thêm các ví dụ mã và sử dụng liên kết làm tham chiếu.

Tôi tạo ra một lớp customDataAnnotationsProvider:

public class CustomDataAnnotationsProvider: DataAnnotationsModelMetadataProvider 
{ 
    private ResourceManager resourceManager = new ResourceManager(); 
    protected override ModelMetadata CreateMetadata(
         IEnumerable<Attribute> attributes, 
         Type containerType, 
         Func<object> modelAccessor, 
         Type modelType, 
         string propertyName) 
    { 
     string key = string.Empty; 
     string localizedValue = string.Empty; 


     foreach (var attr in attributes) 
     { 
      if (attr != null) 
      { 
       if (attr is DisplayAttribute) 
       { 
        key = ((DisplayAttribute)attr).Name; 
        if (!string.IsNullOrEmpty(key)) 
        { 
         localizedValue = resourceManager.GetLocalizedText(key); 
         ((DisplayAttribute)attr).Name = localizedValue; 
        } 
       } 
       else if (attr is ValidationAttribute) 
       { 
        key = ((ValidationAttribute)attr).ErrorMessage; 
        if (!string.IsNullOrEmpty(key)) 
        { 
         localizedValue = resourceManager.GetLocalizedText(key); 
         ((ValidationAttribute)attr).ErrorMessage = localizedValue; 
        } 
       } 
      } 
     } 
     return base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); 
    } 
} 

Sau đó, tôi tham khảo các nhà cung cấp tùy chỉnh trên ApplicationStart trong Global.asax

ModelMetadataProviders.Current = new Project.Web.Helpers.CustomDataAnnotationsProvider(); 

Bạn không cần phải thay đổi mô hình của bạn và có thể sử dụng chú thích hiển thị :

[Display(Name = "CustomerAccountNumber")] 
public string CustomerAccountNumber { get; set; } 
+0

Lưu ý rằng các câu trả lời chỉ có liên kết được khuyến khích, các tham chiếu có xu hướng bị hạn chế theo thời gian. Xem xét thêm một bản tóm tắt độc lập ở đây, giữ liên kết dưới dạng tham chiếu. – kleopatra

Các vấn đề liên quan