How to localize the MvcSiteMapProvider

徘徊边缘 提交于 2019-11-29 11:37:45
NightOwl888

NOTE: An alternative approach to putting your resources in the App_GlobalResources folder (which isn't supported very well by MVC) would be to implement IStringLocalizer.

Localization has remained largely unchanged from Microsoft's SiteMapProvider, which MvcSiteMapProvider was originally based on. For localization, you can still follow the How to: Localize Site-Map Data document for the most part.

Enabling localization is done in the MvcSiteMapProvider_EnableLocalization setting in appSettings, or if using external DI, you would set it in your DI configuration by passing the setting to the enableLocalization constructor parameter of SiteMapBuilderSet. It is enabled by default.

I recommend using explicit localization, as implicit localization requires some hoops to get working and is likely to change in a future version.

The fields that are localizable are:

  • Title
  • Description
  • ImageUrl
  • Attributes (that are string datatype)

Here is an explicit localization example from the MvcMusicStore demo, which has localization used on certain nodes.

<mvcSiteMapNode title="$resources:SiteMapLocalizations,BrowseGenresTitle" controller="Store" action="Index"/>

It is referring to a file named SiteMapLocalizations.resx (and an item named BrowseGenresTitle) in the App_GlobalResources folder.

Alternatively, you can use multiple sitemaps in the same application for each locale which enables a way to have a different site structure (and different URLs) per locale.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!