locked
Best practice for show localized display name for MetaTable and MetaColumn RRS feed

  • Question

  • User-323784514 posted

    Hi, I'm setting the DisplayName in database model's Metatable, and it can be shown in my ASP.NET DYNAMIC DATA website, like this:

    [ScaffoldTable(true)]
    [MetadataType(typeof(ItemMetadata))]
    public class ItemModel
    {
        public Guid Id { get; set; }
        ...
        ...
    }
    
    [DisplayName("ThisIsMyItemModelDisplayName")]
    class ItemMetadata
    {
        [ScaffoldColumn(true)]
        [Display(Order = 1)]
        public Guid Id;
    
        [ScaffoldColumn(true)]
        [Display(Name = "ThisIsItemName", Order = 3)]
        public string ItemName;
    
        ...
    }

    it's good, the website can reflect and show the string I've put in, but now my website need to support different locale languages, so I created a customized Attribute to allow pass in a RESOURCE string key, 

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Field, AllowMultiple = false)]
    public class LocalizeDisplayName : Attribute
    {
        public LocalizeDisplayName(string localizedStringKey)
        {
            this.StringKey = localizedStringKey;
        }
    
        public string StringKey { get; }
    }

    use it like this:

    [LocalizeDisplayName("DynamicDataWebSite_Model_Item_LocalizedDisplayName")]
    class ItemMetadata
    {
        [ScaffoldColumn(true)]
        [Display(Order = 1)]
        public Guid Id;
    
        [ScaffoldColumn(true)]
        [LocalizeDisplayName(Name = "DynamicDataWebSite_Model_Item_Column_ItemName", Order = 3)]
        public string ItemName;
    
        ...
    }

    and then in website pages code which for showing this model, I can resolve the localized string key from a RESOURCE file, then get the localized string:

    var target = metaTable.Attributes[typeof(LocalizeDisplayName)];
    var localizedStr = Resources.Strings.ResourceManager.GetString(((LocalizeDisplayName)target).StringKey, CultureInfo.CurrentCulture);

     above code is easy to inject to most code in aspx.cs, but for those GridView and etc., most of its binding logical are behind the scence, like this is a sample code from Visual Studio Dynamic data website template for showing a list of values for a model:

    <asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" EnablePersistedSelection="true"
            AllowPaging="True" AllowSorting="True" CssClass="DDGridView"
            RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton runat="server" CommandName="Edit" Text="Edit" OnClientClick='return confirm("Super admin, Are you sure you want to edit this item?");' />&nbsp;
                        <asp:LinkButton runat="server" CommandName="Delete" Text="Delete"
                                    OnClientClick='return confirm("Super admin, Are you sure you want to delete this item?");' />&nbsp;
                        <asp:DynamicHyperLink runat="server" Text="Details" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
    
            <PagerStyle CssClass="DDFooter" />
            <PagerTemplate>
                <asp:GridViewPager runat="server" />
            </PagerTemplate>
            <EmptyDataTemplate>
                There are currently no items in this table.
            </EmptyDataTemplate>
    </asp:GridView>

    for here, I want the header row showing the localized string which marked in Metatable's MetaColumn, like the column `ItemName` which marked with RESOURCE KEY

    DynamicDataWebSite_Model_Item_Column_ItemName

     so How I can achieve this by a generic way that suits all types of models?

    Sunday, August 27, 2017 9:48 AM

All replies

  • User-330204900 posted

    I would extend the MetaTable and MeltaColumn classes and then use those in my application 

    i.e.

    public class LocalizedMetaModel : MetaModel

    and

    public class LocalizedSecureMetaTable : MetaTable

    and

    public class LocalizedSecureMetaColumn : MetaColumn

    etc.

    then override the DisplayName property get the LocalizeDisplayNameAttribute if it exists and return that localised name.

    This is a simplistic approach and does not take into account the clients local, which you may need to acquire from  the client and pass to the Table/Column via a function during the Session staturup.

    Sunday, August 27, 2017 12:55 PM
  • User-335504541 posted

    Hi shawn.shao,

    Since you could get the localized string in behind code, you could also set the header in behind code.

    For example:

     GridView1.HeaderRow.Cells[0].Text = localizedStr;

    Best Regards,

    Billy

    Monday, August 28, 2017 9:45 AM